Guest User

Untitled

a guest
Dec 13th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. module.exports.moveFile = function (event, context) {
  2. const objectKey = event.objectKey;
  3. const bucketName = event.bucketName;
  4. const newLocation = 'processed/' + objectKey;
  5. const targetBucket = process.env.TARGET_BUCKET;
  6. const s3 = new AWS.S3();
  7.  
  8. console.log('Moving "', objectKey, '" to new location "', newLocation, '"');
  9. async.waterfall([
  10. (next) => {
  11. s3.copyObject({
  12. Bucket: targetBucket,
  13. Key: newLocation,
  14. CopySource: bucketName + '/' + encodeURIComponent(objectKey)
  15. }, next);
  16. },
  17. (data, next) => {
  18. s3.waitFor('objectExists', {
  19. Bucket: targetBucket,
  20. Key: newLocation
  21. }, next);
  22. },
  23. (data, next) => {
  24. s3.deleteObject({
  25. Bucket: bucketName,
  26. Key: objectKey
  27. }, next);
  28. }
  29. ], (error) => {
  30. if (error) {
  31. console.log('Failed to move file', error);
  32. context.fail();
  33. } else {
  34. context.succeed({
  35. bucketName: event.bucketName,
  36. objectKey: event.objectKey,
  37. newLocation: newLocation
  38. });
  39. }
  40. });
  41. };
Add Comment
Please, Sign In to add comment