Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

I want to upload a big file to dropbox. How can I check the progress? [Docs]

EDIT: The uploader offeset is same below somehow. What am I doing wrong

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

EDIT 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")
like image 238
Abhishek Bhatia Avatar asked Nov 27 '15 13:11

Abhishek Bhatia


1 Answers

upload_chunked, as the documentation notes:

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.

So yes, it uploads the entire file (unless an error occurs) before returning.

If you want to upload a chunk at a time on your own, you should use upload_chunk and commit_chunked_upload.

Here's some working code that shows you how to upload a single chunk at a time and print progress in between chunks:

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.')
like image 64
user94559 Avatar answered Oct 16 '22 13:10

user94559