Chicago Django/Python Blog - Imaginary Landscape

Dan Johnson

Rackspace Temporary URLs with django-cumulus

Dan Johnson—Programmer

December 31, 2012

We love Rackspace at Imaginary Landscape.  Recently, we needed to store a number of large files securely, and for logistical reasons, we chose to store them via Rackspace’s Cloud Files.  Rackspace provides the ability to create temporary URLs for files stored via their service.  This allows temporary access to privately hosted files via a Web address.  This is useful to prevent sharing a file's URL, as the address is only valid for a short amount of time.  More information about temporary URLs with Rackspace Cloud Files can be found here: http://www.rackspace.com/blog/rackspace-cloud-files-how-to-create-temporary-urls/.

django-cumulus is an excellent package for dealing with Rackspace cloud files. However, it provides no native support for creating temporary, expiring URLs.

Therefore, we chose to fork django-cumulus and add this functionality.  The purpose of this post is to provide additional information about how to setup and configure the extra options to enable temporary file URLs.  For additional information about setting up django-cumulus initially, please refer to the django-cumulus documentation.  It’s fantastic.

Setting up your Rackspace account with the necessary information isn’t straightforward.  It requires setting some information that cannot be set via Rackspace’s control panel.  Rackspace allows these to be set via POST requests. 

To summarize, we need to get two pieces of information about our account:  X-Storage-Url and X-Auth-Token.  We will then use these to set a X-Account-Meta-Temp-Url-Key for this account.  In the example below, we will use curl to get and set this information:

First, we need to get some information from Rackspace about our account.

curl -v -H "X-Auth-User: USERNAME" -H "X-Auth-Key: APIACCESSKEY" https://auth.api.rackspacecloud.com/v1.0

This will return two important items that we need to use in the next step:

  • X-Storage-Url
  • X-Auth-Token

Second, we need to set the X-Account-Meta-Temp-Url-Key.  This key is used in your site-specific settings.  If you forget/lose this key, you will need to reset it using this same process.

curl -X POST -H "X-Auth-Token: X-Auth-Token-From-Response” -H "X-Account-Meta-Temp-Url-Key: TheKeyYouWantToUse" X-Storage-Url-From-Response

Third, to use temporary URLs we just need to set a few additional CUMULUS settings in our project’s settings.py

CUMULUS = {
    ...
    'PUBLIC': False,
    'X_ACCOUNT_META_TEMP_URL_KEY': ‘KeyYouSetEarlier’,
    'X_STORAGE_URL': 'X-Storage-Url-From-Response,
    ‘X_TEMP_URL_TIMEOUT’: 900, #Time in seconds. This is not required and defaults to 600
}

Finally, when we access the URL property of an image or file object in Django, it will return a temporary URL instead of a public URL.

The code for this fork is available at https://github.com/ImaginaryLandscape/django-cumulus.


Updated 12/31/12 @ 01:59PM CST by djohnson

Bookmark and Share

Categories: Django

(877) 275-9144 Toll Free | M-F:9-5

Imaginary Landscape, LLC 5121 North Ravenswood Avenue Chicago, Illinois 60640
The domain chicagodjango.com is used with the permission of the Django Software Foundation. Imaginary Landscape LLC is not an official representative of the Django Software Foundation or the Django open source software project.