Search code examples
pythondjangodownloaddropboxdropbox-api

Is there a way to download a file using the Dropbox API without using web server bandwidth?


I'm new to programming in python/Django and the Dropbox-API. I searched and searched but haven't been able to find the answer.

I am using Python-Django and have my Dropbox application authorized to access a Dropbox user's account. I have the tokens I need to access an can run a python script to download the target file, but it ONLY saves it to the web server I'm running it on.

I'm trying to get it so that the file will download directly to the user through the web browser, instead of using my web server to connect to Dropbox and get the file.

Is there a way to download a file using the Dropbox API without using web server bandwidth? I think i'm supposed to use the Dropbox REST API, but I can't figure out how to create the web URL properly so that it authorized to get the file. I have the user's public/private keys for my app, but don't know how to put generate the URL to make it use it.

Any advice or link to information I can learn from to do this would be appreciated.

import re
import base64
from datetime import datetime, timedelta
from dateutil import parser
from django.contrib import admin
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from dropbox import client, rest, session

dropboxPath = '/Photos/Kids.jpg'
localPath = '~/Downloads' # This path is generated through a jQuery Dialog and stored in session variable

def getAPI(userPublicKey, userPrivateKey):
    connection = session.DropboxSession(AppPublicKey, AppPrivateKey, AccessType)
    connection.set_token(userPublicKey, userPrivateKey)
    dropbox = client.DropboxClient(connection)
    api = dropbox
    return api


def fileDownload(request, api, dropboxPath, localPath):
    # Connect to dropbox with user keys
    api = getAPI(userPublicKey, userPrivateKey)

    # Download target file
    path_source = dropboxPath
    path_target = localPath

    # ISSUE SECTION
    # --------------------
    # This section will download the file from the user's dropbox,
    # but it downloads to my web server first (memory or file).
    # I want this to go directly to the user via browser download vs. going through
    # my server.

    f, metadata = api.get_file_and_metadata(path_source)
    out = open(path_target, 'w')
    out.write(f.read())
    out.close()
    print metadata

    return HttpResponseRedirect('/download/complete')

Solution

  • You could get a direct URL to the file content via the /media endpoint and redirect the user to that. This way the user would be downloading the file directly from Dropbox's servers.

    In Python, this would look something like this:

    return HttpResponseRedirect(api.media(path_source)['url'])