自定义图库

本文介绍了如何使用.NET SDK管理自定义图库。

功能描述

您可以自定义智能鉴黄、暴恐涉政识别、图片或视频广告的图片样本,满足个性化内容管控需求。关于参数的详细信息,请参见创建图库API文档

您需要使用内容安全的API接入地址,调用本SDK接口。关于API接入地址的信息,请参见接入地址(Endpoint)

前提条件

已安装.NET依赖。关于安装.NET依赖的具体操作,请参见安装.NET依赖

说明

请一定按照安装.NET依赖页面中的版本安装,否则会导致调用失败。

查询自定义图库列表

您可以使用以下代码查询用户图库列表(包括用户自定义的图库列表和系统回流图库):

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DescribeImageLibRequest describeImageLibRequest = new DescribeImageLibRequest();
            describeImageLibRequest.ServiceModule = "open_api";
            try
            {
                // 获取所有的图库,包括自定义的图库以及回流图库。
                DescribeImageLibResponse describeImageLibResponse = client.GetAcsResponse(describeImageLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(describeImageLibResponse.HttpResponse.Content));

                List<DescribeImageLibResponse.DescribeImageLib_ImageLib> allLibs = describeImageLibResponse.ImageLibList;
                List<DescribeImageLibResponse.DescribeImageLib_ImageLib> customImageLibs = new List<DescribeImageLibResponse.DescribeImageLib_ImageLib>();
                foreach (DescribeImageLibResponse.DescribeImageLib_ImageLib imageLib in allLibs)
                {
                    String source = imageLib.Source;
                    // 过滤出用户自定义的图库。
                    if ("MANUAL".Equals(source))
                    {
                        customImageLibs.Add(imageLib);
                    }
                    // 过滤出系统回流(反馈)的图库。
                    if ("FEEDBACK".Equals(source))
                    {
                        customImageLibs.Add(imageLib);
                    }
                    Console.WriteLine(JsonConvert.SerializeObject(customImageLibs));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

创建自定义图库

您可以使用以下代码创建自定义图库:

说明

请根据您所需要的业务场景设置不同的参数。

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            CreateImageLibRequest createImageLibRequest = new CreateImageLibRequest();
            createImageLibRequest.ServiceModule = "open_api";
            createImageLibRequest.Name = "鉴黄黑库";
            createImageLibRequest.Scene = "PORN";
            createImageLibRequest.Category = "BLACK";

            try
            {
                CreateImageLibResponse createImageLibResponse = client.GetAcsResponse(createImageLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(createImageLibResponse.HttpResponse.Content));

            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

修改自定义图库

您可以使用以下代码修改自定义图库的名称及其适用的业务场景(BizType):

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            UpdateImageLibRequest updateImageLibRequest = new UpdateImageLibRequest();
            // 图库ID。
            updateImageLibRequest.Id = 图库ID;
            updateImageLibRequest.Name = "鉴黄黑库改名";
            updateImageLibRequest.BizTypes = JsonConvert.SerializeObject(new List<string> { "comment" });
            updateImageLibRequest.Category = "WHITE";
            updateImageLibRequest.Scene = "PORN";
            try
            {
                UpdateImageLibResponse updateImageLibResponse = client.GetAcsResponse(updateImageLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(updateImageLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

删除自定义图库

您可以使用以下代码删除自定义图库:

重要

删除自定义图库时,图库下的所有图片也将被删除。

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DeleteImageLibRequest deleteImageLibRequest = new DeleteImageLibRequest();
            // 图库ID。
            deleteImageLibRequest.Id = 图库ID;
            try
            {
                DeleteImageLibResponse deleteImageLibResponse = client.GetAcsResponse(deleteImageLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(deleteImageLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

查询自定义图库图片列表

您可以使用以下代码查询自定义图库中所有已添加的图片列表:

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DescribeImageFromLibRequest describeImageFromLibRequest = new DescribeImageFromLibRequest();
            describeImageFromLibRequest.ImageLibId = 图库ID;
            describeImageFromLibRequest.PageSize = 20;
            describeImageFromLibRequest.CurrentPage = 1;
            try
            {
                DescribeImageFromLibResponse describeImageFromLibResponse = client.GetAcsResponse(describeImageFromLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(describeImageFromLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}

删除自定义图片

您可以使用以下代码删除自定义图库中的多张自定义图片:

using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20170823;
using System.Collections.Generic;

namespace csharp_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 常见获取环境变量方式:
             *     获取RAM用户AccessKey ID:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.GetProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            DeleteImageFromLibRequest deleteImageFromLibRequest = new DeleteImageFromLibRequest();
            deleteImageFromLibRequest.Ids = JsonConvert.SerializeObject(new List<int> { 图片ID });
            try
            {
                DeleteImageFromLibResponse deleteImageFromLibResponse = client.GetAcsResponse(deleteImageFromLibRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(deleteImageFromLibResponse.HttpResponse.Content));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
    }
}