繁体   English   中英

在S3存储桶中的Lambda Python boto3存储文件

[英]Lambda Python boto3 store file in S3 bucket

好的,我已经看到了一些这方面的例子,这是我在AWS Lambda Python 3.6中的代码:

# I just wrote out the file before this...
import boto3
tmp = open('/tmp/' + name_str,"rb") 
s3_client = boto3.resource('s3')
bucket = s3_client.Bucket(S3BUCKETNAME)
bucket.put_object(Key=name_str, Body=tmp, ContentType='text/csv', ContentEncoding='utf-8')

我得到的错误是:

's3.ServiceResource'对象没有属性'put_object':AttributeError

那么,我试试:

s3_client.upload_file('/tmp/' + name_str, S3BUCKETNAME, name_str)

's3.ServiceResource'对象没有属性'upload_file':AttributeError

所以...我必须遗漏一些基本的东西......还有其他一些进口吗? 为什么系统找不到这些功能?

这是对使用何种类型的误解。 应该是:

s3_client = boto3.client('s3')

但请注意,我现在实际使用的代码更像是:

s3_client = boto3.client('s3')
with open('/tmp/' + name_str) as file:
    object = file.read()
    s3_client.put_object(Body=object, Bucket=S3BUCKET, Key=name_str, ContentType='whatever/something', ContentEncoding='whatever-itis', StorageClass='PICK_ONE', ACL='you_choose')

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM