Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports.moveFile = function (event, context) {
- const objectKey = event.objectKey;
- const bucketName = event.bucketName;
- const newLocation = 'processed/' + objectKey;
- const targetBucket = process.env.TARGET_BUCKET;
- const s3 = new AWS.S3();
- console.log('Moving "', objectKey, '" to new location "', newLocation, '"');
- async.waterfall([
- (next) => {
- s3.copyObject({
- Bucket: targetBucket,
- Key: newLocation,
- CopySource: bucketName + '/' + encodeURIComponent(objectKey)
- }, next);
- },
- (data, next) => {
- s3.waitFor('objectExists', {
- Bucket: targetBucket,
- Key: newLocation
- }, next);
- },
- (data, next) => {
- s3.deleteObject({
- Bucket: bucketName,
- Key: objectKey
- }, next);
- }
- ], (error) => {
- if (error) {
- console.log('Failed to move file', error);
- context.fail();
- } else {
- context.succeed({
- bucketName: event.bucketName,
- objectKey: event.objectKey,
- newLocation: newLocation
- });
- }
- });
- };
Add Comment
Please, Sign In to add comment