@@ -39,7 +39,7 @@ class CosConfig(object):
3939 def __init__ (self , Appid = None , Region = None , SecretId = None , SecretKey = None , Token = None , Scheme = None , Timeout = None ,
4040 Access_id = None , Access_key = None , Secret_id = None , Secret_key = None , Endpoint = None , IP = None , Port = None ,
4141 Anonymous = None , UA = None , Proxies = None , Domain = None , ServiceDomain = None , PoolConnections = 10 ,
42- PoolMaxSize = 10 , AllowRedirects = False , SignHost = True , EndpointCi = None ):
42+ PoolMaxSize = 10 , AllowRedirects = False , SignHost = True , EndpointCi = None , EnableOldDomain = True , EnableInternalDomain = True ):
4343 """初始化,保存用户的信息
4444
4545 :param Appid(string): 用户APPID.
@@ -66,6 +66,8 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
6666 :param AllowRedirects(bool): 是否重定向
6767 :param SignHost(bool): 是否将host算入签名
6868 :param EndpointCi(string): ci的endpoint
69+ :param EnableOldDomain(bool): 是否使用旧的myqcloud.com域名访问COS
70+ :param EnableInternalDomain(bool): 是否使用内网域名访问COS
6971 """
7072 self ._appid = to_unicode (Appid )
7173 self ._token = to_unicode (Token )
@@ -85,9 +87,11 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
8587 self ._allow_redirects = AllowRedirects
8688 self ._sign_host = SignHost
8789 self ._copy_part_threshold_size = SINGLE_UPLOAD_LENGTH
90+ self ._enable_old_domain = EnableOldDomain
91+ self ._enable_internal_domain = EnableInternalDomain
8892
8993 if self ._domain is None :
90- self ._endpoint = format_endpoint (Endpoint , Region )
94+ self ._endpoint = format_endpoint (Endpoint , Region , u'cos.' , EnableOldDomain , EnableInternalDomain )
9195 if Scheme is None :
9296 Scheme = u'https'
9397 Scheme = to_unicode (Scheme )
@@ -96,7 +100,8 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
96100 self ._scheme = Scheme
97101
98102 # 格式化ci的endpoint 不支持自定义域名的
99- self ._endpoint_ci = format_endpoint (EndpointCi , Region , u'ci.' )
103+ # ci暂不支持新域名
104+ self ._endpoint_ci = format_endpoint (EndpointCi , Region , u'ci.' , True , False )
100105
101106 # 兼容(SecretId,SecretKey)以及(AccessId,AccessKey)
102107 if (SecretId and SecretKey ):
@@ -205,6 +210,8 @@ def convert_secret_value(self, value):
205210class CosS3Client (object ):
206211 """cos客户端类,封装相应请求"""
207212
213+ __built_in_sessions = None # 内置的静态连接池,多个Client间共享使用
214+
208215 def __init__ (self , conf , retry = 1 , session = None ):
209216 """初始化client对象
210217
@@ -214,15 +221,48 @@ def __init__(self, conf, retry=1, session=None):
214221 """
215222 self ._conf = conf
216223 self ._retry = retry # 重试的次数,分片上传时可适当增大
224+
225+ if not CosS3Client .__built_in_sessions :
226+ with threading .Lock ():
227+ if not CosS3Client .__built_in_sessions : # 加锁后double check
228+ CosS3Client .__built_in_sessions = self .generate_built_in_connection_pool (self ._conf ._pool_connections , self ._conf ._pool_maxsize )
229+
217230 if session is None :
218- self ._session = requests .session ()
219- self ._session .mount ('http://' , requests .adapters .HTTPAdapter (pool_connections = self ._conf ._pool_connections ,
220- pool_maxsize = self ._conf ._pool_maxsize ))
221- self ._session .mount ('https://' , requests .adapters .HTTPAdapter (pool_connections = self ._conf ._pool_connections ,
222- pool_maxsize = self ._conf ._pool_maxsize ))
231+ self ._session = CosS3Client .__built_in_sessions
223232 else :
224233 self ._session = session
225234
235+ def set_built_in_connection_pool_max_size (self , PoolConnections , PoolMaxSize ):
236+ """设置SDK内置的连接池的连接大小,并且重新绑定到client中"""
237+ if not CosS3Client .__built_in_sessions :
238+ return
239+
240+ if CosS3Client .__built_in_sessions .get_adapter ('http://' )._pool_connections == PoolConnections \
241+ and CosS3Client .__built_in_sessions .get_adapter ('http://' )._pool_maxsize == PoolMaxSize :
242+ return
243+
244+ # 判断之前是否绑定到内置连接池
245+ rebound = False
246+ if self ._session and self ._session is CosS3Client .__built_in_sessions :
247+ rebound = True
248+
249+ # 重新生成内置连接池
250+ CosS3Client .__built_in_sessions .close ()
251+ CosS3Client .__built_in_sessions = self .generate_built_in_connection_pool (PoolConnections , PoolMaxSize )
252+
253+ # 重新绑定到内置连接池
254+ if rebound :
255+ self ._session = CosS3Client .__built_in_sessions
256+ logger .warn ("rebound built-in connection pool success. maxsize=%d,%d" % (PoolConnections , PoolMaxSize ))
257+
258+ def generate_built_in_connection_pool (self , PoolConnections , PoolMaxSize ):
259+ """生成SDK内置的连接池,此连接池是client间共用的"""
260+ built_in_sessions = requests .session ()
261+ built_in_sessions .mount ('http://' , requests .adapters .HTTPAdapter (pool_connections = PoolConnections , pool_maxsize = PoolMaxSize ))
262+ built_in_sessions .mount ('https://' , requests .adapters .HTTPAdapter (pool_connections = PoolConnections , pool_maxsize = PoolMaxSize ))
263+ logger .warn ("generate built-in connection pool success. maxsize=%d,%d" % (PoolConnections , PoolMaxSize ))
264+ return built_in_sessions
265+
226266 def get_conf (self ):
227267 """获取配置"""
228268 return self ._conf
@@ -778,7 +818,7 @@ def copy_object(self, Bucket, Key, CopySource, CopyStatus='Copy', **kwargs):
778818 )
779819 """
780820 headers = mapped (kwargs )
781- headers ['x-cos-copy-source' ] = gen_copy_source_url (CopySource )
821+ headers ['x-cos-copy-source' ] = gen_copy_source_url (CopySource , self . _conf . _enable_old_domain , self . _conf . _enable_internal_domain )
782822 if CopyStatus != 'Copy' and CopyStatus != 'Replaced' :
783823 raise CosClientError ('CopyStatus must be Copy or Replaced' )
784824 headers ['x-cos-metadata-directive' ] = CopyStatus
@@ -827,7 +867,7 @@ def upload_part_copy(self, Bucket, Key, PartNumber, UploadId, CopySource, CopySo
827867 )
828868 """
829869 headers = mapped (kwargs )
830- headers ['x-cos-copy-source' ] = gen_copy_source_url (CopySource )
870+ headers ['x-cos-copy-source' ] = gen_copy_source_url (CopySource , self . _conf . _enable_old_domain , self . _conf . _enable_internal_domain )
831871 headers ['x-cos-copy-source-range' ] = CopySourceRange
832872 params = {'partNumber' : PartNumber , 'uploadId' : UploadId }
833873 params = format_values (params )
@@ -3199,7 +3239,12 @@ def list_buckets(self, **kwargs):
31993239 response = client.list_buckets()
32003240 """
32013241 headers = mapped (kwargs )
3202- url = '{scheme}://service.cos.myqcloud.com/' .format (scheme = self ._conf ._scheme )
3242+
3243+ if self ._conf ._enable_old_domain :
3244+ url = '{scheme}://service.cos.myqcloud.com/' .format (scheme = self ._conf ._scheme )
3245+ else :
3246+ url = '{scheme}://service.cos.tencentcos.cn/' .format (scheme = self ._conf ._scheme )
3247+
32033248 if self ._conf ._service_domain is not None :
32043249 url = '{scheme}://{domain}/' .format (scheme = self ._conf ._scheme , domain = self ._conf ._service_domain )
32053250 rt = self .send_request (
@@ -3476,7 +3521,7 @@ def upload_file(self, Bucket, Key, LocalFilePath, PartSize=1, MAXThread=5, Enabl
34763521
34773522 def _head_object_when_copy (self , CopySource , ** kwargs ):
34783523 """查询源文件的长度"""
3479- bucket , path , endpoint , versionid = get_copy_source_info (CopySource )
3524+ bucket , path , endpoint , versionid = get_copy_source_info (CopySource , self . _conf . _enable_old_domain , self . _conf . _enable_internal_domain )
34803525 params = {}
34813526 if versionid != '' :
34823527 params ['versionId' ] = versionid
@@ -3521,7 +3566,7 @@ def _upload_part_copy(self, bucket, key, part_number, upload_id, copy_source, co
35213566 return None
35223567
35233568 def _check_same_region (self , dst_endpoint , CopySource ):
3524- src_endpoint = get_copy_source_info (CopySource )[2 ]
3569+ src_endpoint = get_copy_source_info (CopySource , self . _conf . _enable_old_domain , self . _conf . _enable_internal_domain )[2 ]
35253570 if src_endpoint == dst_endpoint :
35263571 return True
35273572 return False
@@ -5244,7 +5289,7 @@ def ci_auditing_image_batch(self, Bucket, Input, DetectType=None, BizType=None,
52445289 :param Input(dict array): 需要审核的图片信息,每个array元素为dict类型,支持的参数如下:
52455290 Object: 存储在 COS 存储桶中的图片文件名称,例如在目录 test 中的文件 image.jpg,则文件名称为 test/image.jpg。
52465291 Object 和 Url 只能选择其中一种。
5247- Url: 图片文件的链接地址,例如 http://a-1250000.cos.ap-shanghai.myqcloud.com /image.jpg。
5292+ Url: 图片文件的链接地址,例如 http://a-1250000.cos.ap-shanghai.tencentcos.cn /image.jpg。
52485293 Object 和 Url 只能选择其中一种。
52495294 Interval: 截帧频率,GIF 图检测专用,默认值为5,表示从第一帧(包含)开始每隔5帧截取一帧
52505295 MaxFrames: 最大截帧数量,GIF 图检测专用,默认值为5,表示只截取 GIF 的5帧图片进行审核,必须大于0
0 commit comments