dropbox API v2使用python上传大文件

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

我正在尝试通过Dropbox API v2上传大文件(~900MB)但我收到此错误:

requests.exceptions.ConnectionError :('Connection aborted。',ConnectionResetError(104,'peer reset by peer'))

它适用于较小的文件。

我在文档中发现我需要使用files_upload_session_start方法打开上传会话,但我对此命令有错误,我无法进一步使用._append方法。

我怎么解决这个问题?文档中没有信息。我正在使用Python 3.5.1和使用pip安装的最新dropbox模块。

这是我正在运行的代码:

c = Dropbox(access_token)
f = open("D:\\Programs\\ubuntu-13.10-desktop-amd64.iso", "rb")
result = c.files_upload_session_start(f)
f.seek(0, os.SEEK_END)
size = f.tell()
c.files_upload_session_finish(f,     files.UploadSessionCursor(result.session_id, size), files.CommitInfo("/test900.iso"))
python dropbox-api
2个回答
16
投票

对于像这样的大文件,您需要使用上传会话。否则,您将遇到诸如您发布的错误之类的问题。

这使用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, 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()

6
投票

可以使用Dropbox Api v2调用更新@Greg答案:

self.client.files_upload_session_append_v2(
                f.read(self.CHUNK_SIZE), cursor)
cursor.offset = f.tell()
© www.soinside.com 2019 - 2024. All rights reserved.