3030class CosConfig (object ):
3131 """config类,保存用户相关信息"""
3232 def __init__ (self , Appid = None , Region = None , SecretId = None , SecretKey = None , Token = None , Scheme = None , Timeout = None ,
33- Access_id = None , Access_key = None , Secret_id = None , Secret_key = None ,
34- Endpoint = None , IP = None , Port = None , Anonymous = None , UA = None , Proxies = None ):
33+ Access_id = None , Access_key = None , Secret_id = None , Secret_key = None , Endpoint = None , IP = None , Port = None ,
34+ Anonymous = None , UA = None , Proxies = None , Domain = None , ServiceDomain = None ):
3535 """初始化,保存用户的信息
3636
3737 :param Appid(string): 用户APPID.
@@ -51,18 +51,24 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
5151 :param Anonymous(bool): 是否使用匿名访问COS
5252 :param UA(string): 使用自定义的UA来访问COS
5353 :param Proxies(dict): 使用代理来访问COS
54+ :param Domain(string): 使用自定义的域名来访问COS
55+ :param ServiceDomain(string): 使用自定义的域名来访问cos service
5456 """
5557 self ._appid = to_unicode (Appid )
5658 self ._token = to_unicode (Token )
5759 self ._timeout = Timeout
5860 self ._region = Region
59- self ._endpoint = format_endpoint ( Endpoint , Region )
61+ self ._endpoint = Endpoint
6062 self ._ip = to_unicode (IP )
6163 self ._port = Port
6264 self ._anonymous = Anonymous
6365 self ._ua = UA
6466 self ._proxies = Proxies
67+ self ._domain = Domain
68+ self ._service_domain = ServiceDomain
6569
70+ if self ._domain is None :
71+ self ._endpoint = format_endpoint (Endpoint , Region )
6672 if Scheme is None :
6773 Scheme = u'https'
6874 Scheme = to_unicode (Scheme )
@@ -83,21 +89,27 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
8389 else :
8490 raise CosClientError ('SecretId and SecretKey is Required!' )
8591
86- def uri (self , bucket , path = None , endpoint = None ):
92+ def uri (self , bucket , path = None , endpoint = None , domain = None ):
8793 """拼接url
8894
8995 :param bucket(string): 存储桶名称.
9096 :param path(string): 请求COS的路径.
9197 :return(string): 请求COS的URL地址.
9298 """
93- bucket = format_bucket (bucket , self ._appid )
9499 scheme = self ._scheme
95- if endpoint is None :
96- endpoint = self ._endpoint
97-
98100 # 拼接请求的url,默认使用bucket和endpoint拼接请求域名
99- # 指定ip和port时,则使用ip:port方式访问
100- url = u"{bucket}.{endpoint}" .format (bucket = bucket , endpoint = endpoint )
101+ # 使用自定义域名时则使用自定义域名访问
102+ # 指定ip和port时,则使用ip:port方式访问,优先级最高
103+ if domain is None :
104+ domain = self ._domain
105+ if domain is not None :
106+ url = domain
107+ else :
108+ bucket = format_bucket (bucket , self ._appid )
109+ if endpoint is None :
110+ endpoint = self ._endpoint
111+
112+ url = u"{bucket}.{endpoint}" .format (bucket = bucket , endpoint = endpoint )
101113 if self ._ip is not None :
102114 url = self ._ip
103115 if self ._port is not None :
@@ -211,8 +223,11 @@ def send_request(self, method, url, bucket, timeout=30, **kwargs):
211223 kwargs ['headers' ]['User-Agent' ] = 'cos-python-sdk-v' + __version__
212224 if self ._conf ._token is not None :
213225 kwargs ['headers' ]['x-cos-security-token' ] = self ._conf ._token
214- if bucket is not None :
215- kwargs ['headers' ]['Host' ] = self ._conf .get_host (bucket )
226+ if self ._conf ._ip is not None : # 使用IP访问时需要设置请求host
227+ if self ._conf ._domain is not None :
228+ kwargs ['headers' ]['Host' ] = self ._conf ._domain
229+ elif bucket is not None :
230+ kwargs ['headers' ]['Host' ] = self ._conf .get_host (bucket )
216231 kwargs ['headers' ] = format_values (kwargs ['headers' ])
217232 if 'data' in kwargs :
218233 kwargs ['data' ] = to_bytes (kwargs ['data' ])
@@ -2053,7 +2068,7 @@ def put_bucket_policy(self, Bucket, Policy, **kwargs):
20532068 body = Policy
20542069 policy_type = type (body )
20552070 if policy_type != str and policy_type != dict :
2056- raise CosClientError ("Policy must be a json foramt string or json format dict" )
2071+ raise CosClientError ("Policy must be a json format string or json format dict" )
20572072 if policy_type == dict :
20582073 body = json .dumps (body )
20592074
@@ -2573,6 +2588,89 @@ def delete_bucket_tagging(self, Bucket, **kwargs):
25732588 params = params )
25742589 return None
25752590
2591+ def put_bucket_referer (self , Bucket , RefererConfiguration = {}, ** kwargs ):
2592+ """设置bucket的防盗链规则
2593+
2594+ :param Bucket(string): 存储桶名称.
2595+ :param RefererConfiguration(dict): Bucket的防盗链规则
2596+ :param kwargs(dict): 设置请求headers.
2597+ :return: None.
2598+
2599+ .. code-block:: python
2600+
2601+ config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
2602+ client = CosS3Client(config)
2603+ # 设置bucket标签
2604+ referer_config = {
2605+ 'Status': 'Enabled',
2606+ 'RefererType': 'White-List',
2607+ 'EmptyReferConfiguration': 'Allow',
2608+ 'DomainList': {
2609+ 'Domain': [
2610+ '*.qq.com',
2611+ '*.qcloud.com'
2612+ ]
2613+ }
2614+ }
2615+ response = client.put_bucket_referer(
2616+ Bucket='bucket',
2617+ RefererConfiguration=referer_config
2618+ )
2619+ """
2620+ lst = ['<Domain>' , '</Domain>' ] # 类型为list的标签
2621+ xml_config = format_xml (data = RefererConfiguration , root = 'RefererConfiguration' , lst = lst )
2622+ headers = mapped (kwargs )
2623+ headers ['Content-MD5' ] = get_md5 (xml_config )
2624+ headers ['Content-Type' ] = 'application/xml'
2625+ params = {'referer' : '' }
2626+ url = self ._conf .uri (bucket = Bucket )
2627+ logger .info ("put bucket referer, url=:{url} ,headers=:{headers}" .format (
2628+ url = url ,
2629+ headers = headers ))
2630+ rt = self .send_request (
2631+ method = 'PUT' ,
2632+ url = url ,
2633+ bucket = Bucket ,
2634+ data = xml_config ,
2635+ auth = CosS3Auth (self ._conf , params = params ),
2636+ headers = headers ,
2637+ params = params )
2638+ return None
2639+
2640+ def get_bucket_referer (self , Bucket , ** kwargs ):
2641+ """获取bucket防盗链规则
2642+
2643+ :param Bucket(string): 存储桶名称.
2644+ :param kwargs(dict): 设置请求headers.
2645+ :return(dict): Bucket对应的防盗链规则.
2646+
2647+ .. code-block:: python
2648+
2649+ config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
2650+ client = CosS3Client(config)
2651+ # 获取bucket标签
2652+ response = client.get_bucket_referer(
2653+ Bucket='bucket'
2654+ )
2655+ """
2656+ headers = mapped (kwargs )
2657+ params = {'referer' : '' }
2658+ url = self ._conf .uri (bucket = Bucket )
2659+ logger .info ("get bucket referer, url=:{url} ,headers=:{headers}" .format (
2660+ url = url ,
2661+ headers = headers ))
2662+ rt = self .send_request (
2663+ method = 'GET' ,
2664+ url = url ,
2665+ bucket = Bucket ,
2666+ auth = CosS3Auth (self ._conf , params = params ),
2667+ headers = headers ,
2668+ params = params )
2669+ data = xml_to_dict (rt .content )
2670+ if 'DomainList' in data :
2671+ format_dict (data ['DomainList' ], ['Domain' ])
2672+ return data
2673+
25762674 # service interface begin
25772675 def list_buckets (self , ** kwargs ):
25782676 """列出所有bucket
@@ -2589,7 +2687,9 @@ def list_buckets(self, **kwargs):
25892687 )
25902688 """
25912689 headers = mapped (kwargs )
2592- url = 'http://service.cos.myqcloud.com/'
2690+ url = 'https://service.cos.myqcloud.com/'
2691+ if self ._conf ._service_domain is not None :
2692+ url = 'https://{domain}/' .format (domain = self ._conf ._service_domain )
25932693 rt = self .send_request (
25942694 method = 'GET' ,
25952695 url = url ,
@@ -2808,7 +2908,7 @@ def _inner_head_object(self, CopySource):
28082908 params = {}
28092909 if versionid != '' :
28102910 params ['versionId' ] = versionid
2811- url = self ._conf .uri ( bucket = bucket , path = path , endpoint = endpoint )
2911+ url = u"{scheme}://{bucket}.{endpoint}/{path}" . format ( scheme = self ._conf ._scheme , bucket = bucket , endpoint = endpoint , path = path )
28122912 rt = self .send_request (
28132913 method = 'HEAD' ,
28142914 url = url ,
0 commit comments