Skip to content

Commit

Permalink
suppoert ci sensitive-content-recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
degangliu committed Apr 24, 2020
1 parent 00f3b2a commit f8afffe
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
72 changes: 72 additions & 0 deletions qcloud_cos/cos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,78 @@ def get_object(self, Bucket, Key, **kwargs):

return response

def get_object_sensitive_content_recognition(self, Bucket, Key, DetectType, **kwargs):
"""文件内容识别接口 https://cloud.tencent.com/document/product/460/37318
:param Bucket(string): 存储桶名称.
:param Key(string): COS路径.
:param DetectType(int): 内容识别标志,位计算 1:porn, 2:terrorist, 4:politics, 8:ads
:param kwargs(dict): 设置下载的headers.
:return(dict): 下载成功返回的结果,dict类型.
.. code-block:: python
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
client = CosS3Client(config)
# 下载cos上的文件到本地
response = client.get_object_sensitive_content_recognition(
Bucket='bucket',
DetectType=CiDetectType.PORN | CiDetectType.POLITICS,
Key='test.png'
)
print response
"""
headers = mapped(kwargs)
final_headers = {}
params = {}
for key in headers:
if key.startswith("response"):
params[key] = headers[key]
else:
final_headers[key] = headers[key]
headers = final_headers

if 'versionId' in headers:
params['versionId'] = headers['versionId']
del headers['versionId']
params['ci-process'] = 'sensitive-content-recognition'
detect_type = ''
if DetectType & CiDetectType.PORN > 0 :
detect_type += 'porn'
if DetectType & CiDetectType.TERRORIST > 0 :
if len(detect_type) > 0:
detect_type += ','
detect_type += 'terrorist'
if DetectType & CiDetectType.POLITICS > 0 :
if len(detect_type) > 0:
detect_type += ','
detect_type += 'politics'
if DetectType & CiDetectType.ADS > 0 :
if len(detect_type) > 0:
detect_type += ','
detect_type += 'ads'

params['detect-type'] = detect_type
params = format_values(params)

url = self._conf.uri(bucket=Bucket, path=Key)
logger.info("get object sensitive content recognition, url=:{url} ,headers=:{headers}, params=:{params}".format(
url=url,
headers=headers,
params=params))
rt = self.send_request(
method='GET',
url=url,
bucket=Bucket,
stream=True,
auth=CosS3Auth(self._conf, Key, params=params),
params=params,
headers=headers)

data = xml_to_dict(rt.content)

return data

def get_presigned_url(self, Bucket, Key, Method, Expired=300, Params={}, Headers={}):
"""生成预签名的url
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests>=2.8
dicttoxml
six
enum
16 changes: 15 additions & 1 deletion ut/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from qcloud_cos import CosConfig
from qcloud_cos import CosServiceError
from qcloud_cos import get_date
from qcloud_cos.cos_comm import CiDetectType

SECRET_ID = os.environ["SECRET_ID"]
SECRET_KEY = os.environ["SECRET_KEY"]
Expand All @@ -19,6 +20,7 @@
test_bucket = 'cos-python-v5-test-' + str(sys.version_info[0]) + '-' + str(sys.version_info[1]) + '-' + REGION + '-' + APPID
copy_test_bucket = 'copy-' + test_bucket
test_object = "test.txt"
test_image = "12.png"
special_file_name = "中文" + "→↓←→↖↗↙↘! \"#$%&'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
conf = CosConfig(
Region=REGION,
Expand Down Expand Up @@ -1151,6 +1153,16 @@ def test_select_object():
for event in event_stream:
print(event)

def test_get_object_sensitive_content_recognition():
"""测试ci文件内容识别的接口"""
response = client.get_object_sensitive_content_recognition(
Bucket=test_bucket,
Key=test_image,
DetectType=CiDetectType.PORN | CiDetectType.TERRORIST | CiDetectType.POLITICS | CiDetectType.ADS
)
print response
print response['PornInfo']
assert response

if __name__ == "__main__":
setUp()
Expand All @@ -1175,6 +1187,8 @@ def test_select_object():
test_put_get_delete_bucket_inventory()
test_put_get_traffic_limit()
test_put_get_delete_bucket_domain()
"""
test_select_object()
"""

test_get_object_sensitive_content_recognition()
tearDown()

0 comments on commit f8afffe

Please sign in to comment.