How could I use aws lambda to write file to s3 (python)?

后端 未结 2 764
后悔当初
后悔当初 2021-02-02 08:52

I have tried to use lambda function to write a file to S3, then test shows \"succeeded\" ,but nothing appeared in my S3 bucket. What happened? Does anyone can give me some advic

2条回答
  •  天涯浪人
    2021-02-02 09:51

    My response is very similar to Tim B but the most import part is

    1.Go to S3 bucket and create a bucket you want to write to

    2.Follow the below steps otherwise you lambda will fail due to permission/access. I've copied and pasted it the link content here for you too just in case if they change the url /move it to some other page.

    a. Open the roles page in the IAM console.

    b. Choose Create role.

    c. Create a role with the following properties.

    -Trusted entity – AWS Lambda.

    -Permissions – AWSLambdaExecute.

    -Role name – lambda-s3-role.

    The AWSLambdaExecute policy has the permissions that the function needs to manage objects in Amazon S3 and write logs to CloudWatch Logs.

    1. Copy and past this into your Lambda python function

      import json, boto3,os, sys, uuid
      from urllib.parse import unquote_plus
      
      s3_client = boto3.client('s3')
      
      def lambda_handler(event, context):
          some_text = "test"
          #put the bucket name you create in step 1
          bucket_name = "my_buck_name"
          file_name = "my_test_file.csv"
          lambda_path = "/tmp/" + file_name
          s3_path = "output/" + file_name
          os.system('echo testing... >'+lambda_path)
          s3 = boto3.resource("s3")
          s3.meta.client.upload_file(lambda_path, bucket_name, file_name)
      
          return {
              'statusCode': 200,
              'body': json.dumps('file is created in:'+s3_path)
          }
      

提交回复
热议问题