File moderation
This topic describes how to use the Go software development kit (SDK) file moderation API to moderate text and images in files.
Features
File moderation supports only asynchronous detection tasks, which do not return results in real time. For more information about the parameters, see Asynchronous file detection.
-
The SDK supports only publicly accessible file URLs. It does not support local files or binary data.
-
Supported URL types:
-
Public HTTP/HTTPS URLs that do not exceed 2048 characters in length.
-
Alibaba Cloud OSS URLs:
oss://<bucket-name>.<endpoint>/<object-name>. You must grant AI Guardrails permissions to access the destination OSS bucket. The bucket must be in the same region as the AI Guardrails service. For more information, see Grant AI Guardrails permissions to access OSS buckets.
-
Prerequisites
Before you begin, make sure you have:
Installed Go dependencies. See Installation for the required Go version. Using an unsupported version causes subsequent operation calls to fail.
A RAM user AccessKey ID and AccessKey secret stored as environment variables:
ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET
Submit an asynchronous file detection task
|
API |
Description |
Supported regions |
|
FileAsyncScanV2Request |
Submits an asynchronous file detection task to detect text and images in a file. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: To improve detection performance, reuse the instantiated client. This avoids repeatedly establishing connections.
* Common methods to obtain environment variables:
* Obtain the AccessKey ID of the Resource Access Management (RAM) user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of the RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"Obtain the AccessKey ID of the RAM user from an environment variable.",
"Obtain the AccessKey secret of the RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
task := map[string]interface{}{
"dataId": "The data ID", "url": "The URL of the file to be detected",
}
// scenes: The detection scenarios. You can specify multiple scenarios.
content, _ := json.Marshal(
map[string]interface{}{
"textScenes": [...]string{"antispam"}, "imageScenes": [...]string{"porn", "terrorism"},
"tasks": [...]map[string]interface{}{task}, "bizType": "The business scenario",
},
)
request := green.CreateFileAsyncScanRequest()
request.SetContent(content)
response, _err := client.FileAsyncScan(request)
if _err != nil {
fmt.Println(_err.Error())
return
}
if response.GetHttpStatus() != 200 {
fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
}
fmt.Println(response.GetHttpContentString())
}
Query the results of an asynchronous file detection task
|
API |
Description |
Supported regions |
|
FileAsyncScanResultsRequest |
Queries the results of an asynchronous file detection task. |
|
Sample code
package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
"strconv"
)
func main() {
/**
* Note: To improve detection performance, reuse the instantiated client. This avoids repeatedly establishing connections.
* Common methods to obtain environment variables:
* Obtain the AccessKey ID of the Resource Access Management (RAM) user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain the AccessKey secret of the RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
client, _err := green.NewClientWithAccessKey(
"cn-shanghai",
"Obtain the AccessKey ID of the RAM user from an environment variable.",
"Obtain the AccessKey secret of the RAM user from an environment variable.")
if err != nil {
fmt.Println(err.Error())
return
}
content, _ := json.Marshal([...]string{"The ID of the asynchronous file detection task"})
request := green.CreateFileAsyncScanResultsRequest()
request.SetContent(content)
response, _err := client.FileAsyncScanResults(request)
if _err != nil {
fmt.Println(_err.Error())
return
}
if response.GetHttpStatus() != 200 {
fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
}
fmt.Println(response.GetHttpContentString())
}