Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there any feasible way to upload a file which is generated dynamically to amazon s3 directly without first create a local file and then upload to the s3 server? I use python. Thanks

like image 297
susanne Avatar asked Sep 24 '12 18:09

susanne


People also ask

How do I upload local files to aws S3?

To upload folders and files to an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.

How many ways you can upload data to S3?

There are three ways in which you can upload a file to amazon S3.

What happens if you upload the same file to S3?

By default, when you upload the file with same name. It will overwrite the existing file. In case you want to have the previous file available, you need to enable versioning in the bucket.


2 Answers

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

import boto from boto.s3.key import Key import requests  #setup the bucket c = boto.connect_s3(your_s3_key, your_s3_key_secret) b = c.get_bucket(bucket, validate=False)  #download the file url = "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) 
like image 154
JimJty Avatar answered Sep 20 '22 16:09

JimJty


You could use BytesIO from the Python standard library.

from io import BytesIO bytesIO = BytesIO() bytesIO.write('whee') bytesIO.seek(0) s3_file.set_contents_from_file(bytesIO) 
like image 34
Roy Hyunjin Han Avatar answered Sep 20 '22 16:09

Roy Hyunjin Han