Use object tagging to classify objects in a versioning-enabled bucket. Each tag is a key-value pair attached to a specific object version.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with versioning enabled
The
oss:PutObjectTaggingpermission — see Grant custom permissions to a RAM userThe version ID of the object you want to tag — see List objects
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou) and the public endpoint by default. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For supported regions and endpoints, see OSS regions and endpoints.In a versioning-enabled bucket, specify a version ID to tag a particular object version.
Tag a specific object version
The following example tags a specific version of an object by passing a versionId in the PutObjectTaggingRequest. All tags are submitted as a tag set containing one or more key-value pairs.
<?php
// Import the autoloader to load SDK dependencies.
require_once __DIR__ . '/../../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define command-line arguments.
$optsdesc = [
"region" => ['help' => 'The region where the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The endpoint used to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket.', 'required' => True],
"key" => ['help' => 'The name of the object.', 'required' => True],
];
$longopts = \array_map(function ($key) { return "$key:"; }, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
echo "Error: --$key is required. " . $value['help'] . PHP_EOL;
exit(1);
}
}
$region = $options["region"];
$bucket = $options["bucket"];
$key = $options["key"];
// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Initialize the OSS client.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
$client = new Oss\Client($cfg);
// Build the request: attach two tags to a specific object version.
$request = new Oss\Models\PutObjectTaggingRequest(
bucket: $bucket,
key: $key,
versionId: "yourVersionId", // Replace with the actual version ID.
tagging: new Oss\Models\Tagging(
tagSet: new Oss\Models\TagSet(
tags: [
new Oss\Models\Tag('k1', 'v1'),
new Oss\Models\Tag('k2', 'v2'),
]
)
)
);
// Apply the tags.
$result = $client->putObjectTagging($request);
// Print the response.
printf(
"Status code: %s\n" .
"Request ID: %s\n",
$result->statusCode,
$result->requestId
);Replace yourVersionId with the actual version ID. To retrieve version IDs, see List objects.
Response fields
| Field | Description |
|---|---|
statusCode | HTTP status code. 200 indicates the tags were applied successfully. |
requestId | Unique identifier for the request. Use this value when contacting support or investigating errors. |