Use the Content Moderation Python SDK to add an existing person to one or more person groups.
Prerequisites
Before you begin, ensure that you have:
Installed the Python dependencies for the SDK. For details, see Installation.
ImportantUse the Python version specified in the Installation guide. Using an unsupported version causes operation calls to fail.
Downloaded and imported the Extension.Uploader utility class into your project.
Usage notes
A person can belong to one or more person groups.
Use Content Moderation API endpoints when calling this SDK. For the list of endpoints, see Endpoints.
For full parameter details, see API operation for adding a person to a group.
Add a person to a group
The following example adds a person to a specified group using AddGroupsRequest. Before running the code, replace the placeholder values shown in the table:
| Placeholder | Description | Example |
|---|---|---|
<your-access-key-id> | AccessKey ID of your RAM user, stored as an environment variable | LTAI5tXxx |
<your-access-key-secret> | AccessKey secret of your RAM user, stored as an environment variable | xXxXxXx |
<person-id> | The ID of the person to add | python_personId_test_1 |
<group-id> | The ID of the target person group | nantuan |
Reuse the instantiated client across requests to improve performance and avoid repeated connection overhead.
# coding=utf-8
import os
import json
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import AddGroupsRequest
# Initialize the client with credentials from environment variables
access_key_id = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
access_key_secret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient(access_key_id, access_key_secret, "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# Build the request
request = AddGroupsRequest.AddGroupsRequest()
request.set_accept_format('JSON')
request.set_content(
bytearray(
json.dumps({"personId": "<person-id>", "groupIds": ["<group-id>"]}),
"utf-8"
)
)
# Send the request
response = clt.do_action_with_exception(request)
result = json.loads(response)
# Check the result
if result["code"] == 200:
result_object = result["data"]
if result_object["code"] == 200:
# The person was added successfully
person_id = result_object["personId"]
print(person_id)Response
A successful response returns two nested status codes and the person ID:
| Field | Type | Description |
|---|---|---|
code | integer | Outer status code. 200 indicates the request was accepted. |
data.code | integer | Inner status code. 200 indicates the person was added successfully. |
data.personId | string | The ID of the person that was added. |
What's next
To search for a person within a group, see the face search API reference.
To manage groups (create or delete), see the person group management operations.