Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# An AWS Lambda Based Function to Copy S3 Objects

With this AWS Lambda function, you can copy objects from a source S3 bucket to one or more target S3 buckets as they are added to the source bucket.
With this AWS Lambda function, you can copy objects from a source S3 bucket to one or more target S3 buckets as they are added or removed to/from the source bucket.

## Configuration

Expand All @@ -18,7 +18,8 @@ Create an IAM role with the following policy:
"Action": [
"s3:GetBucketTagging",
"s3:GetObject",
"s3:PutObject"
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": [
"*"
Expand Down Expand Up @@ -83,7 +84,11 @@ npm install aws-sdk
* Event Source Type: S3
* Bucket: your source bucket
* Event Type: Object Created
4. Set your Lambda function to execute using the IAM role you created above.
4. Add another event source to your Lambda function:
* Event Source Type: S3
* Bucket: your source bucket
* Event Type: Objects Removed (All)
5. Set your Lambda function to execute using the IAM role you created above.

### Configuration

Expand Down
54 changes: 38 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function processRecord(record, callback) {

// The source bucket and source key are part of the event data
var srcBucket = record.s3.bucket.name;
var eventName = record.eventName;
var srcKey = decodeURIComponent(record.s3.object.key.replace(/\+/g, " "));

// Get the target bucket(s) based on the source bucket.
Expand All @@ -68,24 +69,45 @@ function processRecord(record, callback) {
var targetKey = srcKey;

console.log("Copying '" + srcKey + "' from '" + srcBucket + "' to '" + targetBucketName + "'");
console.log(record);

console.log(eventName);
if (eventName == "ObjectRemoved:DeleteMarkerCreated") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about non-versioned S3 buckets? Is this same event triggered or is it different?

console.log("DELETE MARKER CREATED")
var s3 = createS3(regionName);
s3.deleteObject({
Bucket: targetBucket,
Key: targetKey
},
function (err, data) {
if (err) {
console.log("Error deleting '" + srcKey + "' from '" + srcBucket + "' to '" + targetBucketName + "'");
console.log(err, err.stack); // an error occurred
callback("Error deleting '" + srcKey + "' from '" + srcBucket + "' to '" + targetBucketName + "'");
} else {
callback();
}
});
}
// Copy the object from the source bucket
var s3 = createS3(regionName);
s3.copyObject({
Bucket: targetBucket,
Key: targetKey,

CopySource: encodeURIComponent(srcBucket + '/' + srcKey),
MetadataDirective: 'COPY'
}, function (err, data) {
if (err) {
console.log("Error copying '" + srcKey + "' from '" + srcBucket + "' to '" + targetBucketName + "'");
console.log(err, err.stack); // an error occurred
callback("Error copying '" + srcKey + "' from '" + srcBucket + "' to '" + targetBucketName + "'");
} else {
callback();
}
});
if (eventName == "ObjectCreated:Put") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be else if.

var s3 = createS3(regionName);
s3.copyObject({
Bucket: targetBucket,
Key: targetKey,

CopySource: encodeURIComponent(srcBucket + '/' + srcKey),
MetadataDirective: 'COPY'
}, function (err, data) {
if (err) {
console.log("Error copying '" + srcKey + "' from '" + srcBucket + "' to '" + targetBucketName + "'");
console.log(err, err.stack); // an error occurred
callback("Error copying '" + srcKey + "' from '" + srcBucket + "' to '" + targetBucketName + "'");
} else {
callback();
}
});
};
}, function (err) {
if (err) {
callback(err);
Expand Down