def putFile(self, key, filePath, progressCallback=None, acl=None, metadata={}, mimetype=None, transformer=None, headers={}, args=None, subresource=None): ''' filePath 本地文件路径 progressCallback 上传文件进度回调方法 _callback(self._total, len(data), *self._args) ''' with open(filePath, 'rb') as fp: headers["s-sina-sha1"] = aws_md5(fp) from email.utils import formatdate from calendar import timegm expireDate = expire2datetime(datetime.timedelta(minutes=60 * 24)) expireDate = formatdate(timegm(expireDate.timetuple()), usegmt=True) headers['Date'] = expireDate # with open(filePath, 'rb') as fp: # fileWithCallback = FileWithCallback(fp, progressCallback) # return self.put(key, fileWithCallback, acl, metadata, mimetype, transformer, headers, args, subresource) # fileWithCallback = FileWithCallback(filePath, 'rb', progressCallback) with FileWithCallback(filePath, 'rb', progressCallback) as fileWithCallback: return self.put(key, fileWithCallback, acl, metadata, mimetype, transformer, headers, args, subresource)
def _upload(retries_left=amount_of_retries): try: bucket = SCSBucket(bucket_name) headers={"Content-Length":str(fileChunkWithCallback.bytes)} with FileChunkIO(fileChunkWithCallback.name, 'rb', offset=fileChunkWithCallback.offset, bytes=fileChunkWithCallback.bytes) as fpForMd5: headers["s-sina-sha1"] = aws_md5(fpForMd5) scsResponse = bucket.put(key_name, fileChunkWithCallback, headers=headers, args={'partNumber':'%i'%part.part_num, 'uploadId':upload_id}) part.etag = scsResponse.urllib2Response.info()['ETag'] part.response = scsResponse if num_cb:num_cb(upload_id, parts_amount, part) return part except Exception as exc: raise exc if retries_left: return _upload(retries_left=retries_left - 1) else: # print 'Failed uploading part #%d' % part.part_num six.print_('Failed uploading part #%d' % part.part_num) # print exc six.print_(exc) raise exc else: # print '... Uploaded part #%d' % part.part_num six.print_('... Uploaded part #%d' % part.part_num)
def _upload(retries_left=amount_of_retries): try: bucket = SCSBucket(bucket_name) headers = {"Content-Length": str(fileChunkWithCallback.bytes)} with FileChunkIO(fileChunkWithCallback.name, 'rb', offset=fileChunkWithCallback.offset, bytes=fileChunkWithCallback.bytes) as fpForMd5: headers["s-sina-sha1"] = aws_md5(fpForMd5) scsResponse = bucket.put(key_name, fileChunkWithCallback, headers=headers, args={ 'partNumber': '%i' % part.part_num, 'uploadId': upload_id }) part.etag = scsResponse.urllib2Response.info()['ETag'] part.response = scsResponse if num_cb: num_cb(upload_id, parts_amount, part) return part except Exception as exc: # raise exc if retries_left: return _upload(retries_left=retries_left - 1) else: # print 'Failed uploading part #%d' % part.part_num six.print_('Failed uploading part #%d' % part.part_num) # print exc six.print_(exc) raise exc else: # print '... Uploaded part #%d' % part.part_num six.print_('... Uploaded part #%d' % part.part_num)
def _upload(retries_left=amount_of_retries): try: if debug == 1: # print 'Start uploading part #%d ...' % part.part_num six.print_('Start uploading part #%d ...' % part.part_num) bucket = SCSBucket(bucket_name) with FileChunkWithCallback(source_path, 'rb', offset=offset, bytes=chunk_bytes, cb=cb, upload_id=upload_id, part_num=part.part_num) as fp: headers={"Content-Length":str(chunk_bytes)} with FileChunkIO(source_path, 'rb', offset=offset, bytes=chunk_bytes) as fpForMd5: headers["s-sina-sha1"] = aws_md5(fpForMd5) scsResponse = bucket.put(key_name, fp, headers=headers, args={'partNumber':'%i'%part.part_num, 'uploadId':upload_id}) part.etag = scsResponse.urllib2Response.info()['ETag'] if num_cb:num_cb(upload_id, parts_amount, part) return part except Exception as exc: raise exc if retries_left: return _upload(retries_left=retries_left - 1) else: # print 'Failed uploading part #%d' % part.part_num six.print_('Failed uploading part #%d' % part.part_num) # print exc six.print_(exc) raise exc else: if debug == 1: # print '... Uploaded part #%d' % part.part_num six.print_('... Uploaded part #%d' % part.part_num)
def _upload(retries_left=amount_of_retries): try: if debug == 1: # print 'Start uploading part #%d ...' % part.part_num six.print_('Start uploading part #%d ...' % part.part_num) bucket = SCSBucket(bucket_name) with FileChunkWithCallback(source_path, 'rb', offset=offset, bytes=chunk_bytes, cb=cb, upload_id=upload_id, part_num=part.part_num) as fp: headers = {"Content-Length": str(chunk_bytes)} with FileChunkIO(source_path, 'rb', offset=offset, bytes=chunk_bytes) as fpForMd5: headers["s-sina-sha1"] = aws_md5(fpForMd5) scsResponse = bucket.put(key_name, fp, headers=headers, args={ 'partNumber': '%i' % part.part_num, 'uploadId': upload_id }) part.etag = scsResponse.urllib2Response.info()['ETag'] if num_cb: num_cb(upload_id, parts_amount, part) return part except Exception as exc: raise exc if retries_left: return _upload(retries_left=retries_left - 1) else: # print 'Failed uploading part #%d' % part.part_num six.print_('Failed uploading part #%d' % part.part_num) # print exc six.print_(exc) raise exc else: if debug == 1: # print '... Uploaded part #%d' % part.part_num six.print_('... Uploaded part #%d' % part.part_num)
def putFile(self, key, filePath, progressCallback=None, acl=None, metadata={}, mimetype=None, transformer=None, headers={}, args=None, subresource=None): ''' filePath 本地文件路径 progressCallback 上传文件进度回调方法 _callback(self._total, len(data), *self._args) ''' f = file(filePath, 'rb') headers["s-sina-sha1"] = aws_md5(f) f.close() from email.utils import formatdate from calendar import timegm expireDate = expire2datetime(datetime.timedelta(minutes=60*24)) expireDate = formatdate(timegm(expireDate.timetuple()), usegmt=True) headers['Date'] = expireDate fileWithCallback = FileWithCallback(filePath, 'rb', progressCallback) return self.put(key, fileWithCallback, acl, metadata, mimetype, transformer, headers, args, subresource)
def complete_multipart_upload(self, multipart): jsonArray = [] for part in multipart.parts: jsonDict = {} jsonDict['PartNumber']=part.part_num jsonDict['ETag']=part.etag jsonArray.append(jsonDict) data = json.dumps(jsonArray) headers = {} headers["Content-Type"] = guess_mimetype(multipart.key_name) headers["Content-Length"] = str(len(data)) if "s-sina-sha1" not in headers: headers["s-sina-sha1"] = aws_md5(data) scsreq = self.request(method="POST", data=data, headers=headers, key=multipart.key_name, args={'uploadId':multipart.upload_id}) scsResponse = self.send(scsreq) return scsResponse.close()