我正在使用Dropbox API为python Dropbox应用制作进度条。问题是我不知道如何获得到目前为止写入的字节数,以便可以构建它。有什么办法可以使用python Dropbox API做到这一点?如果不能,我是否可以测量可能是使用os模块从系统发出的字节?

我正在尝试使其与get_chunked_loader一起使用,但是如果我可以获取为put_file()以及file_copy()和file_move Also()编写的字节,那就太好了。到目前为止,我的代码是这样的:

if (file_size >= 4194304):
    big_file = open(path_to_file, 'rb')
    uploader = client.get_chunked_uploader(big_file)
    print "uploading " + path_to_file
    while uploader.offset < file_size:
        percent_complete = bytes_written / file_size * 100
        clearscreen()
        print "%.2f" % percent_complete + "%"


谢谢!

最佳答案

我创建块上传

f = open(filetoupload, 'rb')
uploader = MMChunkedUploader(self.client, f, file_size, 1024*200)
uploader.upload_chunked()
uploader.finish(dropboxfilename)


class MMChunkedUploader(object):
  """Contains the logic around a chunked upload, which uploads a
   large file to Dropbox via the /chunked_upload endpoint.
   """

  def __init__(self, client, file_obj, length, chunk_size = 4 * 1024 * 1024):
    self.client = client
    self.offset = 0
    self.upload_id = None

    self.last_block = None
    self.file_obj = file_obj
    self.target_length = length
    self.chunk_size=chunk_size
    self.clocknumber=0
    dec=float(self.target_length)/chunk_size - self.target_length//chunk_size
    if dec >0:
      self.totalblock=self.target_length/chunk_size +1
    else:
      self.totalblock=self.target_length/chunk_size
def upload_chunked(self, chunk_size = 0):
    """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.

    Parameters
        chunk_size
          The number of bytes to put in each chunk. (Default 4 MB.)
    """
    if chunk_size ==0:
        chunk_size=self.chunk_size

    self.clocknumber=0
    while self.offset < self.target_length:
        self.clocknumber+=1
        print "Block n.", repr(self.clocknumber) , " of " , repr(self.totalblock), " %",  round((float(self.clocknumber)  * 100) / self.totalblock, 0)

        next_chunk_size = min(chunk_size, self.target_length - self.offset)  #sceglie tra min e chuck size
        if self.last_block == None:
            self.last_block = self.file_obj.read(next_chunk_size)
            print "Leggo blocco file"

        try:
            (self.offset, self.upload_id) = self.client.upload_chunk(
                StringIO(self.last_block), next_chunk_size, self.offset, self.upload_id)
            self.last_block = None
        except dropbox.rest.ErrorResponse as e:
            # Handle the case where the server tells us our offset is wrong.
            must_reraise = True
            if e.status == 400:
                reply = e.body
                if "offset" in reply and reply['offset'] != 0 and reply['offset'] > self.offset:
                    self.last_block = None
                    self.offset = reply['offset']
                    must_reraise = False
            if must_reraise:
                raise

def finish(self, path, overwrite=False, parent_rev=None):


        path = "/commit_chunked_upload/%s%s" % (self.client.session.root, dropbox.client.format_path(path))

        params = dict(
            overwrite = bool(overwrite),
            upload_id = self.upload_id
        )

        if parent_rev is not None:
            params['parent_rev'] = parent_rev

        url, params, headers = self.client.request(path, params, content_server=True)

        return self.client.rest_client.POST(url, params, headers)

关于python - Python Dropbox Api的进度栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23816342/

10-12 17:45
查看更多