从python脚本上传文件到我的Dropbox

Chr*_*ina 47 python upload dropbox

我想自动将我的python脚本中的文件上传到我的Dropbox帐户.我无论如何只能通过用户/传递来找到这个.我在Dropbox SDK中看到的所有内容都与具有用户互动的应用相关.我只是想做这样的事情:

https://api-content.dropbox.com/1/files_put/ /?user = me&pass = blah

Spa*_*ine 54

@Christina的答案基于Dropbox APP v1,现已弃用,将于2017年6月28 日关闭.(有关更多信息,请参阅此处.)

APP v2于2015年11月推出,更简单,更一致,更全面.

这是APP v2的源代码.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import dropbox

class TransferData:
    def __init__(self, access_token):
        self.access_token = access_token

    def upload_file(self, file_from, file_to):
        """upload a file to Dropbox using API v2
        """
        dbx = dropbox.Dropbox(self.access_token)

        with open(file_from, 'rb') as f:
            dbx.files_upload(f.read(), file_to)

def main():
    access_token = '******'
    transferData = TransferData(access_token)

    file_from = 'test.txt'
    file_to = '/test_dropbox/test.txt'  # The full path to upload the file to, including the file name

    # API v2
    transferData.upload_file(file_from, file_to)

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

源代码托管在GitHub上,在这里.

  • 我能让它工作的唯一方法是将其更改为:dbx.files_upload(f.read(),file_to) (3认同)

Chr*_*ina 53

感谢@smarx的上述答案!我只想澄清其他任何试图这样做的人.

  1. 当然首先要确保安装Dropbox模块pip install dropbox.

  2. 在"应用控制台"中的自己的保管箱帐户下创建应用.(https://www.dropbox.com/developers/apps)

  3. 只是为了记录我用以下内容创建了我的应用程序:

    一个.应用类型为"Dropbox API APP".

    湾 数据访问类型为"文件和数据存储"

    C.文件夹访问为"我的应用程序需要访问Dropbox上已有的文件".(即:权限类型为"Full Dropbox".)

  4. 然后单击"生成访问令牌"按钮并剪切/粘贴到下面的python示例中代替<auth_token>:

import dropbox client = dropbox.client.DropboxClient(<auth_token>) print 'linked account: ', client.account_info() f = open('working-draft.txt', 'rb') response = client.put_file('/magnum-opus.txt', f) print 'uploaded: ', response folder_metadata = client.metadata('/') print 'metadata: ', folder_metadata f, metadata = client.get_file_and_metadata('/magnum-opus.txt') out = open('magnum-opus.txt', 'wb') out.write(f.read()) out.close() print metadata
Run Code Online (Sandbox Code Playgroud)


Ste*_*ood 12

这是我使用API​​ v2(和Python 3)的方法.我想上传文件并为其创建共享链接,我可以通过电子邮件发送给用户.它基于sparkandshine的例子.注意我认为当前的API文档有一个小错误,sparkandshine已经纠正.

import pathlib
import dropbox
import re

# the source file
folder = pathlib.Path(".")    # located in this folder
filename = "test.txt"         # file name
filepath = folder / filename  # path object, defining the file

# target location in Dropbox
target = "/Temp/"              # the target folder
targetfile = target + filename   # the target path and file name

# Create a dropbox object using an API v2 key
d = dropbox.Dropbox(your_api_access_token)

# open the file and upload it
with filepath.open("rb") as f:
   # upload gives you metadata about the file
   # we want to overwite any previous version of the file
   meta = d.files_upload(f.read(), targetfile, mode=dropbox.files.WriteMode("overwrite"))

# create a shared link
link = d.sharing_create_shared_link(targetfile)

# url which can be shared
url = link.url

# link which directly downloads by replacing ?dl=0 with ?dl=1
dl_url = re.sub(r"\?dl\=0", "?dl=1", url)
print (dl_url)
Run Code Online (Sandbox Code Playgroud)


小智 9

import dropbox
access_token = '************************'
file_from = 'index.jpeg'  //local file path
file_to = '/Siva/index.jpeg'      // dropbox path
def upload_file(file_from, file_to):
    dbx = dropbox.Dropbox(access_token)
    f = open(file_from, 'rb')
    dbx.files_upload(f.read(), file_to)
upload_file(file_from,file_to)
Run Code Online (Sandbox Code Playgroud)


use*_*559 8

验证对Dropbox API的调用的唯一方法是使用OAuth,这涉及用户向您的应用授予权限。我们不允许第三方应用处理用户凭据(用户名和密码)。

如果这仅用于您的帐户,请注意,您可以轻松地为自己的帐户获取OAuth令牌,然后使用它即可。请参阅https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account

如果这是给其他用户的,则他们需要通过浏览器对您的应用进行一次授权才能获得OAuth令牌。但是,一旦有了令牌,就可以继续使用它,因此每个用户只需要这样做一次。