Thursday 6 May 2021

Copy an Object between AWS Accounts using boto3


MOVE S3 OBJECT BETWEEN AWS ACCOUNTS USING BOTO3

Before moving on to the code, let's go through about the /tmp directory provided by the Lambda function.

Temporary storage with /tmp

  • The lambda function execution environment provides a file system for the code to use at /tmp.
  • On each new execution environment is created.
  • It is important to know /tmp is not recreated or cleared on each lambda invocation.
  • And it is okay/safe to use /tmp in Lambda since this folder is not shared with other AWS clients.

Here I have taken a scenario to copy an S3 object from PROD to DEV using boto3 in a lambda function.

import boto3

# AWS credentials of PROD environemnt
s3 = boto3.client('s3',aws_access_key_id='awsAccessKey',
aws_secret_access_key='awsSecretAccessKey')

# AWS credentials of DEV environemnt
s3Dev = boto3.client('s3',aws_access_key_id='awsAccessKeyDev',
aws_secret_access_key=awsSecretAccessKeyDev)

def lambda_handler(event, context):

#Downloads the file from PROD S3 bucket and saves in temporary storage
    s3.download_file('source_bucket', 'folder/source.csv', '/tmp/source.csv')

    print('File Downloaded')

    file = '/tmp//source.csv'

#Uploads the file in temporary space to DEV S3 bucket 
    s3Dev.upload_file(file , 'target_bucket','folder/target.csv')

    print('File Uploaded')

    return {
         'statusCode': 200
    }



Here are some other posts that might be helpful :

Convert Nested JSON to CSV using Python in S3


No comments:

Post a Comment