I've just created a lambda function with runtime Node.js 18.x. It retrieves a list of S3 objects from one bucket, but the copy is not working, even though the status returned is 'Successful'. I've also observed that the console.log before the copy command is output, but not the console.log following the command.
Does anyone see anything obvious that I've overlooked?
import { S3Client, ListObjectsV2Command, CopyObjectCommand } from "@aws-sdk/client-s3"
const client = new S3Client({})
export const handler = async (event) => {
const sourceBucket = 'in-bucket'
const destBucket = 'complete-bucket'
const dateSuffix = '-' + (new Date()).toISOString().substring(0,10).replaceAll('-','')
const commandList = new ListObjectsV2Command({Bucket: sourceBucket})
try {
let isTruncated = true
while (isTruncated) {
const { Contents, IsTruncated, NextContinuationToken } = await client.send(commandList)
Contents.forEach(async (c) => {
const fn = c.Key
const options = {
CopySource: encodeURI(`${sourceBucket}/${fn}`),
Bucket: destBucket,
Key: encodeURI(`${fn}${dateSuffix}`)
}
console.log(`moving ${fn}`)
const commandCopy = new CopyObjectCommand(options)
let response = await client.send(commandCopy)
console.log(`response: ${response}`)
})
isTruncated = IsTruncated
commandList.input.ContinuationToken = NextContinuationToken
}
console.log('Finished')
} catch (err) {
console.error(err)
}
}
Execution results:
Test Event Name
(unsaved) test event
Response
null
Function Logs
START RequestId: dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 Version: $LATEST
2023-07-27T17:50:28.279Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file01.csv
2023-07-27T17:50:28.281Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file02.csv
2023-07-27T17:50:28.337Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file03.csv
2023-07-27T17:50:28.338Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file04.csv
2023-07-27T17:50:28.338Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file05.csv
2023-07-27T17:50:28.338Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file06.csv
2023-07-27T17:50:28.338Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file07.csv
2023-07-27T17:50:28.339Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file08.csv
2023-07-27T17:50:28.339Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file09.csv
2023-07-27T17:50:28.340Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file10.csv
2023-07-27T17:50:28.340Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file11.csv
2023-07-27T17:50:28.340Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file12.csv
2023-07-27T17:50:28.340Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file13.csv
2023-07-27T17:50:28.341Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file14.csv
2023-07-27T17:50:28.341Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file15.csv
2023-07-27T17:50:28.397Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO moving file16.csv
2023-07-27T17:50:28.397Z dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 INFO Finished
END RequestId: dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2
REPORT RequestId: dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2 Duration: 1178.43 ms Billed Duration: 1179 ms Memory Size: 128 MB Max Memory Used: 103 MB Init Duration: 621.73 ms
Request ID
dbb33900-00e4-4e2f-8afe-9e9dd4c00cc2
I applied sample code, e.g., https://docs.aws.amazon.com/AmazonS3/latest/userguide/example_s3_CopyObject_section.html Since the first command returned all the expected key values, I expected the second command to copy the objects from the source bucket to the destination bucket.