If you want to save a list on Amazon S3 using a Lambda function and Boto3, you can follow these steps:
1 First, you need to create a new Lambda function in the AWS Console. Choose the Python runtime and give it an appropriate name.
2 In the Lambda function code, you will need to import the Boto3 library by adding the following code at the beginning of your function:
import boto3
3 Next, you need to create a connection to S3 using the Boto3 client. To do this, add the following code to your function:
s3_client = boto3.client('s3', 'eu-central-1')
4 Now you can use the put_object
method to save your list to S3. For example, if your list is called my_list
and you want to save it to a bucket named bucket_name
and a file named my_list.txt
, you can use the following code:
s3_client.put_object(Bucket='bucket_name', Key='my_list.txt', Body=str(my_list))
5 Finally, save your Lambda function and test it by invoking it. You should now see your list saved to S3 in the specified bucket and file name.
Full code:
import json
import boto3
def lambda_handler(event, context):
bucket_name = "save-data-from-lambda"
list001 = ['this', 'is', 'first', 'list']
s3_client = boto3.client('s3', 'eu-central-1')
# save to s3
save_to_s3 = s3_client.put_object(
Bucket=bucket_name,
Key='my_list.txt',
Body=str(list001)
)
That's it! You now know how to save a list to S3 using a Lambda function and Boto3.
I also prepared a video in which you will learn more details. You will learn how to save and read files from S3 bucket:
If you are interested, I also encourage you to visit my blog where you will find the code that you can easily copy and adapt to your needs:
https://lepczynski.it/en/aws_en/how-to-read-and-write-a-file-on-s3-using-lambda-function-and-boto3/
Top comments (0)