All Products
Search
Document Center

Object Storage Service:Bucket tagging (PHP SDK V2)

Last Updated:Feb 27, 2026

Bucket tags are key-value pairs that categorize and manage your OSS buckets. Use the OSS PHP SDK V2 to set, get, and delete bucket tags.

The sample code uses the public endpoint of the China (Hangzhou) region (cn-hangzhou). To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

Prerequisites

  • PHP 8.0 or later is installed. The bucket tagging methods in the OSS PHP SDK V2 require PHP 8.0 or later.

  • The OSS PHP SDK V2 is installed via Composer. The autoloader is available at vendor/autoload.php.

  • Environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are configured.

Set bucket tags

Call putBucketTags to add or overwrite tags on a bucket. The following example sets two tags (key1=value1 and key2=value2) on the specified bucket.

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define command-line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
    "bucket" => ['help' => 'The name of the bucket.', '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])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help";
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];

// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Initialize the OSSClient instance.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

$client = new Oss\Client($cfg);

// Create a Tagging object with key-value pairs.
$tagging = new Oss\Models\Tagging(
    tagSet: new Oss\Models\TagSet(
        tags: [new Oss\Models\Tag(key: 'key1', value: 'value1'), new Oss\Models\Tag(key: 'key2', value: 'value2')]
    )
);

$request = new Oss\Models\PutBucketTagsRequest(bucket: $bucket, tagging: $tagging);
$result = $client->putBucketTags($request);

printf(
    'status code:' . $result->statusCode . PHP_EOL .
    'request id:' . $result->requestId
);

Expected output:

status code:200
request id:5C3D5D30F3D3BE354E2A****

Get bucket tags

Call getBucketTags to retrieve the tags of a bucket.

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define command-line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
    "bucket" => ['help' => 'The name of the bucket.', '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])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help";
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];

// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Initialize the OSSClient instance.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

$client = new Oss\Client($cfg);

$request = new Oss\Models\GetBucketTagsRequest(bucket: $bucket);
$result = $client->getBucketTags($request);

printf(
    'status code:' . $result->statusCode . PHP_EOL .
    'request id:' . $result->requestId . PHP_EOL .
    'tags:' . var_export($result->tagging, true)
);

Expected output:

status code:200
request id:5C3D5D30F3D3BE354E2A****
tags:AlibabaCloud\Oss\V2\Models\Tagging::__set_state(array(
   'tagSet' =>
  AlibabaCloud\Oss\V2\Models\TagSet::__set_state(array(
     'tags' =>
    array (
      0 =>
      AlibabaCloud\Oss\V2\Models\Tag::__set_state(array(
         'key' => 'key1',
         'value' => 'value1',
      )),
      1 =>
      AlibabaCloud\Oss\V2\Models\Tag::__set_state(array(
         'key' => 'key2',
         'value' => 'value2',
      )),
    ),
  )),
))

Delete bucket tags

Call deleteBucketTags to remove all tags from a bucket.

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define command-line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
    "bucket" => ['help' => 'The name of the bucket.', '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])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help";
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];

// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Initialize the OSSClient instance.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

$client = new Oss\Client($cfg);

$request = new Oss\Models\DeleteBucketTagsRequest(bucket: $bucket);
$result = $client->deleteBucketTags($request);

printf(
    'status code:' . $result->statusCode . PHP_EOL .
    'request id:' . $result->requestId
);

Expected output:

status code:204
request id:5C3D5D30F3D3BE354E2A****

References

  • For more information about the API operation that you can call to configure tags for a bucket, see PutBucketTags.

  • For more information about the API operation that you can call to query the tags of a bucket, see GetBucketTags.

  • For more information about the API operation that you can call to delete the tags of a bucket, see DeleteBucketTags.