This topic describes how to use the Python software development kit (SDK) to detect violations in OSS.
Prerequisites
Python dependencies are installed. For more information, see Installation.
NoteYou must use the required Python version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
The Extension.Uploader utility class is downloaded and imported into your project.
Query violation data in OSS
The following code provides an example. For more information about the API parameters, see OSS Content Moderation API.
#coding=utf-8
from aliyunsdkcore import client
from aliyunsdkgreen.request.v20170823 import DescribeOssResultItemsRequest
# Note: Reuse the instantiated client to improve detection performance and avoid repeated connections.
# Common methods to obtain environment variables:
# Obtain the AccessKey ID of the RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of the RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("Get the AccessKey ID of the RAM user from an environment variable", "Get the AccessKey secret of the RAM user from an environment variable", "cn-shanghai")
# Create a new request for each call. Do not reuse request objects.
request = DescribeOssResultItemsRequest.DescribeOssResultItemsRequest()
request.set_ResourceType("VIDEO")
request.set_Scene("porn")
request.set_Stock("false")
request.set_StartDate("2019-01-02 00:00:00 +0800")
request.set_EndDate("2019-01-07 15:00:53 +0800")
response = clt.do_action_with_exception(request)
print(response)Mark the moderation results for OSS
You can call this API to delete, mark as normal and ignore, or unfreeze content for which moderation results are available.
The following code provides an example. For more information about the API parameters, see OSS Content Moderation API.
# Note: To improve detection performance, reuse the instantiated client whenever possible. Avoid repeatedly establishing connections.
# Common methods for retrieving environment variables:
# RAM user's AccessKey ID: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# RAM user's AccessKey secret: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
#coding=utf-8
from aliyunsdkcore import client
from aliyunsdkgreen.request.v20170823 import MarkOssResultRequest
import json
# Note: Reuse the instantiated client to improve detection performance and avoid repeated connections.
# Common methods to obtain environment variables:
# Obtain the AccessKey ID of the RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of the RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("Get the AccessKey ID of the RAM user from an environment variable", "Get the AccessKey secret of the RAM user from an environment variable", "cn-shanghai")
# Create a new request for each call. Do not reuse request objects.
request = MarkOssResultRequest.MarkOssResultRequest()
request.set_ResourceType("VIDEO")
request.set_Scene("porn")
request.set_Stock("false")
request.set_Ids(json.dumps([10001]))
request.set_Operation("ignore")
response = clt.do_action_with_exception(request)
print(response)Export OSS violation detection results as a TXT file
A maximum of 5,000 records can be exported at a time. The exported file is stored in TXT format, with one record per line. Each record includes the following information: the name of the scanned OSS bucket, the scanned OSS file (key), the scan score, and the suggested action.
The following code provides an example. For more information about the API parameters, see OSS Content Moderation API documentation.
#coding=utf-8
from aliyunsdkcore import client
from aliyunsdkgreen.request.v20170823 import ExportOssResultRequest
import json
# Note: Reuse the instantiated client to improve detection performance and avoid repeated connections.
# Common methods to obtain environment variables:
# Obtain the AccessKey ID of the RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of the RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("Get the AccessKey ID of the RAM user from an environment variable", "Get the AccessKey secret of the RAM user from an environment variable", "cn-shanghai")
# Create a new request for each call. Do not reuse request objects.
request = ExportOssResultRequest.ExportOssResultRequest()
request.set_ResourceType("VIDEO")
request.set_Scene("porn")
request.set_Stock("false")
request.set_StartDate("2019-01-02 00:00:00 +0800")
request.set_EndDate("2019-01-07 15:00:53 +0800")
response = clt.do_action_with_exception(request)
print(response)