@@ -256,6 +256,8 @@ def send_request(self, method, url, bucket, timeout=30, **kwargs):
256256 res = self ._session .head (url , timeout = timeout , proxies = self ._conf ._proxies , ** kwargs )
257257 if res .status_code < 400 : # 2xx和3xx都认为是成功的
258258 return res
259+ elif res .status_code < 500 : # 4xx 不重试
260+ break
259261 except Exception as e : # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误
260262 logger .exception ('url:%s, retry_time:%d exception:%s' % (url , j , str (e )))
261263 if j < self ._retry :
@@ -381,6 +383,78 @@ def get_object(self, Bucket, Key, **kwargs):
381383
382384 return response
383385
386+ def get_object_sensitive_content_recognition (self , Bucket , Key , DetectType , ** kwargs ):
387+ """文件内容识别接口 https://cloud.tencent.com/document/product/460/37318
388+
389+ :param Bucket(string): 存储桶名称.
390+ :param Key(string): COS路径.
391+ :param DetectType(int): 内容识别标志,位计算 1:porn, 2:terrorist, 4:politics, 8:ads
392+ :param kwargs(dict): 设置下载的headers.
393+ :return(dict): 下载成功返回的结果,dict类型.
394+
395+ .. code-block:: python
396+
397+ config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
398+ client = CosS3Client(config)
399+ # 下载cos上的文件到本地
400+ response = client.get_object_sensitive_content_recognition(
401+ Bucket='bucket',
402+ DetectType=CiDetectType.PORN | CiDetectType.POLITICS,
403+ Key='test.png'
404+ )
405+ print response
406+ """
407+ headers = mapped (kwargs )
408+ final_headers = {}
409+ params = {}
410+ for key in headers :
411+ if key .startswith ("response" ):
412+ params [key ] = headers [key ]
413+ else :
414+ final_headers [key ] = headers [key ]
415+ headers = final_headers
416+
417+ if 'versionId' in headers :
418+ params ['versionId' ] = headers ['versionId' ]
419+ del headers ['versionId' ]
420+ params ['ci-process' ] = 'sensitive-content-recognition'
421+ detect_type = ''
422+ if DetectType & CiDetectType .PORN > 0 :
423+ detect_type += 'porn'
424+ if DetectType & CiDetectType .TERRORIST > 0 :
425+ if len (detect_type ) > 0 :
426+ detect_type += ','
427+ detect_type += 'terrorist'
428+ if DetectType & CiDetectType .POLITICS > 0 :
429+ if len (detect_type ) > 0 :
430+ detect_type += ','
431+ detect_type += 'politics'
432+ if DetectType & CiDetectType .ADS > 0 :
433+ if len (detect_type ) > 0 :
434+ detect_type += ','
435+ detect_type += 'ads'
436+
437+ params ['detect-type' ] = detect_type
438+ params = format_values (params )
439+
440+ url = self ._conf .uri (bucket = Bucket , path = Key )
441+ logger .info ("get object sensitive content recognition, url=:{url} ,headers=:{headers}, params=:{params}" .format (
442+ url = url ,
443+ headers = headers ,
444+ params = params ))
445+ rt = self .send_request (
446+ method = 'GET' ,
447+ url = url ,
448+ bucket = Bucket ,
449+ stream = True ,
450+ auth = CosS3Auth (self ._conf , Key , params = params ),
451+ params = params ,
452+ headers = headers )
453+
454+ data = xml_to_dict (rt .content )
455+
456+ return data
457+
384458 def get_presigned_url (self , Bucket , Key , Method , Expired = 300 , Params = {}, Headers = {}):
385459 """生成预签名的url
386460
@@ -732,8 +806,7 @@ def upload_part(self, Bucket, Key, Body, PartNumber, UploadId, EnableMD5=False,
732806 params = params ,
733807 auth = CosS3Auth (self ._conf , Key , params = params ),
734808 data = Body )
735- response = dict ()
736- response ['ETag' ] = rt .headers ['ETag' ]
809+ response = dict (** rt .headers )
737810 return response
738811
739812 def complete_multipart_upload (self , Bucket , Key , UploadId , MultipartUpload = {}, ** kwargs ):
0 commit comments