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
hugo boss amazon

Mellemøsten er vidne enorme udvikling i sektoren for affaldshåndtering. Bee? Ah, en af ​​de førende virksomheder i affaldshåndtering vil udvide sine operationer og nå i Kongeriget Saudi-Arabien i et forsøg på at forbedre miljøet. Med stigningen i mængden af ​​affald, har virksomhederne været tvunget til at fremme udvikling og strategier i en effektiv affaldshåndtering.? I en artikel featured på Construction Week Online formanden for Bee'ah, HE Salim Al Owais sagde, "Bee'ah har nået et punkt, hvor det nu er parat til at eksportere og replikere dets affaldshåndtering ekspertise i andre lande i GCC og Saudi miljø marked giver det største potentiale på grund af sin enorme landskabet og dets ligheder med Emiratet Sharjah i form af befolkningstæthed og industriområder ". Affaldsproduktion fra byområder i den mellemøstlige region er mere end 150 millioner tons om året. Fleming Gulf Konferencer gennem sin fjerde udgave af Waste Management Middle East Forum, der afholdes den 11-12 November i Dubai - UAE vil samle affaldshåndtering fagfolk fra Mellemøsten for at diskutere fremgangsmåder til effektivt at håndtere affald. Dubai Kommune Miljøministeriet - Oman, De Forenede Nationers Miljøprogram, GE Power Conversion og Abu Dhabi Fødevareregion vil præsentere på forummet. Emnerne på forummet omfatter samtidige affaldshåndtering udfordringer i Mellemøsten, der beskæftiger sig med konstruktion og nedrivningsaffald og affaldssektoren politik & regelsæt i Abu Dhabi blandt others.Challenges for luftkvalitet management er en af ​​de centrale emner i forum være dækket af Dr.? mer Aga, Assoc. Professor, Leder af Miljøteknologi Institut fra University of Dammam, KSA. Han vil fokusere på emner som sammenhængen mellem luftforurening og risiko for menneskers sundhed, byområder emissioner og dødelighed i byområder byer og forslag forbedre luftkvaliteten i regionen. Dr. Aga modtog sin Ph.D. i Miljøteknologi fra Middle East Technical University, Tyrkiet i 2000. Hans forskning har fokuseret på forvaltning af fast affald, luftkvalitet og forureningsbekæmpelse, vandkvalitet og afsaltning. Hans forskergruppe har udviklet fuldautomatisk regnvand sekventiel sampler og web-baserede luftforurening forudsigelse system.MAG Group International, en paraplyorganisation brand for over 70 russiske selskaber er guld sponsor for forummet. De seneste sponsorer inkluderer Bee? Ah, Enerkem, Keppel Integrated Engineering, Metso og Outotec GmbH Abu Dhabi. Energi fra affald er nøglen til at mætte det voksende energibehov på verdensplan. Med protektion fra Miljøministeriet & Water, UAE for sin tidligere udgave forummet var en stor succes med virksomheder fremvisning af deres teknologier, og lære mere om at tilpasse deres operationer med andre aktører i branchen.

commmenttor
© Agiliq, 2009-2012