Example #1
0
    def create_multipart_upload(self, Bucket, Key, **kwargs):
        """创建分块上传,适用于大文件上传

        :param Bucket(string): 存储桶名称.
        :param Key(string): COS路径.
        :param kwargs(dict): 设置请求headers.
        :return(dict): 初始化分块上传返回的结果,包含UploadId等信息.

        .. code-block:: python

            config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
            client = CosS3Client(config)
            # 创建分块上传
            response = client.create_multipart_upload(
                Bucket='bucket',
                Key='test.txt'
            )
        """
        headers = mapped(kwargs)
        params = {'uploads': ''}
        params = format_values(params)
        url = self._conf.uri(bucket=Bucket, path=Key)
        rt = self.send_request(method='POST',
                               url=url,
                               bucket=Bucket,
                               auth=CosS3Auth(self._conf, Key, params=params),
                               headers=headers,
                               params=params)

        data = xml_to_dict(rt.content)
        return data
Example #2
0
    def create_bucket(self, Bucket, **kwargs):
        """创建一个bucket

        :param Bucket(string): 存储桶名称.
        :param kwargs(dict): 设置请求headers.
        :return: None.

        .. code-block:: python

            config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
            client = CosS3Client(config)
            # 创建bucket
            response = client.create_bucket(
                Bucket='bucket'
            )
        """
        headers = mapped(kwargs)
        url = self._conf.uri(bucket=Bucket)
        rt = self.send_request(
            method='PUT',
            url=url,
            bucket=Bucket,
            auth=CosS3Auth(self._conf),
            headers=headers)
        return None
Example #3
0
    def upload_part(self,
                    Bucket,
                    Key,
                    Body,
                    PartNumber,
                    UploadId,
                    EnableMD5=False,
                    **kwargs):
        """上传分块,单个大小不得超过5GB

        :param Bucket(string): 存储桶名称.
        :param Key(string): COS路径.
        :param Body(file|string): 上传分块的内容,可以为文件流或者字节流.
        :param PartNumber(int): 上传分块的编号.
        :param UploadId(string): 分块上传创建的UploadId.
        :param kwargs(dict): 设置请求headers.
        :param EnableMD5(bool): 是否需要SDK计算Content-MD5,打开此开关会增加上传耗时.
        :return(dict): 上传成功返回的结果,包含单个分块ETag等信息.

        .. code-block:: python

            config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
            client = CosS3Client(config)
            # 分块上传
            with open('test.txt', 'rb') as fp:
                data = fp.read(1024*1024)
                response = client.upload_part(
                    Bucket='bucket',
                    Body=data,
                    Key='test.txt'
                )
        """
        check_object_content_length(Body)
        headers = mapped(kwargs)
        params = {'partNumber': PartNumber, 'uploadId': UploadId}
        params = format_values(params)
        url = self._conf.uri(bucket=Bucket, path=Key)
        if EnableMD5:
            md5_str = get_content_md5(Body)
            if md5_str:
                headers['Content-MD5'] = md5_str
        rt = self.send_request(method='PUT',
                               url=url,
                               bucket=Bucket,
                               headers=headers,
                               params=params,
                               auth=CosS3Auth(self._conf, Key, params=params),
                               data=Body)
        response = dict()
        response['ETag'] = rt.headers['ETag']
        return response
Example #4
0
    def complete_multipart_upload(self,
                                  Bucket,
                                  Key,
                                  UploadId,
                                  MultipartUpload={},
                                  **kwargs):
        """完成分片上传,除最后一块分块块大小必须大于等于1MB,否则会返回错误.

        :param Bucket(string): 存储桶名称.
        :param Key(string): COS路径.
        :param UploadId(string): 分块上传创建的UploadId.
        :param MultipartUpload(dict): 所有分块的信息,包含Etag和PartNumber.
        :param kwargs(dict): 设置请求headers.
        :return(dict): 上传成功返回的结果,包含整个文件的ETag等信息.

        .. code-block:: python

            config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
            client = CosS3Client(config)
            # 分块上传
            response = client.complete_multipart_upload(
                Bucket='bucket',
                Key='multipartfile.txt',
                UploadId='uploadid',
                MultipartUpload={'Part': lst}
            )
        """
        headers = mapped(kwargs)
        params = {'uploadId': UploadId}
        params = format_values(params)
        url = self._conf.uri(bucket=Bucket, path=Key)
        rt = self.send_request(
            method='POST',
            url=url,
            bucket=Bucket,
            auth=CosS3Auth(self._conf, Key, params=params),
            data=dict_to_xml(MultipartUpload),
            timeout=1200,  # 分片上传大文件的时间比较长,设置为20min
            headers=headers,
            params=params)
        body = xml_to_dict(rt.content)
        # 分块上传文件返回200OK并不能代表文件上传成功,返回的body里面如果没有ETag则认为上传失败
        if 'ETag' not in body:
            raise CosServiceError('POST', rt.content, 200)
        data = dict(**rt.headers)
        data.update(body)
        return data
Example #5
0
    def copy_object(self,
                    Bucket,
                    Key,
                    CopySource,
                    CopyStatus='Copy',
                    **kwargs):
        """文件拷贝,文件信息修改

        :param Bucket(string): 存储桶名称.
        :param Key(string): 上传COS路径.
        :param CopySource(dict): 拷贝源,包含Appid,Bucket,Region,Key.
        :param CopyStatus(string): 拷贝状态,可选值'Copy'|'Replaced'.
        :param kwargs(dict): 设置请求headers.
        :return(dict): 拷贝成功的结果.

        .. code-block:: python

            config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
            client = CosS3Client(config)
            # 文件拷贝
            copy_source = {'Bucket': 'test04-1252448703', 'Key': '/test.txt', 'Region': 'ap-beijing-1'}
            response = client.copy_object(
                Bucket='bucket',
                Key='test.txt',
                CopySource=copy_source
            )
        """
        headers = mapped(kwargs)
        headers['x-cos-copy-source'] = gen_copy_source_url(CopySource)
        if CopyStatus != 'Copy' and CopyStatus != 'Replaced':
            raise CosClientError('CopyStatus must be Copy or Replaced')
        headers['x-cos-metadata-directive'] = CopyStatus
        url = self._conf.uri(bucket=Bucket, path=Key)
        rt = self.send_request(method='PUT',
                               url=url,
                               bucket=Bucket,
                               auth=CosS3Auth(self._conf, Key),
                               headers=headers)
        body = xml_to_dict(rt.content)
        if 'ETag' not in body:
            raise CosServiceError('PUT', rt.content, 200)
        data = dict(**rt.headers)
        data.update(body)
        return data
Example #6
0
def put_object(client, Bucket, Body, Key, EnableMD5=False, **kwargs):
    """单文件上传接口,适用于小文件,最大不得超过5GB

    :param Bucket(string): 存储桶名称.
    :param Body(file|string): 上传的文件内容,类型为文件流或字节流.
    :param Key(string): COS路径.
    :param EnableMD5(bool): 是否需要SDK计算Content-MD5,打开此开关会增加上传耗时.
    :kwargs(dict): 设置上传的headers.
    :return(dict): 上传成功返回的结果,包含ETag等信息.

    .. code-block:: python

        config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
        client = CosS3Client(config)
        # 上传本地文件到cos
        with open('test.txt', 'rb') as fp:
            response = client.put_object(
                Bucket='bucket',
                Body=fp,
                Key='test.txt'
            )
            print (response['ETag'])
    """
    if not isinstance(client, CosS3Client):
        raise Exception("client error!")
    check_object_content_length(Body)
    headers = mapped(kwargs)
    url = client._conf.uri(bucket=Bucket, path=Key)
    logging.info("put object, url=:{url} ,headers=:{headers}".format(
        url=url,
        headers=headers))
    if EnableMD5:
        md5_str = get_content_md5(Body)
        if md5_str:
            headers['Content-MD5'] = md5_str
    rt = client.send_request(
        method='PUT',
        url=url,
        bucket=Bucket,
        auth=CosS3Auth(client._conf, Key),
        data=Body,
        headers=headers)

    return rt.url
Example #7
0
    def list_multipart_uploads(self,
                               Bucket,
                               Prefix="",
                               Delimiter="",
                               KeyMarker="",
                               UploadIdMarker="",
                               MaxUploads=1000,
                               EncodingType="",
                               **kwargs):
        """获取Bucket中正在进行的分块上传

        :param Bucket(string): 存储桶名称.
        :param Prefix(string): 设置匹配文件的前缀.
        :param Delimiter(string): 分隔符.
        :param KeyMarker(string): 从KeyMarker指定的Key开始列出条目.
        :param UploadIdMarker(string): 从UploadIdMarker指定的UploadID开始列出条目.
        :param MaxUploads(int): 设置单次返回最大的数量,最大为1000.
        :param EncodingType(string): 设置返回结果编码方式,只能设置为url.
        :param kwargs(dict): 设置请求headers.
        :return(dict): 文件的相关信息,包括Etag等信息.

        .. code-block:: python

            config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
            client = CosS3Client(config)
            # 列出所有分块上传
            response = client.list_multipart_uploads(
                Bucket='bucket',
                MaxUploads=100,
                Prefix='中文',
                Delimiter='/'
            )
        """
        headers = mapped(kwargs)
        decodeflag = True
        url = self._conf.uri(bucket=Bucket)
        params = {
            'uploads': '',
            'prefix': Prefix,
            'delimiter': Delimiter,
            'key-marker': KeyMarker,
            'upload-id-marker': UploadIdMarker,
            'max-uploads': MaxUploads
        }
        if EncodingType:
            if EncodingType != 'url':
                raise CosClientError('EncodingType must be url')
            decodeflag = False
            params['encoding-type'] = EncodingType
        else:
            params['encoding-type'] = 'url'
        params = format_values(params)
        rt = self.send_request(method='GET',
                               url=url,
                               bucket=Bucket,
                               params=params,
                               headers=headers,
                               auth=CosS3Auth(self._conf, params=params))

        data = xml_to_dict(rt.content)
        format_dict(data, ['Upload', 'CommonPrefixes'])
        if decodeflag:
            decode_result(data, [
                'Prefix', 'KeyMarker', 'NextKeyMarker', 'UploadIdMarker',
                'NextUploadIdMarker'
            ], [['Upload', 'Key'], ['CommonPrefixes', 'Prefix']])
        return data
Example #8
0
    def list_objects(self,
                     Bucket,
                     Prefix="",
                     Delimiter="",
                     Marker="",
                     MaxKeys=1000,
                     EncodingType="",
                     **kwargs):
        """获取文件列表

        :param Bucket(string): 存储桶名称.
        :param Prefix(string): 设置匹配文件的前缀.
        :param Delimiter(string): 分隔符.
        :param Marker(string): 从marker开始列出条目.
        :param MaxKeys(int): 设置单次返回最大的数量,最大为1000.
        :param EncodingType(string): 设置返回结果编码方式,只能设置为url.
        :param kwargs(dict): 设置请求headers.
        :return(dict): 文件的相关信息,包括Etag等信息.

        .. code-block:: python

            config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
            client = CosS3Client(config)
            # 列出bucket
            response = client.list_objects(
                Bucket='bucket',
                MaxKeys=100,
                Prefix='中文',
                Delimiter='/'
            )
        """
        decodeflag = True  # 是否需要对结果进行decode
        headers = mapped(kwargs)
        url = self._conf.uri(bucket=Bucket)

        params = {
            'prefix': Prefix,
            'delimiter': Delimiter,
            'marker': Marker,
            'max-keys': MaxKeys
        }
        if EncodingType:
            if EncodingType != 'url':
                raise CosClientError('EncodingType must be url')
            decodeflag = False  # 用户自己设置了EncodingType不需要去decode
            params['encoding-type'] = EncodingType
        else:
            params['encoding-type'] = 'url'
        params = format_values(params)
        rt = self.send_request(method='GET',
                               url=url,
                               bucket=Bucket,
                               params=params,
                               headers=headers,
                               auth=CosS3Auth(self._conf, params=params))
        data = xml_to_dict(rt.content)
        format_dict(data, ['Contents', 'CommonPrefixes'])
        if decodeflag:
            decode_result(data, ['Prefix', 'Marker', 'NextMarker'],
                          [['Contents', 'Key'], ['CommonPrefixes', 'Prefix']])
        return data