How to get contents of a text file from AWS s3 using a lambda function?

后端 未结 3 1807
栀梦
栀梦 2020-12-13 05:54

I was wondering if I could set up a lambda function for AWS, triggered whenever a new text file is uploaded into an s3 bucket. In the function, I would like to get the conte

3条回答
  •  情书的邮戳
    2020-12-13 06:27

    I am using lambda function with a python 3.6 environment. The code below will read the contents of a file main.txt inside bucket my_s3_bucket. Make sure to replace name of bucket and file name according to your needs.

    def lambda_handler(event, context):
        # TODO implement
        import boto3
    
        s3 = boto3.client('s3')
        data = s3.get_object(Bucket='my_s3_bucket', Key='main.txt')
        contents = data['Body'].read()
        print(contents)
    

提交回复
热议问题