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

Rob*_*ann 5 amazon-s3 python-3.x boto3 aws-lambda

好的,我已经看到了一些这样的例子,这是我在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')
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

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

那么,我试试:

s3_client.upload_file('/tmp/' + name_str, S3BUCKETNAME, name_str)
Run Code Online (Sandbox Code Playgroud)

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

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

Rob*_*ann 5

这是对使用什么类型的误解。本来应该是:

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')
Run Code Online (Sandbox Code Playgroud)