Query object tags (PHP SDK V2)

Updated at:

When versioning is enabled on a bucket, OSS returns the tags of the current object version by default. To query tags on a specific version, include the version ID in the request.

Prerequisites

Before you begin, make sure you have:

Usage notes

  • The examples in this topic use the cn-hangzhou region with the public endpoint. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For endpoint mappings, see OSS regions and endpoints.

  • Object tags are key-value pairs. For background on object tagging, see Object tagging.

  • For the underlying API operation, see GetObjectTagging.

Query object tags

The following example queries the tags of a specific object version by specifying its version ID.

<?php

// Load the autoload file to initialize SDK dependencies
require_once __DIR__ . '/../../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define CLI parameters
$optsdesc = [
    "region"   => ['help' => 'The region where the bucket is located.', 'required' => True],  // REQUIRED
    "endpoint" => ['help' => 'The endpoint used to access OSS.',        'required' => False], // Optional
    "bucket"   => ['help' => 'The bucket name.',                        'required' => True],  // REQUIRED
    "key"      => ['help' => 'The object name.',                        'required' => True],  // REQUIRED
];

// Build the long option list for getopt (each option requires a value)
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

$options = getopt("", $longopts);

// Validate required parameters
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        echo "Error: the following arguments are required: --$key, " . $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 SDK configuration
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create the OSS client
$client = new Oss\Client($cfg);

// Build the request — set versionId to query a specific version's tags
$request = new Oss\Models\GetObjectTaggingRequest(
    bucket:    $bucket,     // REQUIRED
    key:       $key,        // REQUIRED
    versionId: "yourVersionId", // Optional — omit to query the current version
);

// Send the request
$result = $client->getObjectTagging($request);

// Print the response
printf(
    'status code: ' . $result->statusCode . PHP_EOL .
    'request ID: '  . $result->requestId  . PHP_EOL .
    'result: '      . var_export($result, true) . PHP_EOL
);

Replace the following placeholder before running the code:

PlaceholderDescription
yourVersionIdThe version ID of the object version whose tags you want to query

What's next

  • To query tags on the current object version (without specifying a version ID), omit the versionId parameter from GetObjectTaggingRequest.

  • To set or update object tags, see Set object tags.

  • To delete object tags, see Delete object tags.