OSS content moderation
This topic describes how to use the Java SDK to detect non-compliant content in OSS.
Prerequisites
Java dependencies are installed. For more information, see Installation.
NoteYou must use the Java 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 if you submit a local image or a binary image stream for image moderation.
Get OSS non-compliant detection data
Refer to the following code sample to retrieve OSS non-compliant detection data:
The following code is a simple example. For more information about API parameters, see OSS Content Moderation API.
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.green.model.v20170823.DescribeOssResultItemsRequest;
import com.aliyuncs.green.model.v20170823.DescribeOssResultItemsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class Main {
public static void main(String[] args) {
/**
* An Alibaba Cloud account AccessKey has full permissions on all APIs. For better security, use a RAM user to make API calls or perform O&M.
* Common methods to get environment variables:
* Method 1:
* Get the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* Method 2:
* Get the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
"Obtain the AccessKey ID of a RAM user from an environment variable",
"Obtain the AccessKey secret of a RAM user from an environment variable");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
DescribeOssResultItemsRequest describeKeywordLibRequest = new DescribeOssResultItemsRequest();
describeKeywordLibRequest.setResourceType("VIDEO");
describeKeywordLibRequest.setScene("porn");
describeKeywordLibRequest.setStock(false);
describeKeywordLibRequest.setStartDate("2018-12-07 00:00:00 +0800");
describeKeywordLibRequest.setEndDate("2018-12-13 15:00:53 +0800");
try {
DescribeOssResultItemsResponse describeOssResultItemsResponse = client.getAcsResponse(describeKeywordLibRequest);
// The total number of results.
describeOssResultItemsResponse.getTotalCount();
// The current page number.
describeOssResultItemsResponse.getCurrentPage();
// The number of entries per page.
describeOssResultItemsResponse.getPageSize();
// The list of scan results.
for (DescribeOssResultItemsResponse.ScanResult scanResult : describeOssResultItemsResponse.getScanResultList()) {
// The OSS bucket where the resource is stored.
scanResult.getBucket();
// The data ID.
scanResult.getDataId();
// The processing status of the resource.
scanResult.getHandleStatus();
// The unique ID of the scan result.
scanResult.getId();
// The URL of the original file.
scanResult.getNewUrl();
// The name of the OSS object.
scanResult.getObject();
// The time when the request was sent.
scanResult.getRequestTime();
// The status of the resource.
scanResult.getResourceStatus();
// The time when the scan was complete.
scanResult.getScanFinishedTime();
scanResult.getScore();
// The recommended subsequent operation.
scanResult.getSuggestion();
// The task ID.
scanResult.getTaskId();
// The URL of the thumbnail.
scanResult.getThumbnail();
// (For video objects only) The risky frames.
for (DescribeOssResultItemsResponse.ScanResult.FrameResult frameResult : scanResult.getFrameResults()) {
// The position of the frame, which is the offset from the beginning of the video in seconds.
frameResult.getOffset();
// The URL of the frame's thumbnail.
frameResult.getThumbnail();
// The URL of the frame.
frameResult.getUrl();
}
}
} catch (ClientException e) {
e.printStackTrace();
}
}
}Mark the moderation results for OSS
You can use this API to mark and manage the scan results for OSS. You can call this API to delete content, mark content as normal, or unfreeze content.
The following code is a simple example. For more information about API parameters, see OSS Content Moderation API.
import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.green.model.v20170823.MarkOssResultRequest;
import com.aliyuncs.green.model.v20170823.MarkOssResultResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
/**
* An Alibaba Cloud account AccessKey has full permissions on all APIs. For better security, use a RAM user to make API calls or perform O&M.
* Common methods to get environment variables:
* Method 1:
* Get the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* Method 2:
* Get the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
"Obtain the AccessKey ID of a RAM user from an environment variable",
"Obtain the AccessKey secret of a RAM user from an environment variable");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
MarkOssResultRequest markOssResultRequest = new MarkOssResultRequest();
markOssResultRequest.setResourceType("VIDEO");
markOssResultRequest.setScene("terrorism ");
markOssResultRequest.setStock(false);
markOssResultRequest.setIds(JSON.toJSONString(Arrays.asList(24930001L)));
markOssResultRequest.setOperation("ignore");
try {
MarkOssResultResponse markOssResultResponse = client.getAcsResponse(markOssResultRequest);
// The request ID. A success response indicates that the operation is successful.
String requestId = markOssResultResponse.getRequestId();
System.out.println("Mark success." + JSON.toJSONString(markOssResultResponse));
} catch (ClientException e) {
e.printStackTrace();
}
}
}Export OSS non-compliant detection results as a file
Refer to the following code sample to export OSS non-compliant detection results as a file:
The following code is a simple example. For more information about API parameters, see OSS Content Moderation API.
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.green.model.v20170823.ExportOssResultRequest;
import com.aliyuncs.green.model.v20170823.ExportOssResultResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class Main {
public static void main(String[] args) {
/**
* An Alibaba Cloud account AccessKey has full permissions on all APIs. For better security, use a RAM user to make API calls or perform O&M.
* Common methods to get environment variables:
* Method 1:
* Get the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* Method 2:
* Get the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Get the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
"Obtain the AccessKey ID of a RAM user from an environment variable",
"Obtain the AccessKey secret of a RAM user from an environment variable");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
ExportOssResultRequest exportOssResultRequest = new ExportOssResultRequest();
exportOssResultRequest.setResourceType("VIDEO");
exportOssResultRequest.setScene("porn");
exportOssResultRequest.setStock(false);
exportOssResultRequest.setStartDate("2018-12-07 00:00:00 +0800");
exportOssResultRequest.setEndDate("2018-12-13 15:00:53 +0800");
try {
ExportOssResultResponse exportOssResultResponse = client.getAcsResponse(exportOssResultRequest);
// The URL of the file.
String fileUrl = exportOssResultResponse.getFileUrl();
System.out.println("Export success. File URL: " + fileUrl);
} catch (ClientException e) {
e.printStackTrace();
}
}
}