From 6ba8fd1f40cd62d2ebc1ae1393450e6e10d4c446 Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Tue, 3 Aug 2021 20:26:24 +0800 Subject: [PATCH 01/12] support redirect config --- qcloud_cos/cos_client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index 4f544a59..108b868e 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -39,7 +39,7 @@ class CosConfig(object): def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token=None, Scheme=None, Timeout=None, Access_id=None, Access_key=None, Secret_id=None, Secret_key=None, Endpoint=None, IP=None, Port=None, Anonymous=None, UA=None, Proxies=None, Domain=None, ServiceDomain=None, PoolConnections=10, - PoolMaxSize=10): + PoolMaxSize=10, AllowRedirects=True): """初始化,保存用户的信息 :param Appid(string): 用户APPID. @@ -63,6 +63,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token :param ServiceDomain(string): 使用自定义的域名来访问cos service :param PoolConnections(int): 连接池个数 :param PoolMaxSize(int): 连接池中最大连接数 + :param AllowRedirects(bool): 是否重定向 """ self._appid = to_unicode(Appid) self._token = to_unicode(Token) @@ -78,6 +79,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token self._service_domain = ServiceDomain self._pool_connections = PoolConnections self._pool_maxsize = PoolMaxSize + self._allow_redirects = AllowRedirects if self._domain is None: self._endpoint = format_endpoint(Endpoint, Region) @@ -255,6 +257,8 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwar kwargs['data'] = to_bytes(kwargs['data']) if self._conf._ip is not None and self._conf._scheme == 'https': kwargs['verify'] = False + if self._conf._allow_redirects is not None: + kwargs['allow_redirects'] = self._conf._allow_redirects for j in range(self._retry + 1): try: if j != 0: From d8fdd0db921698ec1344dbaaf8840c3ffbd1f6d6 Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Wed, 4 Aug 2021 17:38:20 +0800 Subject: [PATCH 02/12] set allow_redirects default value to false --- qcloud_cos/cos_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index 108b868e..9ebf4e47 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -39,7 +39,7 @@ class CosConfig(object): def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token=None, Scheme=None, Timeout=None, Access_id=None, Access_key=None, Secret_id=None, Secret_key=None, Endpoint=None, IP=None, Port=None, Anonymous=None, UA=None, Proxies=None, Domain=None, ServiceDomain=None, PoolConnections=10, - PoolMaxSize=10, AllowRedirects=True): + PoolMaxSize=10, AllowRedirects=False): """初始化,保存用户的信息 :param Appid(string): 用户APPID. From 330d9438b9a2d17fd3ca036a22b4ae00061aa8e7 Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Thu, 19 Aug 2021 20:07:07 +0800 Subject: [PATCH 03/12] add host to str_to_sign --- qcloud_cos/cos_auth.py | 5 +++++ qcloud_cos/cos_client.py | 2 ++ 2 files changed, 7 insertions(+) diff --git a/qcloud_cos/cos_auth.py b/qcloud_cos/cos_auth.py index 158c01cf..04605de5 100644 --- a/qcloud_cos/cos_auth.py +++ b/qcloud_cos/cos_auth.py @@ -40,6 +40,7 @@ def __init__(self, conf, key=None, params={}, expire=10000): self._secret_id = conf._secret_id self._secret_key = conf._secret_key self._anonymous = conf._anonymous + self._host = conf._host self._expire = expire self._params = params if key: @@ -55,6 +56,10 @@ def __call__(self, r): path = self._path uri_params = self._params headers = filter_headers(r.headers) + + if self._host is not None: + headers["host"] = self._host # host加入签名计算,避免别篡改 + # reserved keywords in headers urlencode are -_.~, notice that / should be encoded and space should not be encoded to plus sign(+) headers = dict([(quote(to_bytes(to_str(k)), '-_.~').lower(), quote(to_bytes(to_str(v)), '-_.~')) for k, v in headers.items()]) # headers中的key转换为小写,value进行encode diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index 108b868e..05626539 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -118,12 +118,14 @@ def uri(self, bucket, path=None, endpoint=None, domain=None): domain = self._domain if domain is not None: url = domain + self._host = domain else: bucket = format_bucket(bucket, self._appid) if endpoint is None: endpoint = self._endpoint url = u"{bucket}.{endpoint}".format(bucket=bucket, endpoint=endpoint) + self._host = url if self._ip is not None: url = self._ip if self._port is not None: From 650ef6add9a6cce4193022454c495ceab4b73a41 Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Thu, 19 Aug 2021 22:10:46 +0800 Subject: [PATCH 04/12] add host to the string to sign --- qcloud_cos/cos_client.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index 02af0b26..5643fb0c 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -80,6 +80,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token self._pool_connections = PoolConnections self._pool_maxsize = PoolMaxSize self._allow_redirects = AllowRedirects + self._host = None # 给一个默认值,避免使用时报成员不存在 if self._domain is None: self._endpoint = format_endpoint(Endpoint, Region) @@ -3008,9 +3009,12 @@ def list_buckets(self, **kwargs): response = client.list_buckets() """ headers = mapped(kwargs) - url = '{scheme}://service.cos.myqcloud.com/'.format(scheme=self._conf._scheme) + host = u'service.cos.myqcloud.com' + self._conf._host = host + url = '{scheme}://{host}/'.format(scheme=self._conf._scheme, host=host) if self._conf._service_domain is not None: url = '{scheme}://{domain}/'.format(scheme=self._conf._scheme, domain=self._conf._service_domain) + self._conf._host = self._conf._service_domain rt = self.send_request( method='GET', url=url, @@ -3270,8 +3274,10 @@ def _inner_head_object(self, CopySource): params = {} if versionid != '': params['versionId'] = versionid - url = u"{scheme}://{bucket}.{endpoint}/{path}".format(scheme=self._conf._scheme, bucket=bucket, - endpoint=endpoint, path=quote(to_bytes(path), '/-_.~')) + host = u'{bucket}.{endpoint}'.format(bucket=bucket, endpoint=endpoint) + self._conf._host = host + url = u"{scheme}://{host}/{path}".format(scheme=self._conf._scheme, host=host, + path=quote(to_bytes(path), '/-_.~')) rt = self.send_request( method='HEAD', url=url, From 7b0f6b413971d8204bdd68fa7c259056a70c408e Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Fri, 20 Aug 2021 15:38:58 +0800 Subject: [PATCH 05/12] add host to the string to sign --- qcloud_cos/cos_client.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index f39756df..5643fb0c 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -80,6 +80,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token self._pool_connections = PoolConnections self._pool_maxsize = PoolMaxSize self._allow_redirects = AllowRedirects + self._host = None # 给一个默认值,避免使用时报成员不存在 if self._domain is None: self._endpoint = format_endpoint(Endpoint, Region) From 4e940df53af99bc361172bdea5f442ca47bd3488 Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Fri, 20 Aug 2021 15:42:38 +0800 Subject: [PATCH 06/12] add host to the string to sign --- qcloud_cos/cos_auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qcloud_cos/cos_auth.py b/qcloud_cos/cos_auth.py index 04605de5..906fcaf4 100644 --- a/qcloud_cos/cos_auth.py +++ b/qcloud_cos/cos_auth.py @@ -58,7 +58,7 @@ def __call__(self, r): headers = filter_headers(r.headers) if self._host is not None: - headers["host"] = self._host # host加入签名计算,避免别篡改 + headers["host"] = self._host # host加入签名计算,避免被篡改 # reserved keywords in headers urlencode are -_.~, notice that / should be encoded and space should not be encoded to plus sign(+) headers = dict([(quote(to_bytes(to_str(k)), '-_.~').lower(), quote(to_bytes(to_str(v)), '-_.~')) for k, v in From a0f7b37ba06c09f3cbdded068465983f467b72e1 Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Mon, 23 Aug 2021 18:50:31 +0800 Subject: [PATCH 07/12] add host to the string to sign --- qcloud_cos/cos_auth.py | 32 ++++++++++++++++++++++++++++---- qcloud_cos/cos_client.py | 33 +++++++++++++++------------------ 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/qcloud_cos/cos_auth.py b/qcloud_cos/cos_auth.py index 906fcaf4..331b52e0 100644 --- a/qcloud_cos/cos_auth.py +++ b/qcloud_cos/cos_auth.py @@ -36,13 +36,19 @@ def filter_headers(data): class CosS3Auth(AuthBase): - def __init__(self, conf, key=None, params={}, expire=10000): + def __init__(self, conf, key=None, params={}, expire=10000, sign_host=None): self._secret_id = conf._secret_id self._secret_key = conf._secret_key self._anonymous = conf._anonymous - self._host = conf._host self._expire = expire self._params = params + + # 如果API指定了是否签名host,则以具体API为准,如果未指定则以配置为准 + if sign_host is True or sign_host is False: + self._sign_host = sign_host + else: + self._sign_host = conf._sign_host + if key: key = to_unicode(key) if key[0] == u'/': @@ -57,8 +63,26 @@ def __call__(self, r): uri_params = self._params headers = filter_headers(r.headers) - if self._host is not None: - headers["host"] = self._host # host加入签名计算,避免被篡改 + # 如果headers中不包含host头域,则从url中提取host,并且加入签名计算 + if self._sign_host: + + # 判断headers中是否包含host头域 + contain_host = False + for i in headers: + if str.lower(i) == 'host': + contain_host = True + break + + # 从url中提取host + if contain_host is False: + host_str = r.url + host_begin = host_str.find('//') + 2 + if host_begin >= 2 and host_begin < len(host_str): + host_str = r.url[host_begin:] + host_end = host_str.find('/') + if host_end > 0: + host_str = host_str[:host_end] + headers["host"] = host_str # reserved keywords in headers urlencode are -_.~, notice that / should be encoded and space should not be encoded to plus sign(+) headers = dict([(quote(to_bytes(to_str(k)), '-_.~').lower(), quote(to_bytes(to_str(v)), '-_.~')) for k, v in diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index 5643fb0c..bfca7996 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -39,7 +39,7 @@ class CosConfig(object): def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token=None, Scheme=None, Timeout=None, Access_id=None, Access_key=None, Secret_id=None, Secret_key=None, Endpoint=None, IP=None, Port=None, Anonymous=None, UA=None, Proxies=None, Domain=None, ServiceDomain=None, PoolConnections=10, - PoolMaxSize=10, AllowRedirects=False): + PoolMaxSize=10, AllowRedirects=False, SignHost=True): """初始化,保存用户的信息 :param Appid(string): 用户APPID. @@ -64,6 +64,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token :param PoolConnections(int): 连接池个数 :param PoolMaxSize(int): 连接池中最大连接数 :param AllowRedirects(bool): 是否重定向 + :param SignHost(bool): 是否将host算入签名 """ self._appid = to_unicode(Appid) self._token = to_unicode(Token) @@ -80,7 +81,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token self._pool_connections = PoolConnections self._pool_maxsize = PoolMaxSize self._allow_redirects = AllowRedirects - self._host = None # 给一个默认值,避免使用时报成员不存在 + self._sign_host = SignHost if self._domain is None: self._endpoint = format_endpoint(Endpoint, Region) @@ -119,14 +120,12 @@ def uri(self, bucket, path=None, endpoint=None, domain=None): domain = self._domain if domain is not None: url = domain - self._host = domain else: bucket = format_bucket(bucket, self._appid) if endpoint is None: endpoint = self._endpoint url = u"{bucket}.{endpoint}".format(bucket=bucket, endpoint=endpoint) - self._host = url if self._ip is not None: url = self._ip if self._port is not None: @@ -204,13 +203,14 @@ def get_conf(self): """获取配置""" return self._conf - def get_auth(self, Method, Bucket, Key, Expired=300, Headers={}, Params={}): + def get_auth(self, Method, Bucket, Key, Expired=300, SignHost=None, Headers={}, Params={}): """获取签名 :param Method(string): http method,如'PUT','GET'. :param Bucket(string): 存储桶名称. :param Key(string): 请求COS的路径. :param Expired(int): 签名有效时间,单位为s. + :param SignHost(bool): 是否将host算入签名. :param headers(dict): 签名中的http headers. :param params(dict): 签名中的http params. :return (string): 计算出的V5签名. @@ -232,7 +232,7 @@ def get_auth(self, Method, Bucket, Key, Expired=300, Headers={}, Params={}): """ url = self._conf.uri(bucket=Bucket, path=Key) r = Request(Method, url, headers=Headers, params=Params) - auth = CosS3Auth(self._conf, Key, Params, Expired) + auth = CosS3Auth(self._conf, Key, Params, Expired, SignHost) return auth(r).headers['Authorization'] def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwargs): @@ -494,13 +494,14 @@ def get_object_sensitive_content_recognition(self, Bucket, Key, DetectType, Inte return data - def get_presigned_url(self, Bucket, Key, Method, Expired=300, Params={}, Headers={}): + def get_presigned_url(self, Bucket, Key, Method, Expired=300, SignHost=None, Params={}, Headers={}): """生成预签名的url :param Bucket(string): 存储桶名称. :param Key(string): COS路径. :param Method(string): HTTP请求的方法, 'PUT'|'POST'|'GET'|'DELETE'|'HEAD' :param Expired(int): 签名过期时间. + :param SignHost(bool): 是否将host算入签名. :param Params(dict): 签入签名的参数 :param Headers(dict): 签入签名的头部 :return(string): 预先签名的URL. @@ -517,19 +518,20 @@ def get_presigned_url(self, Bucket, Key, Method, Expired=300, Params={}, Headers ) """ url = self._conf.uri(bucket=Bucket, path=Key) - sign = self.get_auth(Method=Method, Bucket=Bucket, Key=Key, Expired=Expired, Headers=Headers, Params=Params) + sign = self.get_auth(Method=Method, Bucket=Bucket, Key=Key, Expired=Expired, SignHost=SignHost, Headers=Headers, Params=Params) sign = urlencode(dict([item.split('=', 1) for item in sign.split('&')])) url = url + '?' + sign if Params: url = url + '&' + urlencode(Params) return url - def get_presigned_download_url(self, Bucket, Key, Expired=300, Params={}, Headers={}): + def get_presigned_download_url(self, Bucket, Key, Expired=300, SignHost=None, Params={}, Headers={}): """生成预签名的下载url :param Bucket(string): 存储桶名称. :param Key(string): COS路径. :param Expired(int): 签名过期时间. + :param SignHost(bool): 是否将host算入签名. :param Params(dict): 签入签名的参数 :param Headers(dict): 签入签名的头部 :return(string): 预先签名的下载URL. @@ -544,7 +546,7 @@ def get_presigned_download_url(self, Bucket, Key, Expired=300, Params={}, Header Key='test.txt' ) """ - return self.get_presigned_url(Bucket, Key, 'GET', Expired, Params, Headers) + return self.get_presigned_url(Bucket, Key, 'GET', Expired, SignHost, Params, Headers) def get_object_url(self, Bucket, Key): """生成对象访问的url @@ -3009,12 +3011,9 @@ def list_buckets(self, **kwargs): response = client.list_buckets() """ headers = mapped(kwargs) - host = u'service.cos.myqcloud.com' - self._conf._host = host - url = '{scheme}://{host}/'.format(scheme=self._conf._scheme, host=host) + url = '{scheme}://service.cos.myqcloud.com/'.format(scheme=self._conf._scheme) if self._conf._service_domain is not None: url = '{scheme}://{domain}/'.format(scheme=self._conf._scheme, domain=self._conf._service_domain) - self._conf._host = self._conf._service_domain rt = self.send_request( method='GET', url=url, @@ -3274,10 +3273,8 @@ def _inner_head_object(self, CopySource): params = {} if versionid != '': params['versionId'] = versionid - host = u'{bucket}.{endpoint}'.format(bucket=bucket, endpoint=endpoint) - self._conf._host = host - url = u"{scheme}://{host}/{path}".format(scheme=self._conf._scheme, host=host, - path=quote(to_bytes(path), '/-_.~')) + url = u"{scheme}://{bucket}.{endpoint}/{path}".format(scheme=self._conf._scheme, bucket=bucket, + endpoint=endpoint, path=quote(to_bytes(path), '/-_.~')) rt = self.send_request( method='HEAD', url=url, From 32275af767c740ed19cc5751653d20a45b789825 Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Tue, 24 Aug 2021 11:46:42 +0800 Subject: [PATCH 08/12] url = u"{scheme}://{bucket}.{endpoint}/{ --- .gitignore | 2 ++ qcloud_cos/cos_auth.py | 11 +++-------- qcloud_cos/cos_client.py | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 0d20b648..31eec97e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *.pyc +cos_test.py +.vscode/launch.json diff --git a/qcloud_cos/cos_auth.py b/qcloud_cos/cos_auth.py index 331b52e0..7ab779cc 100644 --- a/qcloud_cos/cos_auth.py +++ b/qcloud_cos/cos_auth.py @@ -75,14 +75,9 @@ def __call__(self, r): # 从url中提取host if contain_host is False: - host_str = r.url - host_begin = host_str.find('//') + 2 - if host_begin >= 2 and host_begin < len(host_str): - host_str = r.url[host_begin:] - host_end = host_str.find('/') - if host_end > 0: - host_str = host_str[:host_end] - headers["host"] = host_str + url_parsed = urlparse(r.url) + if url_parsed.hostname is not None: + headers["host"] = url_parsed.hostname # reserved keywords in headers urlencode are -_.~, notice that / should be encoded and space should not be encoded to plus sign(+) headers = dict([(quote(to_bytes(to_str(k)), '-_.~').lower(), quote(to_bytes(to_str(v)), '-_.~')) for k, v in diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index bfca7996..3c0b578e 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -3273,7 +3273,7 @@ def _inner_head_object(self, CopySource): params = {} if versionid != '': params['versionId'] = versionid - url = u"{scheme}://{bucket}.{endpoint}/{path}".format(scheme=self._conf._scheme, bucket=bucket, + url = u"{scheme}://{bucket}.{endpoint}/{path}".format(scheme=self._conf._scheme, bucket=bucket, endpoint=endpoint, path=quote(to_bytes(path), '/-_.~')) rt = self.send_request( method='HEAD', From 13600d021c1a6d405eaaa2a9a403c3e4c1fc941f Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Tue, 24 Aug 2021 11:50:22 +0800 Subject: [PATCH 09/12] add host to the string to sign --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 31eec97e..0d20b648 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ *.pyc -cos_test.py -.vscode/launch.json From a220f19016dc28ececf0604c51539905117f4c4c Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Tue, 24 Aug 2021 15:50:20 +0800 Subject: [PATCH 10/12] add host to the string to sign --- qcloud_cos/cos_auth.py | 8 ++++---- qcloud_cos/cos_client.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/qcloud_cos/cos_auth.py b/qcloud_cos/cos_auth.py index 7ab779cc..ef4e8564 100644 --- a/qcloud_cos/cos_auth.py +++ b/qcloud_cos/cos_auth.py @@ -44,8 +44,8 @@ def __init__(self, conf, key=None, params={}, expire=10000, sign_host=None): self._params = params # 如果API指定了是否签名host,则以具体API为准,如果未指定则以配置为准 - if sign_host is True or sign_host is False: - self._sign_host = sign_host + if sign_host is not None: + self._sign_host = bool(sign_host) else: self._sign_host = conf._sign_host @@ -69,12 +69,12 @@ def __call__(self, r): # 判断headers中是否包含host头域 contain_host = False for i in headers: - if str.lower(i) == 'host': + if str.lower(i) == "host": # 兼容host/Host/HOST等 contain_host = True break # 从url中提取host - if contain_host is False: + if not contain_host: url_parsed = urlparse(r.url) if url_parsed.hostname is not None: headers["host"] = url_parsed.hostname diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index 3c0b578e..331c8f6d 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -203,16 +203,16 @@ def get_conf(self): """获取配置""" return self._conf - def get_auth(self, Method, Bucket, Key, Expired=300, SignHost=None, Headers={}, Params={}): + def get_auth(self, Method, Bucket, Key, Expired=300, Headers={}, Params={}, SignHost=None): """获取签名 :param Method(string): http method,如'PUT','GET'. :param Bucket(string): 存储桶名称. :param Key(string): 请求COS的路径. :param Expired(int): 签名有效时间,单位为s. - :param SignHost(bool): 是否将host算入签名. :param headers(dict): 签名中的http headers. :param params(dict): 签名中的http params. + :param SignHost(bool): 是否将host算入签名. :return (string): 计算出的V5签名. .. code-block:: python @@ -494,16 +494,16 @@ def get_object_sensitive_content_recognition(self, Bucket, Key, DetectType, Inte return data - def get_presigned_url(self, Bucket, Key, Method, Expired=300, SignHost=None, Params={}, Headers={}): + def get_presigned_url(self, Bucket, Key, Method, Expired=300, Params={}, Headers={}, SignHost=None): """生成预签名的url :param Bucket(string): 存储桶名称. :param Key(string): COS路径. :param Method(string): HTTP请求的方法, 'PUT'|'POST'|'GET'|'DELETE'|'HEAD' :param Expired(int): 签名过期时间. - :param SignHost(bool): 是否将host算入签名. :param Params(dict): 签入签名的参数 :param Headers(dict): 签入签名的头部 + :param SignHost(bool): 是否将host算入签名. :return(string): 预先签名的URL. .. code-block:: python @@ -518,22 +518,22 @@ def get_presigned_url(self, Bucket, Key, Method, Expired=300, SignHost=None, Par ) """ url = self._conf.uri(bucket=Bucket, path=Key) - sign = self.get_auth(Method=Method, Bucket=Bucket, Key=Key, Expired=Expired, SignHost=SignHost, Headers=Headers, Params=Params) + sign = self.get_auth(Method=Method, Bucket=Bucket, Key=Key, Expired=Expired, Headers=Headers, Params=Params, SignHost=SignHost) sign = urlencode(dict([item.split('=', 1) for item in sign.split('&')])) url = url + '?' + sign if Params: url = url + '&' + urlencode(Params) return url - def get_presigned_download_url(self, Bucket, Key, Expired=300, SignHost=None, Params={}, Headers={}): + def get_presigned_download_url(self, Bucket, Key, Expired=300, Params={}, Headers={}, SignHost=None): """生成预签名的下载url :param Bucket(string): 存储桶名称. :param Key(string): COS路径. :param Expired(int): 签名过期时间. - :param SignHost(bool): 是否将host算入签名. :param Params(dict): 签入签名的参数 :param Headers(dict): 签入签名的头部 + :param SignHost(bool): 是否将host算入签名. :return(string): 预先签名的下载URL. .. code-block:: python @@ -546,7 +546,7 @@ def get_presigned_download_url(self, Bucket, Key, Expired=300, SignHost=None, Pa Key='test.txt' ) """ - return self.get_presigned_url(Bucket, Key, 'GET', Expired, SignHost, Params, Headers) + return self.get_presigned_url(Bucket, Key, 'GET', Expired, Params, Headers, SignHost) def get_object_url(self, Bucket, Key): """生成对象访问的url From 3301558e4773cd70be066bff5bb0c29512e94a60 Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Fri, 8 Oct 2021 16:51:45 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E9=92=88=E5=AF=B9=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E8=A6=81=E6=9B=BF=E6=8D=A2=E8=87=AA=E5=B7=B1=E7=9A=84=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E8=A6=81=E8=AE=BE=E7=BD=AE=E7=9A=84=E5=A4=B4?= =?UTF-8?q?=E5=9F=9F=E5=8F=82=E6=95=B0=E3=80=81=E6=88=96=E8=80=85=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E5=8F=82=E6=95=B0=E8=A6=81=E6=9C=89=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=8C=87=E5=AF=BC=E5=92=8C=E7=A4=BA=E4=BE=8B=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/batch_operation_demo.py | 13 +++++++------ demo/ci_compress.py | 13 +++++++------ demo/ci_watermark.py | 13 +++++++------ demo/demo.py | 20 ++++++++++---------- demo/dir_download_demo.py | 16 ++++++++-------- demo/fetch_demo.py | 15 ++++++++------- 6 files changed, 47 insertions(+), 43 deletions(-) diff --git a/demo/batch_operation_demo.py b/demo/batch_operation_demo.py index 4626e9ae..c77f4bc7 100644 --- a/demo/batch_operation_demo.py +++ b/demo/batch_operation_demo.py @@ -17,12 +17,13 @@ logging.basicConfig(level=logging.INFO, stream=sys.stdout) -# 设置用户属性, 包括secret_id, secret_key, region -# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成 -secret_id = '' # 替换为用户的secret_id -secret_key = '' # 替换为用户的secret_key -region = 'ap-guangzhou' # 替换为用户的region -token = None # 使用临时密钥需要传入Token,默认为空,可不填 +# 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在CosConfig中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 +secret_id = 'SecretId' # 替换为用户的 SecretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +secret_key = 'SecretKey' # 替换为用户的 SecretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket + # COS支持的所有region列表参见https://www.qcloud.com/document/product/436/6224 +token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048 + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 client = CosS3Client(config) diff --git a/demo/ci_compress.py b/demo/ci_compress.py index 434f39a3..09bbe1ee 100644 --- a/demo/ci_compress.py +++ b/demo/ci_compress.py @@ -12,12 +12,13 @@ logging.basicConfig(level=logging.INFO, stream=sys.stdout) -# 设置用户属性, 包括secret_id, secret_key, region -# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成 -secret_id = '' # 替换为用户的secret_id -secret_key = '' # 替换为用户的secret_key -region = 'ap-guangzhou' # 替换为用户的region -token = None # 使用临时密钥需要传入Token,默认为空,可不填 +# 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在CosConfig中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 +secret_id = 'SecretId' # 替换为用户的 SecretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +secret_key = 'SecretKey' # 替换为用户的 SecretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket + # COS支持的所有region列表参见https://www.qcloud.com/document/product/436/6224 +token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048 + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 client = CosS3Client(config) diff --git a/demo/ci_watermark.py b/demo/ci_watermark.py index 9a5dfca6..777e95c1 100644 --- a/demo/ci_watermark.py +++ b/demo/ci_watermark.py @@ -12,12 +12,13 @@ logging.basicConfig(level=logging.INFO, stream=sys.stdout) -# 设置用户属性, 包括secret_id, secret_key, region -# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成 -secret_id = '' # 替换为用户的secret_id -secret_key = '' # 替换为用户的secret_key -region = 'ap-guangzhou' # 替换为用户的region -token = None # 使用临时密钥需要传入Token,默认为空,可不填 +# 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在CosConfig中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 +secret_id = 'SecretId' # 替换为用户的 SecretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +secret_key = 'SecretKey' # 替换为用户的 SecretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket + # COS支持的所有region列表参见https://www.qcloud.com/document/product/436/6224 +token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048 + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 client = CosS3Client(config) diff --git a/demo/demo.py b/demo/demo.py index d20c3d52..3fe2a4cf 100644 --- a/demo/demo.py +++ b/demo/demo.py @@ -30,16 +30,16 @@ def percentage(consumed_bytes, total_bytes): logging.basicConfig(level=logging.INFO, stream=sys.stdout) - # 设置用户属性, 包括secret_id, secret_key, region - # appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成 - secret_id = 'secret_id' # 替换为用户的secret_id - secret_key = 'secret_key' # 替换为用户的secret_key - region = 'ap-beijing' # 替换为用户的region - token = None # 使用临时密钥需要传入Token,默认为空,可不填 - - # domain自定义域名, 通常不用设置, 如果使用全球加速域名, 则设置成对应的域名, 如mybucket-01234.cos.accelerate.myqcloud.com, - # 开启全球加速请参考https://cloud.tencent.com/document/product/436/38864 - domain = None + # 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在CosConfig中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 + secret_id = 'SecretId' # 替换为用户的 SecretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi + secret_key = 'SecretKey' # 替换为用户的 SecretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi + region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket + # COS支持的所有region列表参见https://www.qcloud.com/document/product/436/6224 + token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048 + domain = None # domain可以不填,此时使用COS区域域名访问存储桶。domain也可以填写用户自定义域名,或者桶的全球加速域名 + # 填写用户自定义域名,比如user-define.example.com,需要先开启桶的自定义域名,具体请参见https://cloud.tencent.com/document/product/436/36638 + # 填写桶的全球加速域名,比如examplebucket-1250000000.cos.accelerate.myqcloud.com,需要先开启桶的全球加速功能,请参见https://cloud.tencent.com/document/product/436/38864 + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Domain=domain) # 获取配置对象 client = CosS3Client(config) diff --git a/demo/dir_download_demo.py b/demo/dir_download_demo.py index 634897b7..2f7edd61 100644 --- a/demo/dir_download_demo.py +++ b/demo/dir_download_demo.py @@ -10,14 +10,14 @@ logging.basicConfig(level=logging.INFO, stream=sys.stdout) -# 设置用户属性, 包括secret_id, secret_key, region -# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成 -secret_id = 'secret_id' # 替换为用户的secret_id -secret_key = 'secret_key' # 替换为用户的secret_key -region = 'ap-shanghai' # 替换为用户的region - -token = None # 使用临时密钥需要传入Token,默认为空,可不填 -scheme = 'http' +# 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在CosConfig中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 +secret_id = 'SecretId' # 替换为用户的 SecretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +secret_key = 'SecretKey' # 替换为用户的 SecretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket + # COS支持的所有region列表参见https://www.qcloud.com/document/product/436/6224 +token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048 +scheme = 'http' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) # 获取配置对象 client = CosS3Client(config) diff --git a/demo/fetch_demo.py b/demo/fetch_demo.py index 7b50d7a1..f46a6944 100644 --- a/demo/fetch_demo.py +++ b/demo/fetch_demo.py @@ -9,13 +9,14 @@ logging.basicConfig(level=logging.INFO, stream=sys.stdout) -# 设置用户属性, 包括secret_id, secret_key, region -# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成 -secret_id = '' # 替换为用户的secret_id -secret_key = '' # 替换为用户的secret_key -region = 'ap-beijing' # 替换为用户的region -token = None # 使用临时密钥需要传入Token,默认为空,可不填 -scheme = 'http' +# 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在CosConfig中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 +secret_id = 'SecretId' # 替换为用户的 SecretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +secret_key = 'SecretKey' # 替换为用户的 SecretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket + # COS支持的所有region列表参见https://www.qcloud.com/document/product/436/6224 +token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048 +scheme = 'http' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) # 获取配置对象 client = CosS3Client(config) From 2c29bd64ff1809810750d899460e0e3ecd8553b5 Mon Sep 17 00:00:00 2001 From: yuniszhang Date: Mon, 11 Oct 2021 20:00:37 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E5=8F=91=E5=B8=83=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- setup.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7e99e367..953e9a97 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.pyc \ No newline at end of file +*.pyc +ignore/ \ No newline at end of file diff --git a/setup.py b/setup.py index 8f656f45..b1dab597 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ def long_description(): setup( name='cos-python-sdk-v5', - version='1.9.8', + version='1.9.9', url='https://www.qcloud.com/', license='MIT', author='tiedu, lewzylu, channingliu',