Add a person to a group

更新时间:
复制 MD 格式

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:

  1. Installed the Python dependencies for the SDK. For details, see Installation.

    Important

    Use the Python version specified in the Installation guide. Using an unsupported version causes operation calls to fail.

  2. Downloaded and imported the Extension.Uploader utility class into your project.

Usage notes

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:

PlaceholderDescriptionExample
<your-access-key-id>AccessKey ID of your RAM user, stored as an environment variableLTAI5tXxx
<your-access-key-secret>AccessKey secret of your RAM user, stored as an environment variablexXxXxXx
<person-id>The ID of the person to addpython_personId_test_1
<group-id>The ID of the target person groupnantuan
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:

FieldTypeDescription
codeintegerOuter status code. 200 indicates the request was accepted.
data.codeintegerInner status code. 200 indicates the person was added successfully.
data.personIdstringThe 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.