async def upload_part_copy(self, source_bucket_name, source_key, byte_range, target_key, target_upload_id, target_part_number, headers=None): """分片拷贝。把一个已有文件的一部分或整体拷贝成目标文件的一个分片。 :param byte_range: 指定待拷贝内容在源文件里的范围。参见 :ref:`byte_range` :param headers: HTTP头部 :type headers: 可以是dict,建议是oss2.CaseInsensitiveDict :return: :class:`PutObjectResult <oss2.models.PutObjectResult>` """ headers = http.CaseInsensitiveDict(headers) headers[ 'x-oss-copy-source'] = '/' + source_bucket_name + '/' + source_key range_string = _make_range_string(byte_range) if range_string: headers['x-oss-copy-source-range'] = range_string resp = await self.__do_object('PUT', target_key, params={ 'uploadId': target_upload_id, 'partNumber': str(target_part_number) }, headers=headers) return models.PutObjectResult(resp)
async def complete_multipart_upload(self, key, upload_id, parts, headers=None): """完成分片上传,创建文件。 :param str key: 待上传的文件名,这个文件名要和 :func:`init_multipart_upload` 的文件名一致。 :param str upload_id: 分片上传ID :param parts: PartInfo列表。PartInfo中的part_number和etag是必填项。其中的etag可以从 :func:`upload_part` 的返回值中得到。 :type parts: list of `PartInfo <oss2.models.PartInfo>` :param headers: HTTP头部 :type headers: 可以是dict,建议是oss2.CaseInsensitiveDict :return: :class:`PutObjectResult <oss2.models.PutObjectResult>` """ data = xml_utils.to_complete_upload_request( sorted(parts, key=lambda p: p.part_number)) resp = await self.__do_object('POST', key, params={'uploadId': upload_id}, data=data, headers=headers) return models.PutObjectResult(resp)
async def upload_part(self, key, upload_id, part_number, data, progress_callback=None, headers=None): """上传一个分片。 :param str key: 待上传文件名,这个文件名要和 :func:`init_multipart_upload` 的文件名一致。 :param str upload_id: 分片上传ID :param int part_number: 分片号,最小值是1. :param data: 待上传数据。 :param progress_callback: 用户指定进度回调函数。可以用来实现进度条等功能。参考 :ref:`progress_callback` 。 :param headers: 用户指定的HTTP头部。可以指定Content-MD5头部等 :type headers: 可以是dict,建议是oss2.CaseInsensitiveDict :return: :class:`PutObjectResult <oss2.models.PutObjectResult>` """ if progress_callback: data = utils.make_progress_adapter(data, progress_callback) if self.enable_crc: data = utils.make_crc_adapter(data) resp = await self.__do_object('PUT', key, params={ 'uploadId': upload_id, 'partNumber': str(part_number) }, headers=headers, data=data) result = models.PutObjectResult(resp) if self.enable_crc and result.crc is not None: utils.check_crc('put', data.crc, result.crc) return result
async def put_object(self, key, data, headers=None, progress_callback=None): """上传一个普通文件。 用法 :: >>> bucket.put_object('readme.txt', 'content of readme.txt') >>> with open(u'local_file.txt', 'rb') as f: >>> bucket.put_object('remote_file.txt', f) :param key: 上传到OSS的文件名 :param data: 待上传的内容。 :type data: bytes,str或file-like object :param headers: 用户指定的HTTP头部。可以指定Content-Type、Content-MD5、x-oss-meta-开头的头部等 :type headers: 可以是dict,建议是oss2.CaseInsensitiveDict :param progress_callback: 用户指定的进度回调函数。可以用来实现进度条等功能。参考 :ref:`progress_callback` 。 :return: :class:`PutObjectResult <oss2.models.PutObjectResult>` """ headers = utils.set_content_type(http.CaseInsensitiveDict(headers), key) if progress_callback: data = utils.make_progress_adapter(data, progress_callback) if self.enable_crc: data = utils.make_crc_adapter(data) resp = await self.__do_object('PUT', key, data=data, headers=headers) result = models.PutObjectResult(resp) if self.enable_crc and result.crc is not None: utils.check_crc('put', data.crc, result.crc) return result
async def copy_object(self, source_bucket_name, source_key, target_key, headers=None): """拷贝一个文件到当前Bucket。 :param str source_bucket_name: 源Bucket名 :param str source_key: 源文件名 :param str target_key: 目标文件名 :param headers: HTTP头部 :type headers: 可以是dict,建议是oss2.CaseInsensitiveDict :return: :class:`PutObjectResult <oss2.models.PutObjectResult>` """ headers = http.CaseInsensitiveDict(headers) headers[ 'x-oss-copy-source'] = '/' + source_bucket_name + '/' + urlquote( source_key, '') resp = await self.__do_object('PUT', target_key, headers=headers) return models.PutObjectResult(resp)