从python scrip上传文件到我的dropbox

2024-06-17 08:04:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从python脚本自动上传一个文件到dropbox帐户。我找不到只使用用户/通行证的方法。我在Dropbox SDK中看到的一切都与一个具有用户交互的应用程序有关。我只想做这样的事:

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


Tags: 文件方法用户https脚本comapi应用程序
3条回答

@Christina的答案基于DropboxAPP v1,该选项现在已弃用,将于2017年6月28日关闭。(有关详细信息,请参阅here。)

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()

源代码托管在GitHub,here上。

下面是我使用API v2(和Python 3)的方法。我想上传一个文件并为它创建一个共享链接,我可以通过电子邮件发送给用户。这是基于斯巴康德的例子。注意我认为当前的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)

重要提示:由于dropbox现在使用v2 API,因此不推荐使用此答案。
关于当前的API版本解决方案,请参见@SparkAndShine的答案

感谢@smarx给出上述答案!我只是想为其他人澄清一下。

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

  2. 在“应用程序控制台”中,在自己的dropbox帐户下创建一个应用程序。(https://www.dropbox.com/developers/apps

  3. 作为记录,我创建的应用程序如下:

    a.应用类型为“Dropbox API应用”。

    b.数据访问类型为“文件和数据存储”

    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

相关问题 更多 >