Dropbox Python API:上传文件

问题描述 投票:0回答:4

[编辑]

此问题已得到解答,我不接受任何新答案。

[编辑结束]

注意:在不同的编程语言中都存在这样的问题,但它们没有专门解决Python的

dropbox
库(或者至少我找不到任何这样做的库),这就是我创建这个的原因问题。

我想知道如何使用 Python 2.7 中的

dropbox
库将文件上传到我的 Dropbox 并读回文件。

我已成功连接到我的 Dropbox,Dropbox 对象称为 db。

如果有人知道如何做到这一点,请写一个包含方法调用和参数的答案,或者如果这是重复的问题,请使用链接进行评论。

提前谢谢您!

python python-2.7 web dropbox dropbox-api
4个回答
1
投票

Dropbox Python SDK同时提供 API v1 和 API v2 功能以实现向后兼容性,但现在只应使用 API v2,因为 API v1 已已弃用教程涵盖了使用 API v2 功能的基础知识。



这使用 Dropbox Python SDK 从远程路径

/Homework/math/Prime_Numbers.txt
的 Dropbox API 下载文件到本地文件
Prime_Numbers.txt
:

import dropbox
dbx = dropbox.Dropbox("<ACCESS_TOKEN>")

with open("Prime_Numbers.txt", "wb") as f:
    metadata, res = dbx.files_download(path="/Homework/math/Prime_Numbers.txt")
    f.write(res.content)

<ACCESS_TOKEN>
应替换为您的访问令牌。



上传:

这使用 Dropbox Python SDK 将文件从

file_path
指定的本地文件上传到 Dropbox API 到
dest_path
指定的远程路径。它还根据文件的大小选择是否使用上传会话:

f = open(file_path)
file_size = os.path.getsize(file_path)

CHUNK_SIZE = 4 * 1024 * 1024

if file_size <= CHUNK_SIZE:

    print dbx.files_upload(f.read(), dest_path)

else:

    upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))
    cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id,
                                               offset=f.tell())
    commit = dropbox.files.CommitInfo(path=dest_path)

    while f.tell() < file_size:
        if ((file_size - f.tell()) <= CHUNK_SIZE):
            print dbx.files_upload_session_finish(f.read(CHUNK_SIZE),
                                            cursor,
                                            commit)
        else:
            dbx.files_upload_session_append(f.read(CHUNK_SIZE),
                                            cursor.session_id,
                                            cursor.offset)
            cursor.offset = f.tell()

f.close()

0
投票

注意

这是 API v1,现已弃用。请谨慎使用或使用当前支持的 API。

初始化 Dropbox 客户端

import dropbox
access_token = 'SOME_ACCESS_TOKEN'
client = dropbox.client.DropboxClient(access_token)

上传文件

src_file = open('SOME_LOCAL_FILE', 'r')
response = client.put_file('SOME_REMOTE_FILE', src_file)

下载文件

dest_file = open('SOME_LOCAL_FILE', 'w')
with client.get_file('SOME_REMOTE_FILE') as src_file:
    dest_file.write(src_file.read())

参考

更简洁的API文档请参考Python核心API文档


0
投票
f = open(file_path)
file_size = os.path.getsize(file_path)
CHUNK_SIZE = 4 * 1024 * 1024

if file_size <= CHUNK_SIZE:
    print dbx.files_upload(f, dest_path)
else:
    upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))

    cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id, offset=f.tell())
    commit = dropbox.files.CommitInfo(path=dest_path)

    while f.tell() < file_size:
        commit = dropbox.files.CommitInfo(path=dest_path)    
        if ((file_size - f.tell()) <= CHUNK_SIZE):
            print dbx.files_upload_session_finish(f.read(CHUNK_SIZE),                               
                                                          cursor,
                                                          commit)
        else:
            dbx.files_upload_session_append(f.read(CHUNK_SIZE),cursor.session_id, cursor.offset)
           cursor.offset = f.tell()

© www.soinside.com 2019 - 2024. All rights reserved.