All Products
Search
Document Center

Object Storage Service:Query object tags (PHP SDK V2)

Last Updated:Aug 05, 2025

When versioning is enabled for the bucket containing the object whose tags you want to query, Object Storage Service (OSS) returns the tags of the current version of the object by default. To query the tags of a specific version of the object, you must specify the object's version ID.

Usage notes

  • The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. By default, the public endpoint is used. If you access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about the mappings between OSS regions and endpoints, see OSS regions and endpoints.

  • To query the tags of an object, you must have the oss:GetObjectTagging permission. For more information, see Grant custom permissions to a RAM user.

Note
  • Object tagging uses a key-value pair to tag objects. For more information about object tagging, see Object tagging in the Developer Guide.

  • For more information about how to query the tags of an object, see GetObjectTagging.

Sample code

Note
  • If versioning is enabled for the bucket that stores the object whose tags you want to query, you can query the tags of a specific version of the object by specifying the version ID of the object. If you do not specify the version ID, OSS queries only the tags of the current version of the object.

  • For more information about how to obtain the version ID, see List objects.

The following sample code demonstrates how to query the tags of a specific version of an object in a bucket:

<?php

// Import the autoload file to ensure that the dependency libraries are correctly loaded
require_once __DIR__ . '/../../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the description information of command line parameters
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region where the bucket is located (required)
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint (optional)
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name (required)
    "key" => ['help' => 'The name of the object', 'required' => True], // The object name (required)
];

// Convert parameter descriptions to the long option format required by getopt
// Add ":" after each parameter to indicate that the parameter requires a value
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse command line parameters
$options = getopt("", $longopts);

// Verify whether the required parameters exist
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help']; // Get the help information of the parameter
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1); // Exit the program if a required parameter is missing
    }
}

// Extract values from the parsed parameters
$region = $options["region"]; // The region where the bucket is located
$bucket = $options["bucket"]; // The bucket name
$key = $options["key"];       // The object name

// Load credential information from environment variables
// Use EnvironmentVariableCredentialsProvider to read the Access Key ID and Access Key Secret from environment variables
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider
$cfg->setRegion($region); // Set the region where the bucket is located
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]); // Set the endpoint if provided
}

// Create an OSSClient instance
$client = new Oss\Client($cfg);

// Create a GetObjectTaggingRequest object to query the tags of the object
$request = new Oss\Models\GetObjectTaggingRequest(
    bucket: $bucket,
    key: $key,
    versionId:"yourVersionId", //Specify the actual version ID
);

// Execute the operation to query the object tags
$result = $client->getObjectTagging($request);

// Print the result of querying the object tags
printf(
    'status code:' . $result->statusCode . PHP_EOL . // HTTP status code, such as 200 for success
    'request id:' . $result->requestId . PHP_EOL .   // Request ID, used for debugging or tracking requests
    'result:' . var_export($result, true) . PHP_EOL  // Detailed results of the object tags obtained
);