好的,我已经看到了一些示例,这是我在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_client.upload_file('/tmp/' + name_str, S3BUCKETNAME, name_str)



所以...我一定缺少基本的东西...还有其他输入吗?系统为什么找不到这些功能?

最佳答案

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

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')

关于python-3.x - Lambda Python boto3将文件存储在S3存储桶中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47329773/

10-12 14:02