Skip to content

Commit

Permalink
retrieve object metadata after renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisaCG committed Oct 17, 2024
1 parent bc3a86b commit 6e4b32d
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,28 +421,14 @@ export const renameS3Objects = async (
await s3Client.send(command);

if (Contents) {
// retrieve information of file or directory
const fileContents = await s3Client.send(
// retrieve content of file or directory
const oldFileContents = await s3Client.send(
new GetObjectCommand({
Bucket: bucketName,
Key: Contents[0].Key!
})
);

const body = await fileContents.Body?.transformToString();

data = {
name: newFileName,
path: newLocalPath,
last_modified: fileContents.LastModified!.toISOString(),
created: '',
content: body ? body : [],
format: fileFormat as Contents.FileFormat,
mimetype: fileMimeType,
size: fileContents.ContentLength!,
writable: true,
type: fileType
};
const body = await oldFileContents.Body?.transformToString();

const promises = Contents.map(async c => {
const remainingFilePath = c.Key!.substring(oldLocalPath.length);
Expand All @@ -461,6 +447,29 @@ export const renameS3Objects = async (
);
});
await Promise.all(promises);

// retrieve last modified time for new file, does not apply to remaming directory
const newFileMetadata = await s3Client.send(
new HeadObjectCommand({
Bucket: bucketName,
Key: newLocalPath
})
);

data = {
name: newFileName,
path: newLocalPath,
last_modified:
newFileMetadata.LastModified!.toISOString() ??
new Date().toISOString(),
created: '',
content: body ? body : [],
format: fileFormat as Contents.FileFormat,
mimetype: fileMimeType,
size: oldFileContents.ContentLength!,
writable: true,
type: fileType
};
}
if (isTruncated) {
isTruncated = IsTruncated;
Expand Down

0 comments on commit 6e4b32d

Please sign in to comment.