Question

I'm using django-storages for a project hosted on Heroku. I have django-storages configured and am now serving static files from S3 just fine.

I want to programmatically take an image located at an URL and upload it to S3 and associate it with a model.

I know how to download files from an url with either urlib2 or requests.

What I want to know is how best to get that image onto S3 and have it available to me to use with my model and associated templates. File size is indeterminate, but will probably always be less than 10MB in size.

My questions boil down to:

  • How do I get image located at an url onto S3?
  • Once image is on S3, how do I associate that image with a model?
  • Because of any inherent limitations on Heroku is there a way to avoid saving to Heroku storage and "stream" the file right to S3...or is this a non-issue?
Was it helpful?

Solution

I had the same problem, I use the following method to save the image

from boto.s3.key import Key
import urllib2
import StringIO
from django.conf import settings

def save_image_s3(self, img_url, img_name):
    """Saves the image in img_url into S3 with the name img_name"""
    conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME)
    k = Key(bucket)
    k.key = img_name
    fp = StringIO.StringIO(urllib2.urlopen(img_url).read())   
    k.set_contents_from_file(fp)
    return img_name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top