2

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?

Community
  • 1
  • 1
Doug Bradshaw
  • 1,452
  • 1
  • 16
  • 20

1 Answers1

5

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')
Jordon Phillips
  • 14,963
  • 4
  • 35
  • 42
  • This returns a dictionary object. But what I want is a file type object that I can then process, for example with Pillow. – Doug Bradshaw Dec 10 '15 at 20:06
  • 1
    The dictionary object contains the file object under the keyword "Body". So to complete the example, just use f = obj['Body'] – Doug Bradshaw Dec 10 '15 at 20:18