コード例 #1
0
 def upload(self, fp, size=None, progress_callback=None):
     """
     Upload file contents.
     fp can be any file-like object, but if you don't specify it's size in advance it must support tell and seek methods.
     Progress callback is optional - if provided, it should match signature of ProgressCallbacks.upload_progress
     """
     if isinstance(fp, six.binary_type):
         fp = six.BytesIO(fp)
     if size is None:
         size = base.get_file_size(fp)
     if size < self._upload_chunk_size:
         # simple, one request upload
         retries = max(self._upload_retries, 1)
         while retries > 0:
             url = self._client.get_url(self._url_template_content,
                                        path=self.path)
             chunk = base._FileChunk(fp, 0, size)
             r = self._client.POST(url,
                                   data=chunk,
                                   headers={'Content-length': str(size)})
             exc.default.check_response(r)
             server_sha = r.headers['X-Sha512-Checksum']
             our_sha = chunk.sha.hexdigest()
             if server_sha == our_sha:
                 break
             retries -= 1
             # TODO: retry network errors too
         if retries == 0:
             raise exc.ChecksumError("Failed to upload file", {})
     else:  # chunked upload
         return self._chunked_upload(fp, size, progress_callback)
コード例 #2
0
ファイル: resources.py プロジェクト: ajassal/python-egnyte
 def upload(self, fp, threads=2, chunksize_mb=100, size=None, progress_callback=None):
     """
     Upload file contents.
     fp can be any file-like object, but if you don't specify it's size in advance it must support tell and seek methods.
     Progress callback is optional - if provided, it should match signature of ProgressCallbacks.upload_progress
     """
     if isinstance(fp, six.binary_type):
         fp = six.BytesIO(fp)
     if size is None:
         size = base.get_file_size(fp)
     if size < self._upload_chunk_threshold:
         # simple, one request upload
         retries = max(self._upload_retries, 1)
         while retries > 0:
             url = self._client.get_url(self._url_template_content, path=self.path)
             chunk = base._FileChunk(fp, 0, size)
             try:
                 r = self._client.POST(url, data=chunk, headers={'Content-length': size})
                 exc.default.check_response(r)
                 server_sha = r.headers['X-Sha512-Checksum']
                 our_sha = chunk.sha.hexdigest()
                 if server_sha == our_sha:
                     break
             except requests.ConnectionError as e:
                 print('retrying on error: %s' % e)
             retries -= 1
             # TODO: retry network errors too
         if retries == 0:
             raise exc.ChecksumError("Failed to upload file", {})
     else:  # chunked upload
         return self._chunked_upload(fp, size, threads, chunksize_mb, progress_callback)