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
Responses
| 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() Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot