Lab 12

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Lab-12: Demonstration on AWS Lambda Function to copy the data from one S3 bucket to another S3

bucket.
Create another bucket
Again Come to create function

And use refresh button


And click on deploy
delete object In s3 bucket
Go to test

Upload a file in the input bucket


import json

import boto3

def lambda_handler(event, context):

# Get the source and destination bucket names

source_bucket = event['Records'][0]['s3']['bucket']['name']

destination_bucket = 'outputs3bucket4654'
# Create an S3 client

s3 = boto3.client('s3')

try:

# List objects in the source bucket

response = s3.list_objects(Bucket=source_bucket)

if 'Contents' in response:

# Iterate through the objects and copy each to the destination bucket

for obj in response['Contents']:

object_key = obj['Key']

copy_source = {'Bucket': source_bucket, 'Key': object_key}

s3.copy_object(CopySource=copy_source, Bucket=destination_bucket, Key=object_key)

return {

'statusCode': 200,

'body': json.dumps('Copy operation successful!')

else:

return {

'statusCode': 200,

'body': json.dumps('No objects to copy.')

except Exception as e:

return {

'statusCode': 500,

'body': json.dumps(f'Error: {e}')

}
Json code

"Records": [

"s3":

"s3SchemaVersion": "1.0",

"bucket":

"name": "inputs3bucket4654"

}
Delection:

You might also like