Python 使用 Boto3 作为字符串打开 S3 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31976273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:49:58  来源:igfitidea点击:

Open S3 object as a string with Boto3

pythonamazon-s3botoboto3

提问by Gahl Levy

I'm aware that with Boto 2 it's possible to open an S3 object as a string with: get_contents_as_string()

我知道使用 Boto 2 可以将 S3 对象作为字符串打开: get_contents_as_string()

Is there an equivalent function in boto3 ?

boto3 中是否有等效功能?

回答by Gahl Levy

This isn't in the boto3 documentation. This worked for me:

这不在 boto3 文档中。这对我有用:

object.get()["Body"].read()

object being an s3 object: http://boto3.readthedocs.org/en/latest/reference/services/s3.html#object

对象是 s3 对象:http: //boto3.readthedocs.org/en/latest/reference/services/s3.html#object

回答by Kamil Sindi

readwill return bytes. At least for Python 3, if you want to return a string, you have to decode using the right encoding:

read将返回字节。至少对于 Python 3,如果你想返回一个字符串,你必须使用正确的编码进行解码:

import boto3

s3 = boto3.resource('s3')

obj = s3.Object(bucket, key)
obj.get()['Body'].read().decode('utf-8') 

回答by Pyglouthon

If body contains a io.StringIO, you have to do like below:

如果 body 包含 io.StringIO,则必须执行以下操作:

object.get()['Body'].getvalue()

回答by EvgenyKolyakov

I had a problem to read/parse the object from S3 because of .get()using Python 2.7 inside an AWS Lambda.

由于.get()在 AWS Lambda中使用 Python 2.7,我在从 S3 读取/解析对象时遇到问题。

I added json to the example to show it became parsable :)

我在示例中添加了 json 以显示它可以解析:)

import boto3
import json

s3 = boto3.client('s3')

obj = s3.get_object(Bucket=bucket, Key=key)
j = json.loads(obj['Body'].read())

NOTE (for python 2.7): My object is all ascii, so I don't need .decode('utf-8')

注意(对于 python 2.7):我的对象都是 ascii,所以我不需要 .decode('utf-8')

NOTE (for python 3.6+): We moved to python 3.6 and discovered that read()now returns bytesso if you want to get a string out of it, you must use:

注意(对于 python 3.6+):我们转移到 python 3.6 并发现read()现在返回,bytes所以如果你想从中获取一个字符串,你必须使用:

j = json.loads(obj['Body'].read().decode('utf-8'))

j = json.loads(obj['Body'].read().decode('utf-8'))

回答by Gatsby Lee

Python3 + Using boto3 API approach.

Python3 + 使用 boto3 API 方法。

By using S3.Client.download_fileobj APIand Python file-like object, S3 Object content can be retrieved to memory.

通过使用S3.Client.download_fileobj APIPython 类文件对象,可以将 S3 对象内容检索到内存中。

Since the retrieved content is bytes, in order to convert to str, it need to be decoded.

由于检索到的内容是字节,为了转换为str,需要对其进行解码。

import io
import boto3

client = boto3.client('s3')
bytes_buffer = io.BytesIO()
client.download_fileobj(Bucket=bucket_name, Key=object_key, Fileobj=bytes_buffer)
byte_value = bytes_buffer.getvalue()
str_value = byte_value.decode() #python3, default decoding is utf-8