Download Image file and upload to database

Hi, I have a form that enables a user to upload an image from twitter based upon the handle. I trying to automate the download/upload to improve the UX. I then want to use the image for other pages.

I think i have two options, 1 would be to dynamically retrieve the image using http as the path and the other is would be to download and store the path to the media folder location in the model.

I think from a performance perspective it would be better to reference the image stored in the media folder rather than loading via http each time.

So I am trying to build this into my view.

username = 'someUser'

consumer_key = "######..."
consumer_secret = "######..."
access_token = "######..."
access_token_secret = "######..."
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user = api.get_user(user_id = username ,screen_name = username)
url = user.profile_image_url
url = url.replace('_normal.jpg','.jpg')

remote_url = url
local_file = 'profile.jpg'
request.urlretrieve(remote_url, local_file)


This allows me to download the file and store it on a local disk which won’t work once in production.

My Model

profile_image = ResizedImageField(size=[400, 400],crop=['middle', 'center'],quality=100,upload_to='images/profiles/', blank=True)

I could maybe save it to images/profiles/<something>.jpg and then within my view, I reference this file and delete it after?

        if form.is_valid():
            username = request.POST['twitter_handle'] # for POST form method
            user = api.get_user(user_id = username ,screen_name = username)
            url = user.profile_image_url
            url = url.replace('_normal.jpg','.jpg')
            remote_url = url
            local_file = 'images/profile'
            urllib.request.urlretrieve(remote_url, local_file)
            form =
            form.profile = profile
            form.image = local_file

Ideally, I need the file to be uploaded to my media_root path, but I feel like I am approaching this the wrong way.


Take a look at the requests library, specifically the response object.

Using it to retrieve the image will give you an object that you can then write to any desired location - including passing it through any type of cropping / filtering tool you may desire.

Hi Ken,

I’m using the following to get the file and save the content, which works great when writing locally, but won’t work in production cause I’ve repointed my static and media root to the azure storage account.

I’m using r = requests.get(url) to return the object and then I’ve tried to replace the local path with the remote path but it fails with

OSError: [Errno 22] Invalid argument: ''

open(img_url, 'wb').write(r.content)

I’ve thought it might be the permissions on the storage account so I have passed in a SAS token in the URL with R/W permissions but still the same error.

Any clues? or is this the wrong way to be doing this?



Sorry, no clue here. You’d want to check the docs for azure to see what APIs they may have available for writing to a file.

You may need to POST that content to the appropriate URL as an http request rather than trying to write it as a file - but that’s as much a guess on my part as anything else.