问题
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