使用 Boto3 作为字符串打开 S3 对象 [英] Open S3 object as a string with Boto3

查看:23
本文介绍了使用 Boto3 作为字符串打开 S3 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

boto3 中是否有等效的函数?

Is there an equivalent function in boto3 ?

推荐答案

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

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

import boto3

s3 = boto3.resource('s3')

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

这篇关于使用 Boto3 作为字符串打开 S3 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆