簡體   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