简体   繁体   English

我如何使用 aws lambda 将文件写入 s3 (python)?

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

I have tried to use lambda function to write a file to S3, then test shows "succeeded" ,but nothing appeared in my S3 bucket.我尝试使用 lambda 函数将文件写入 S3,然后测试显示“成功”,但我的 S3 存储桶中没有出现任何内容。 What happened?发生了什么? Does anyone can give me some advice or solutions?有人可以给我一些建议或解决方案吗? Thanks a lot.非常感谢。 Here's my code.这是我的代码。

import json
import boto3

def lambda_handler(event, context):

string = "dfghj"

file_name = "hello.txt"
lambda_path = "/tmp/" + file_name
s3_path = "/100001/20180223/" + file_name

with open(lambda_path, 'w+') as file:
    file.write(string)
    file.close()

s3 = boto3.resource('s3')
s3.meta.client.upload_file(lambda_path, 's3bucket', s3_path)

I've had success streaming data to S3, it has to be encoded to do this:我已经成功地将数据流式传输到 S3,必须对其进行编码才能执行此操作:

import boto3

def lambda_handler(event, context):
    string = "dfghj"
    encoded_string = string.encode("utf-8")

    bucket_name = "s3bucket"
    file_name = "hello.txt"
    s3_path = "100001/20180223/" + file_name

    s3 = boto3.resource("s3")
    s3.Bucket(bucket_name).put_object(Key=s3_path, Body=encoded_string)

If the data is in a file, you can read this file and send it up:如果数据在文件中,您可以读取此文件并将其发送:

with open(filename) as f:
    string = f.read()

encoded_string = string.encode("utf-8")

My response is very similar to Tim B but the most import part is我的回答与 Tim B 非常相似,但最重要的部分是

1.Go to S3 bucket and create a bucket you want to write to 1.转到S3存储桶并创建一个要写入的存储桶

2.Follow the below steps otherwise you lambda will fail due to permission/access. 2.按照以下步骤操作,否则您的 lambda 将因权限/访问而失败。 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.在 IAM 控制台中打开角色页面

b.湾。 Choose Create role.选择创建角色。

c. C。 Create a role with the following properties.创建具有以下属性的角色。

-Trusted entity – AWS Lambda. - 受信任的实体——AWS Lambda。

-Permissions – AWSLambdaExecute. - 权限 – AWSLambdaExecute。

-Role name – lambda-s3-role. -角色名称 - 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. AWSLambdaExecute 策略具有该函数管理 Amazon S3 中的对象并将日志写入 CloudWatch Logs 所需的权限。

  1. Copy and past this into your Lambda python function将其复制并粘贴到您的 Lambda python 函数中

    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) }
from os import path
import json, boto3, sys, uuid
import requests
s3_client = boto3.client('s3')

def lambda_handler(event, context):
    bucket_name = "mybucket"
    url = "https://i.imgur.com/ExdKOOz.png"
    reqponse = requests.get(url)
    filenname = get_filename(url)
    img = reqponse.content
    s3 = boto3.resource("s3")
    s3.Bucket(bucket_name).put_object(Key=filenname, Body=img)
    
    return {'statusCode': 200,'body': json.dumps('file is created in:')}

def get_filename(url):
    fragment_removed = url.split("#")[0]
    query_string_removed = fragment_removed.split("?")[0]
    scheme_removed = query_string_removed.split("://")[-1].split(":")[-1]
    if scheme_removed.find("/") == -1:
       return ""
    return path.basename(scheme_removed)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 AWS Lambda (python) 编写 csv 文件并将其保存到 S3 中 - Write csv file and save it into S3 using AWS Lambda (python) 如何从 S3 加载泡菜文件以在 AWS Lambda 中使用? - How to load a pickle file from S3 to use in AWS Lambda? 如何使用 Lambda 和 Python 在 AWS s3 中读取和覆盖文件? - How to read and overwrite a file in AWS s3 using Lambda and Python? how to get s3 object key by object url when I use aws lambda python?or How to get object by url? - how to get s3 object key by object url when I use aws lambda python?or How to get object by url? 如何使用 S3 和 Slack 集成编写 AWS lambda 函数 - How to write a AWS lambda function with S3 and Slack integration 如何使用 AWS Lambda Python 读取 AWS S3 存储的 word 文档(.doc 和 .docx)文件内容? - How to read AWS S3 stored word document (.doc and .docx) file content using AWS Lambda Python? AWS Lambda s3文件解压缩python中的文件名问题 - File name issue in AWS lambda s3 file unzip python 使用AWS lambda函数使用boto3 python将S3文件从zip转换为gzip - Use AWS lambda function to convert S3 file from zip to gzip using boto3 python 从 s3 存储桶中获取 python 类文件并在 aws lambda 中使用它 - get python class file from s3 bucket and use it inside aws lambda 使用AWS Lambda创建CSV并保存到S3(Python) - Use AWS Lambda to create CSV and save to S3 (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM