将文件上传到保管箱时的进度条

Progress bar while uploading a file to dropbox

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

我想上传一个大文件到保管箱。如何查看进度? [Docs]

编辑: 上传者偏移量在下面以某种方式相同。我做错了什么

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

编辑 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")

upload_chunked,如 the documentation 注释:

Uploads data from this ChunkedUploader's file_obj in chunks, until an error occurs. Throws an exception when an error occurs, and can be called again to resume the upload.

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

如果你想自己一次上传一个块,你应该使用upload_chunk and commit_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.')