How do you perform a credentialed download from s3 using boto3 without saving a file?

眉间皱痕 提交于 2021-01-28 04:15:38

问题


It is simple to perform a credentialed download from s3 to a file using

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)

It is easy to download from s3 to a file-like object using

import from StringIO import StringIO
import urllib

file_like_object = StringIO(urllib.urlopen(url).read())

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

But how do you perform a credentialed download from s3 to a file-like object?


回答1:


You just need to make a call to 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')


来源:https://stackoverflow.com/questions/34209938/how-do-you-perform-a-credentialed-download-from-s3-using-boto3-without-saving-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!