Updated on 2026-08-14 GMT+08:00

Adding Tags to an Object

Function

This API is used to add tags to or update tags of an object. An object tag is a key-value pair.

If there is no object version ID (indicated by versionId) contained in the request, you must have the PutObjectTagging permission. If there is an object version ID (indicated by versionId) contained in the request, you must have both the PutObjectTagging and PutObjectVersionTagging permissions. By default, only the object owner can perform this operation. The object owner can grant this permission to others by using a bucket or user policy.

Tags are added to the current version of an object by default. You can use the versionId parameter to add tags to any other version. If the version is a delete marker, OBS returns 404 Not Found.

Restrictions

  • To add tags to an object, you must be the bucket owner or have the required permissions (obs:object:PutObjectTagging (versioning disabled) and obs:object:PutObjectVersionTagging (versioning enabled or suspended) granted using IAM or obs:object:PutObjectTagging (versioning disabled) and obs:object:PutObjectVersionTagging (versioning enabled or suspended) granted using a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
  • The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
  • This API is not available for parallel file systems.
  • Each object can have up to 10 tags.

Method

ObsClient.setObjectTagging(params)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

String

Yes

Explanation:

Bucket name

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain a period (.) and a hyphen (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets with the same name in the same region, no error will be reported and the bucket properties comply with those set in the first creation request.

Default value:

None

Key

String

Yes

Explanation:

Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path of the object that does not contain the bucket name.

For example, if the address for accessing the object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt.

Value range:

The value must contain 1 to 1,024 characters.

Default value:

None

VersionId

String

No

Explanation:

Object version ID, for example, G001117FCE89978B0000401205D5DC9A

Value range:

The value must contain 32 characters.

Default value:

None

Tags

Array

Yes

Explanation:

Tag list, for example, [{ Key: 'key1', Value: 'value1' }, { Key: 'key2', Value: 'value2' }]

Restrictions:

  • Constraints on tag key-value pairs:

    A tag key is case-sensitive and must be unique. It cannot be left blank or exceed 128 characters. The following characters are not allowed: ,/|<>=*\

    A tag value is case-sensitive and can be left blank. It cannot exceed 255 characters. The following characters are not allowed: ,/|<>=*\

Default value:

None

Responses

Table 2 ICommonMsg

Parameter

Type

Description

Status

Number

Explanation:

HTTP status code returned by the OBS server

Value range:

A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For details, see Status Codes.

Code

String

Explanation:

Error code returned by the OBS server

Message

String

Explanation:

Error description returned by the OBS server

HostId

String

Explanation:

Request server ID returned by the OBS server

RequestId

String

Explanation:

Request ID returned by the OBS server

Id2

String

Explanation:

Request ID2 returned by the OBS server

Indicator

String

Explanation:

Error code details returned by the OBS server

Sample Code

This example adds tags to object objectname.

// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
const AK = process.env.AccessKeyID
const SK = process.env.SecretAccessKey
// (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them using environment variables.
const security_token= process.env.SecurityToken
// Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
const server = "https://obs.ap-southeast-1.myhuaweicloud.com"

// Create an ObsClient instance.
var obsClient = new ObsClient({
    access_key_id: AK,
    secret_access_key: SK,
    server: server
});

async function setObjectTagging() {
    try {
        const params = {
            Bucket: 'examplebucket',
            Key: 'objectname',
            // Specify an object version when versioning is enabled.
            // VersionId: '',
            // List of object tags
            Tags: [
                { Key: 'key1', Value: 'value1' },
                { Key: 'key2', Value: 'value2' }
            ]
        };
        // Add tags to the object.
        const result = await obsClient.setObjectTagging(params);
        if (result.CommonMsg.Status <= 300) {
            console.log("Set object(%s)'s tagging successful!", params.Key);
            console.log("RequestId: %s", result.CommonMsg.RequestId);
            return;
        };
        console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
        console.log("Status: %d", result.CommonMsg.Status);
        console.log("Code: %s", result.CommonMsg.Code);
        console.log("Message: %s", result.CommonMsg.Message);
        console.log("RequestId: %s", result.CommonMsg.RequestId);
    } catch (error) {
        console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
        console.log(error);
    };
};

setObjectTagging()