Object Storage Service (OSS) lets you attach tags to objects to classify and manage them. This topic shows how to configure object tags using OSS SDK for PHP 2.0.
Usage notes
The examples use the region ID
cn-hangzhou(China (Hangzhou)) and a public endpoint. To access OSS from other Alibaba Cloud products in the same region, use an internal endpoint instead. For supported regions and endpoints, see Regions and endpoints.Configuring object tags requires the
oss:PutObjectTaggingpermission. For details, see Attach a custom policy to a RAM user.
Operations overview
| Scenario | How to pass tags | Key parameter or API |
|---|---|---|
| Simple upload | Inline with the upload request | tagging on PutObjectRequest |
| Multipart upload | Inline when initiating the upload | tagging on InitiateMultipartUploadRequest |
| Append upload | Inline with the first append request | tagging on AppendObjectRequest |
| Copy object | Replace source tags during copy | taggingDirective: "Replace" + tagging on CopyObjectRequest |
| Existing object | Dedicated tagging API (replaces all tags) | PutObjectTaggingRequest |
Set tags during upload
All upload examples use the URL-encoded string format for tags: "key1=value1&key2=value2".
Add or update tags on an existing object
If an existing object has no tags or the tags of the object do not meet your requirements, you can add tags to or modify the tags of the object.
<?php
// Load dependencies.
require_once __DIR__ . '/../../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define and parse command line parameters.
$optsdesc = [
"region" => ['help' => 'The region where the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The endpoint for OSS access.', 'required' => False],
"bucket" => ['help' => 'The bucket name.', 'required' => True],
"key" => ['help' => 'The object name.', 'required' => True],
];
$longopts = \array_map(fn($key) => "$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 client.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
$client = new Oss\Client($cfg);
// Replace all tags on the object with the specified tag set.
$request = new Oss\Models\PutObjectTaggingRequest(
bucket: $bucket,
key: $key,
tagging: new Oss\Models\Tagging(
tagSet: new Oss\Models\TagSet(
tags: [
new Oss\Models\Tag('k1', 'v1'),
new Oss\Models\Tag('k2', 'v2'),
]
)
)
);
$result = $client->putObjectTagging($request);
printf(
'status code: %s' . PHP_EOL .
'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);