将文件上传到Dropbox时的进度条

Abh*_*tia 10 python dropbox python-2.7 dropbox-api progress-bar

import dropbox
client = dropbox.client.DropboxClient('<token>')
f = open('/ssd-scratch/abhishekb/try/1.mat', 'rb')
response = client.put_file('/data/1.mat', f)
Run Code Online (Sandbox Code Playgroud)

我想将一个大文件上传到dropbox.我该如何检查进度?[文件]

编辑:以上的某种情况下,上传者也是如此.我究竟做错了什么

import os,pdb,dropbox
size=1194304
client = dropbox.client.DropboxClient(token)
path='D:/bci_code/datasets/1.mat'

tot_size = os.path.getsize(path)
bigFile = open(path, 'rb')

uploader = client.get_chunked_uploader(bigFile, size)
print "uploading: ", tot_size
while uploader.offset < tot_size:
    try:
        upload = uploader.upload_chunked()
        print uploader.offset
    except rest.ErrorResponse, e:
        print("something went wrong")
Run Code Online (Sandbox Code Playgroud)

编辑2:

size=1194304
tot_size = os.path.getsize(path)
bigFile = open(path, 'rb')

uploader = client.get_chunked_uploader(bigFile, tot_size)
print "uploading: ", tot_size
while uploader.offset < tot_size:
    try:
        upload = uploader.upload_chunked(chunk_size=size)
        print uploader.offset
    except rest.ErrorResponse, e:
        print("something went wrong")
Run Code Online (Sandbox Code Playgroud)

use*_*559 12

upload_chunked,如文档说明:

从该上载数据ChunkedUploaderfile_obj以块的形式,直到发生错误.发生错误时引发异常,可以再次调用以恢复上载.

所以是的,它会在返回之前上传整个文件(除非发生错误).

如果你想自己一次上传一个块,你应该使用upload_chunkcommit_chunked_upload.

这里有一些工作代码,向您展示如何一次上传一个块并在块之间打印进度:

from io import BytesIO
import os

from dropbox.client import DropboxClient

client = DropboxClient(ACCESS_TOKEN)

path = 'test.data'
chunk_size = 1024*1024 # 1MB

total_size = os.path.getsize(path)
upload_id = None
offset = 0
with open(path, 'rb') as f:
    while offset < total_size:
        offset, upload_id = client.upload_chunk(
            BytesIO(f.read(chunk_size)),
            offset=offset, upload_id=upload_id)

        print('Uploaded so far: {} bytes'.format(offset))

# Note the "auto/" on the next line, which is needed because
# this method doesn't attach the root by itself.
client.commit_chunked_upload('auto/test.data', upload_id)
print('Upload complete.')
Run Code Online (Sandbox Code Playgroud)