Created
November 3, 2022 17:33
-
-
Save chrishowell/9eb0ddf29e57403c41500506ea4f661e to your computer and use it in GitHub Desktop.
LambdaReplicator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('Loading function'); | |
const aws = require('aws-sdk'); | |
const s3 = new aws.S3({ apiVersion: '2006-03-01' }); | |
exports.handler = async (event, context) => { | |
const bucket = event.Records[0].s3.bucket.name; | |
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); | |
const params = { | |
Bucket: bucket, | |
Key: key, | |
}; | |
const destRegions = [ | |
"euw1", | |
"euc1", | |
"use1", | |
"usw2", | |
"apse1", | |
"apse2" | |
] | |
const destParamsTemplate = { | |
CopySource: "/" + bucket + "/" + key, | |
Key: key | |
}; | |
var operations = [] | |
for (var i in destRegions) { | |
const destParams = {} | |
Object.assign(destParams, destParamsTemplate); | |
destParams.Bucket = bucket + "-" + destRegions[i] | |
console.log("Copy request with params: " + JSON.stringify(destParams)) | |
const regionPush = s3.copyObject(destParams).promise() | |
operations.push(regionPush) | |
} | |
await Promise.allSettled(operations) | |
.then((results) => results.forEach((result) => console.log(result))); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment