在S3存储桶中的Lambda Python boto3存储文件(Lambda Python boto3 store file in S3 bucket)

编程入门 行业动态 更新时间:2024-10-28 03:27:55
在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

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

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

更多推荐

本文发布于:2023-08-06 17:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Python   Lambda   bucket   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!