Object Storage Service (OSS) lets you use object tagging to classify objects in buckets. You can set lifecycle rules, access permissions, and other configurations for objects that have the same tags.
Notes
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To set object tags, you must have the
oss:PutObjectTaggingpermission. For more information, see Grant a custom access policy to a RAM user.
Add object tags during an upload
The following examples show how to add object tags for simple uploads, multipart uploads, and append uploads.
Adding object tags during a simple upload
The following code shows how to add object tags during a simple upload.
<?php if (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php'; } if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; } use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run this example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. $provider = new EnvironmentVariableCredentialsProvider(); // Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. $endpoint = "yourEndpoint"; // Specify the bucket name. For example, examplebucket. $bucket = "examplebucket"; // Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. $object = "exampledir/exampleobject.txt"; // Specify the string to upload. $content = "hello world"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the object tags. $options = array( OssClient::OSS_HEADERS => array( 'x-oss-tagging' => 'key1=value1&key2=value2&key3=value3', )); try { // Upload the object using a simple upload. $ossClient->putObject($bucket, $object, $content, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");Add object tags during a multipart upload
The following code shows how to add object tags when you upload a local file to OSS using multipart upload.
<?php if (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php'; } if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; } use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run this example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. $provider = new EnvironmentVariableCredentialsProvider(); // Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. $endpoint = "yourEndpoint"; // Specify the bucket name. For example, examplebucket. $bucket = "examplebucket"; // Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. $object = "exampledir/exampleobject.txt"; // Specify the full path of the local file, such as D:\\localpath\\examplefile.txt. If you specify only the file name, such as examplefile.txt, the file is uploaded from the local path of the project where the sample program is located. $file = "D:\\localpath\\examplefile.txt"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the object tags. $options = array( OssClient::OSS_CHECK_MD5 => true, OssClient::OSS_PART_SIZE => 1, OssClient::OSS_HEADERS => array( 'x-oss-tagging' => 'key1=value1&key2=value2&key3=value3', ), ); try { // Upload the object using a multipart upload. $ossClient->multiuploadFile($bucket, $object, $file, $options); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");Adding object tags during an append upload
The following code shows how to add object tags during an append upload.
<?php if (is_file(__DIR__ . '/../autoload.php')) { require_once __DIR__ . '/../autoload.php'; } if (is_file(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; } use OSS\Credentials\EnvironmentVariableCredentialsProvider; use OSS\OssClient; use OSS\CoreOssException; // Obtain access credentials from environment variables. Before you run this example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. $provider = new EnvironmentVariableCredentialsProvider(); // Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. $endpoint = "yourEndpoint"; // Specify the bucket name. For example, examplebucket. $bucket = "examplebucket"; // Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. $object = "exampledir/exampleobject.txt"; // Specify the strings to append in sequence. $content_array = array('Hello OSS', 'Hi OSS'); $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); // Set the object tags. $options = array( OssClient::OSS_HEADERS => array( 'x-oss-tagging' => 'key1=value1&key2=value2', )); try { // Upload the object using an append upload. $position = $ossClient->appendObject($bucket, $object, $content_array[0], 0, $options); $position = $ossClient->appendObject($bucket, $object, $content_array[1], $position); } catch (OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");
Add or change object tags for an uploaded object
If you did not add object tags when you uploaded an object, or if the existing tags no longer meet your needs, you can add or change the object tags after the upload is complete.
The following code shows how to add or change object tags for an uploaded object.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Model\TaggingConfig;
use OSS\Model\Tag;
// Obtain access credentials from environment variables. Before you run this example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name. For example, examplebucket.
$bucket = "examplebucket";
// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
$object = "exampledir/exampleobject.txt";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Set the object tags.
$config = new TaggingConfig();
$config->addTag(new Tag("key1", "value1"));
$config->addTag(new Tag("key2", "value2"));
try {
$ossClient->putObjectTagging($bucket, $object, $config);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");Add or change object tags for a specific object version
In a bucket with versioning enabled, you can add or change object tags for a specific version of an object by specifying its version ID.
The following code shows how to add or change object tags for a specific version of an object.
For more information about how to obtain a version ID, see List objects (PHP SDK V1).
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Model\TaggingConfig;
use OSS\Model\Tag;
// Obtain access credentials from environment variables. Before you run this example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name. For example, examplebucket.
$bucket = "examplebucket";
// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
$object = "exampledir/exampleobject.txt";
// Specify the version ID of the object.
$options = array(
'versionId'=>'CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****'
);
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Set the object tags.
$config = new TaggingConfig();
$config->addTag(new Tag("key1", "value1"));
$config->addTag(new Tag("key2", "value2"));
try {
$ossClient->putObjectTagging($bucket, $object, $config,$options);
printf("putObjectTagging Success" . "\n");
} catch (OssException $e) {
printf($e->getMessage() . "\n");
return;
}