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

时间:2017-11-16 12:33:35

标签: python-3.x amazon-s3 aws-lambda boto3

好的,我已经看到了一些这方面的例子,这是我在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的

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

1 个答案:

答案 0 :(得分:4)

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

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