How to upload a file to S3 without creating a temporary local file How to upload a file to S3 without creating a temporary local file python python

How to upload a file to S3 without creating a temporary local file


Here is an example downloading an image (using requests library) and uploading it to s3, without writing to a local file:

import botofrom boto.s3.key import Keyimport requests#setup the bucketc = boto.connect_s3(your_s3_key, your_s3_key_secret)b = c.get_bucket(bucket, validate=False)#download the fileurl = "http://en.wikipedia.org/static/images/project-logos/enwiki.png"r = requests.get(url)if r.status_code == 200:    #upload the file    k = Key(b)    k.key = "image1.png"    k.content_type = r.headers['content-type']    k.set_contents_from_string(r.content)


You could use BytesIO from the Python standard library.

from io import BytesIObytesIO = BytesIO()bytesIO.write('whee')bytesIO.seek(0)s3_file.set_contents_from_file(bytesIO)


The boto library's Key object has several methods you might be interested in:

For an example of using set_contents_from_string, see Storing Data section of the boto documentation, pasted here for completeness:

>>> from boto.s3.key import Key>>> k = Key(bucket)>>> k.key = 'foobar'>>> k.set_contents_from_string('This is a test of S3')