-
Notifications
You must be signed in to change notification settings - Fork 3
/
copyFunction.js
43 lines (40 loc) · 1.21 KB
/
copyFunction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Load the AWS SDK
const aws = require('aws-sdk');
// Construct the AWS S3 Object
const s3 = new aws.S3({
apiVersion: '2006-03-01'
});
// Define 2 new variables for the source and destination buckets
var srcBucket = process.env.SOURCE_BUCKET;
var destBucket = process.env.DEST_BUCKET + '/';
//Main function
exports.handler = (event, context, callback) => {
//Copy the current object to the destination bucket
if(event.delete){
s3.deleteObject({
Bucket: destBucket + event.destRoute,
Key: event.sourceObject
}, function(deleteErr, data) {
if (deleteErr) {
console.log("Error: " + deleteErr);
}
else {
console.log('Successfully deleted the item');
}
});
}
else {
s3.copyObject({
CopySource: srcBucket + '/' + event.sourceRoute + '/' + event.sourceObject,
Bucket: destBucket + event.destRoute,
Key: event.sourceObject
}, function(copyErr, copyData){
if (copyErr) {
console.log("Error: " + copyErr);
} else {
console.log('Successfully copied the item');
}
});
}
callback(null, 'All Done!');
};