Initialization
IAcsClient is the Java client of the AI Guardrails SDK for Java (aliyun-java-sdk-green). Before making API requests, create an IAcsClient instance and configure the required DefaultProfile settings.
Prerequisites
Before you begin, ensure that you have:
An Alibaba Cloud account with Content Moderation activated
An AccessKey ID and AccessKey secret — see Obtain an AccessKey pair
A supported region — see Endpoints
Only specific algorithm models are available in the Singapore, Germany (Frankfurt), and US (Virginia) regions.
Set up credentials
Store your AccessKey credentials as environment variables rather than hardcoding them in your application. The SDK supports two methods to read environment variables in Java:
| Method | AccessKey ID | AccessKey secret |
|---|---|---|
System.getenv() | System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") | System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") |
System.getProperty() | System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID") | System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET") |
Use a RAM user's AccessKey pair instead of your Alibaba Cloud account's root credentials to limit permissions and reduce security risk.
Create an IAcsClient instance for content moderation
Use the following code to create an IAcsClient instance for moderating images, audio, videos, and text:
// Use a RAM user's AccessKey pair to reduce security risk.
// Read credentials from environment variables — avoid hardcoding keys in source code.
//
// To read credentials:
// AccessKey ID: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
// or System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID")
// AccessKey secret: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// or System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
IAcsClient recognitionClient = new DefaultAcsClient(profile);Replace cn-shanghai with the region closest to your users. For available regions, see Endpoints.
Create an IAcsClient instance for custom library management
Use the following code to create an IAcsClient instance for managing custom image libraries, term libraries, and text pattern libraries:
// Use a RAM user's AccessKey pair to reduce security risk.
// Read credentials from environment variables — avoid hardcoding keys in source code.
//
// To read credentials:
// AccessKey ID: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
// or System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID")
// AccessKey secret: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// or System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
IAcsClient managementClient = new DefaultAcsClient(profile);Custom library management operations (managementClient) and content moderation operations (recognitionClient) are separate clients. Create both if your application uses both capabilities.