如何在不保存文件的情况下使用boto3从s3执行凭证下载?

时间:2015-12-10 19:16:49

标签: python amazon-s3 boto3

使用

执行从s3到文件的凭证下载很简单
int[] array2 = new int[array.length];
for(int x = 0; x < array2.length; x++){
    array2[x] = array[array.length - x - 1];
}

使用

可以轻松地从s3下载到类似文件的对象
import boto3

s3 = boto3.resource('s3')
def save_file_from_s3(bucket_name, key_name, file_name):
    b = s3.Bucket(bucket_name)
    b.download_file(key_name, file_name)

(见How do I read image data from a URL in Python?

但是,如何从s3执行凭证下载到类似文件的对象?

1 个答案:

答案 0 :(得分:3)

您只需拨打s3.Bucket.Object.get

即可
import boto3

s3 = boto3.resource('s3')
# obj = s3.Object(bucket_name, key_name).get()
bucket = s3.Bucket(bucket_name)
obj = bucket.Object(key_name).get()
body = obj.get('Body')