2

Python/Django と Dropbox-API でのプログラミングは初めてです。探しても探しても答えは出ませんでした。

Python-Django を使用しており、Dropbox アプリケーションが Dropbox ユーザーのアカウントへのアクセスを承認されています。アクセスに必要なトークンがあり、python スクリプトを実行してターゲット ファイルをダウンロードできますが、実行中の Web サーバーにのみ保存されます。

Web サーバーを使用して Dropbox に接続してファイルを取得するのではなく、ファイルが Web ブラウザーを介してユーザーに直接ダウンロードされるように取得しようとしています。

Web サーバーの帯域幅を使用せずに Dropbox API を使用してファイルをダウンロードする方法はありますか? Dropbox REST API を使用することになっていると思いますが、ファイルの取得を承認するために Web URL を適切に作成する方法がわかりません。アプリのユーザーの公開/秘密キーを持っていますが、URL を生成して使用する方法がわかりません。

これを行うために私が学べるアドバイスや情報へのリンクをいただければ幸いです。

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')
4

2 に答える 2

2

/mediaエンドポイント経由でファイル コンテンツへの直接 URL を取得し、ユーザーをそこにリダイレクトできます。この方法では、ユーザーはファイルを Dropbox のサーバーから直接ダウンロードします。

Python では、これは次のようになります。

return HttpResponseRedirect(api.media(path_source)['url'])
于 2013-09-21T17:21:32.587 に答える