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
        }).promise();
        
        console.log(getResult);
        
        const putResult = await targetS3.putObject({
            Bucket: process.env.targetBucketName,
            Key: sourceObjectKey,
            Body: getResult.Body // access object byte stream
        }).promise();
        
        console.log(putResult);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s