本示例介绍如何使用Alibaba Cloud SDK for Java对OSS内容检测进行管理。

前提条件

在使用本教程之前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-green -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-green</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.61</version>
        </dependency>
    </dependencies>

代码示例

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20170823.*;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.ArrayList;
import java.util.List;

public class OssResultItems {

    // 以文件形式导出OSS违规检测结果
    private static void exportOssResult(IAcsClient client) {
        ExportOssResultRequest request = new ExportOssResultRequest();
        request.setResourceType("VIDEO");
        request.setScene("porn");
        request.setStock(false);
        request.setStartDate("2019-11-22 00:00:00 +0800");
        request.setEndDate("2019-11-28 15:00:53 +0800");
        try {
            ExportOssResultResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
    // 对OSS的审核结果进行标记
    private static void markOssResult(IAcsClient client) {
        MarkOssResultRequest request = new MarkOssResultRequest();
        request.setResourceType("VIDEO");
        request.setScene("terrorism");
        request.setStock(false);
        List<Long> ids = new ArrayList<Long>();
        ids.add(24930001L);
        request.setIds(JSON.toJSONString(ids));
        request.setOperation("ignore");
        try {
            MarkOssResultResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    // 获取OSS违规检测数据
    private static void describeOssResultItems(IAcsClient client) {
        DescribeOssResultItemsRequest request = new DescribeOssResultItemsRequest();
        request.setResourceType("VIDEO");
        request.setScene("porn");
        request.setStock(false);
        request.setStartDate("2019-11-22 00:00:00 +0800");
        request.setEndDate("2019-11-28 15:00:53 +0800");
        try {
            DescribeOssResultItemsResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    // 初始化公共请求参数
    public static IAcsClient createClient() {
        IClientProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 您的可用区ID
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }

    public static void main(String[] args) {
        IAcsClient client = createClient();
        // 获取OSS违规检测数据
        describeOssResultItems(client);
        // 对OSS的审核结果进行标记
        markOssResult(client);
        // 以文件形式导出OSS违规检测结果
        exportOssResult(client);
    }
}

执行结果

正确的返回结果类似如下:
{
    "currentPage":1,
    "pageSize":20,
    "queryId":"qid-7*******1rH6Dy",
    "requestId":"507FE30E-9DA5-41E9-84EF-E61C487D7E67",
    "scanResultList":[],
    "totalCount":0
}
{
    "requestId":"8C6C6D46-62AD-418C-97BA-AA40B483F3B0"
}
{
    "fileUrl":"https://a********f*******p0****3D",
    "requestId":"5AD89F41-4D16-44CE-887B-7C4E3E841B30"
}