好的,我已经看到了一些这样的例子,这是我在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
所以...我必须遗漏一些基本的东西......还有其他一些进口吗? 为什么系统找不到这些功能?
ok, I've seen a few examples of this, and here is my code in 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')The error I get is :
's3.ServiceResource' object has no attribute 'put_object': AttributeError
Well, then I try:
s3_client.upload_file('/tmp/' + name_str, S3BUCKETNAME, name_str)'s3.ServiceResource' object has no attribute 'upload_file': AttributeError
So... I must be missing something basic... Is there some other import? Why can't the system find these functions?
最满意答案
这是对使用何种类型的误解。 应该是:
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')This was a misunderstanding of what type to use. It should have been:
s3_client = boto3.client('s3')
But note that the code I actually use now is more like:
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')更多推荐
发布评论