Friday, March 24, 2023

lambda function that upload a file to an S3 bucket

example of a lambda function that uploads a file to an S3 bucket using the Boto3 library:Python lam

python
import boto3 
def lambda_handler(event, context): 
# Set the S3 bucket and object key 
 s3_bucket = 'your-bucket-name' 
 s3_key = 'path/to/your/file.txt' 
# Create a new S3 resource and upload the file 
s3 = boto3.resource('s3'
 s3.meta.client.upload_file('/tmp/file.txt', s3_bucket, s3_key) 
# Return a success message 
return { 'statusCode': 200, 'body': 'File uploaded to S3' }

This function assumes that the file you want to upload is located in the /tmp directory of the lambda function's runtime environment. You can modify the s3_bucket and s3_key variables to match the S3 bucket and object key you want to upload the file to.

You'll also need to make sure that your lambda function has the necessary permissions to access your S3 bucket. You can do this by creating an IAM role with the AmazonS3FullAccess policy and assigning it to your lambda function

No comments: