Skip to content

Commit 1d94dea

Browse files
authored
Merge branch 'master' into s3
2 parents 29efb60 + b7f1dba commit 1d94dea

File tree

5 files changed

+105
-3
lines changed

5 files changed

+105
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ install:
1010
- pip install nose
1111
- pip install pycodestyle
1212
- pip install dicttoxml
13+
notifications:
14+
email:
15+
recipients:
16+
- wjielai@tencent.com
17+
- fysntian@tencent.com
1318
script:
1419
- pycodestyle --max-line-length=200 qcloud_cos/.
1520
- nosetests -s -v ut/

qcloud_cos/cos_client.py

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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):

qcloud_cos/cos_comm.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,11 @@ def get_date(yy, mm, dd):
412412
date_str = datetime(yy, mm, dd).isoformat()
413413
final_date_str = date_str+'+08:00'
414414
return final_date_str
415+
416+
417+
class CiDetectType():
418+
"""ci内容设备的类型设置,可与操作设多个"""
419+
PORN = 1
420+
TERRORIST = 2
421+
POLITICS = 4
422+
ADS = 8

ut/test.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from qcloud_cos import CosConfig
1111
from qcloud_cos import CosServiceError
1212
from qcloud_cos import get_date
13+
from qcloud_cos.cos_comm import CiDetectType
1314

1415
SECRET_ID = os.environ["SECRET_ID"]
1516
SECRET_KEY = os.environ["SECRET_KEY"]
@@ -1152,6 +1153,18 @@ def test_select_object():
11521153
print(event)
11531154

11541155

1156+
def _test_get_object_sensitive_content_recognition():
1157+
"""测试ci文件内容识别的接口"""
1158+
print(CiDetectType)
1159+
response = client.get_object_sensitive_content_recognition(
1160+
Bucket=test_bucket,
1161+
Key=test_object,
1162+
DetectType=(CiDetectType.PORN | CiDetectType.TERRORIST | CiDetectType.POLITICS | CiDetectType.ADS)
1163+
)
1164+
print(response)
1165+
assert response
1166+
1167+
11551168
if __name__ == "__main__":
11561169
setUp()
11571170
"""
@@ -1175,6 +1188,8 @@ def test_select_object():
11751188
test_put_get_delete_bucket_inventory()
11761189
test_put_get_traffic_limit()
11771190
test_put_get_delete_bucket_domain()
1178-
"""
11791191
test_select_object()
1192+
_test_get_object_sensitive_content_recognition()
1193+
"""
1194+
11801195
tearDown()

0 commit comments

Comments
 (0)