S3 Cross-Region In-Memory Transfer (NodeJS SDK) – By Example

It seemed unnecessary to use a Lambda or whatsoever to transfer an S3 file. However, for restricted regions like China mainland regions and gov regions, this could be useful.

It also shows how to directly upload to S3 from memory without writing to disk.

This is a Lambda function triggered by S3 PUT events.

    const sourceS3 = new AWS.S3();
    const { targetS3AK, targetS3SK, targetS3Region } = process.env;
    const targetS3 = new AWS.S3({
        accessKeyId: targetS3AK,
        secretAccessKey: targetS3SK,
        region: targetS3Region

    const sourceBucketName = event['Records'][0]['s3']['bucket']['name'];
    const sourceObjectKey = event['Records'][0]['s3']['object']['key'];

    try {
        const getResult = await sourceS3.getObject({
            Bucket: sourceBucketName,
            Key: sourceObjectKey
        const putResult = await targetS3.putObject({
            Bucket: process.env.targetBucketName,
            Key: sourceObjectKey,
            Body: getResult.Body // access object byte stream