Use the OSS SDK for PHP V2 to retrieve the tag set associated with an object.
Prerequisites
Before you begin, ensure that you have:
The
oss:GetObjectTaggingpermission. For details, see Grant custom permissions to a RAM userYour AccessKey ID and AccessKey Secret stored as environment variables
Usage notes
The examples in this topic use the China (Hangzhou) region (
cn-hangzhou) and the public endpoint. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For a list of supported regions and endpoints, see OSS regions and endpoints.Object tagging uses key-value pairs to label an object. For background information, see Object tagging.
For the underlying API, see GetObjectTagging.
Get object tags
The following example retrieves all tags on a specified object.
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Initialize the client.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion('cn-hangzhou');
$client = new Oss\Client($cfg);
// Retrieve object tags.
$request = new Oss\Models\GetObjectTaggingRequest(
bucket: 'examplebucket',
key: 'exampleobject.txt'
);
$result = $client->getObjectTagging($request);
// Print the tag set.
printf(
'HTTP status: %s' . PHP_EOL .
'Request ID: %s' . PHP_EOL .
'Result: %s' . PHP_EOL,
$result->statusCode,
$result->requestId,
var_export($result, true)
);Replace examplebucket and exampleobject.txt with your actual bucket name and object name.
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
bucket | Yes | string | The name of the bucket that contains the object. |
key | Yes | string | The full name (path) of the object. |
Response
A successful request returns HTTP status 200 and a result object with the following fields:
| Field | Type | Description |
|---|---|---|
statusCode | integer | The HTTP status code. 200 indicates success. |
requestId | string | The unique request ID for debugging and support. |