Dropbox file upload handler for django

By : Thejaswi Puthraya

Dropbox announced new pro plans last week and some accounts have had their storage size doubled. Wouldn't it be wonderful if we could upload all our files to dropbox from our django webapp?

In this post, I write a custom file upload handler that will upload files from our application to dropbox.

Let us see how to use the custom file upload handler.

Install the Dropbox Python SDK before you setup your django app to handle the file uploads.

In your settings.py, add the following attributes (with the values filled):

DROPBOX_APP_KEY = ""
DROPBOX_APP_SECRET_KEY = ""
DROPBOX_APP_ACCESS_TOKEN = ""
DROPBOX_APP_ACCESS_TOKEN_SECRET = ""

# Optional values below

# The folder where you want the files uploaded.
# Example: /Public or /
DROPBOX_FILE_UPLOAD_FOLDER = ""
# The value below may be either 'app_folder' or 'dropbox'
DROPBOX_ACCESS_TYPE = ""

The DROPBOX_APP_KEY and DROPBOX_APP_SECRET_KEY are provided to you when you create a new dropbox app. Fetching the access token and access token secret is outside the scope of this blog post but you can follow the Getting Started Guide until the Get an access token section and then paste the access token key and secret in the DROPBOX_APP_ACCESS_TOKEN and DROPBOX_APP_ACCESS_TOKEN_SECRET attributes respectively.

Add the DropboxFileUploadHandler to any app (in my case testapp) and reference it in the FILE_UPLOAD_HANDLERS in settings.py:

FILE_UPLOAD_HANDLERS = (
    "testapp.dropbox_upload_handler.DropboxFileUploadHandler",
)

That's it and you are done!

Note

Since dropbox doesn't support chunked uploads, the file is first uploaded to the temporary file upload directory on the server and then onto dropbox.

Here's how you would handle the file post upload in your view:

def file_upload_handler_view(request):
    if request.method == "POST":
        file_uploaded = request.FILES["name_of_file_input"]
        print file_uploaded.read()
        # Helpful attribute to get dropbox file metadata
        # like path on the server, size, thumbnail etc
        file_uploaded.dropbox_metadata

The DropboxFile returned is an instance of httplib.HTTPResponse and so all file like methods are not defined but some basic methods like read are supported. There is an attribute called dropbox_metadata on the uploaded file that holds the dropbox metadata.


Related Posts


Can we help you build amazing apps? Contact us today.

Comments

Alex J 2nd Aug., 2012

Excellent post! thanks for sharing :)

commmenttor
Allen

Hey, I'm a community blog curator for DZone. I wanted to talk with you about potentially featuring your blog on DZone's content portals. Send me an email at allenc [at] dzone [dot] com and I'll explain the details.

commmenttor
testdomain

Woah! I'm really loving the template/theme of this site. It's simple, yet effective. A lot of times it's very difficult to get that "perfect balance" between superb usability and visual appeal. I must say that you've done a superb job with this. In addition, the blog loads extremely quick for me on Chrome. Outstanding Blog!

commmenttor
Andres O 26th April, 2013

Awesome,, this is really helpfull. So now, after upload files, how can show in a view, what i've upload before?

commmenttor
Sriram Velamur

Hi Thejaswi,

Thanks for the pointers. I am not into Django and I've been using Tornado for a while now and we just finished up on an internal app that interfaces with Dropbox and had issues wrt the RequestTokens being updated every time the authentication url is hit. Did you face any such issues in particular ? Could you throw some more light into that, in another post perhaps ? :)

Sriram Velamur

commmenttor
Post a comment Name :

Email :

Your site url:

Comment :

© Agiliq, 2009-2012