Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a file directly to S3

Given a resource link, for example:

http://www.google.com/images/srpr/logo3w.png

Is there a way to download that png directly to S3 (preferably using Boto)? If so, how would this be done?

like image 845
David542 Avatar asked Feb 24 '13 08:02

David542


2 Answers

You can use urllib2 to fetch the file and use the response object to write it into S3 using boto.

from boto.s3.connection import S3Connection
from boto.s3.key import Key
import urllib2

request = urllib2.Request('http://www.google.com/images/srpr/logo3w.png')
response = urllib2.urlopen(request)

conn = S3Connection("MYAWSID", "MYAWSSECRET")
bucket = conn.create_bucket('MyBucket')
k = Key(bucket)
k.name = "logo3w"
k.set_contents_from_string(response.read(), {'Content-Type': response.info().gettype()})

If you don't have the packages you can install them from PIP using

pip install urllib2_file
pip install boto
like image 80
John Hawkins Avatar answered Sep 23 '22 19:09

John Hawkins


Any 'download to S3' implicitly means 'download and then upload to S3' - whether you do that upload manually or a script or library like boto does it.

If using a script or library (boto) it would download the image to a file-system attached to the system it was running on - your local workstation or a server - and then use the AWS keys and libraries to upload it to S3.

like image 37
Akber Choudhry Avatar answered Sep 23 '22 19:09

Akber Choudhry