Todos os produtos
Search
Central de documentação

Object Storage Service:Obter tags de objeto usando o OSS SDK for PHP 2.0

Última atualização: Jul 03, 2026

Este tópico descreve como obter tags de objeto com o OSS SDK for PHP 2.0.

Notas de uso

  • O código de exemplo deste tópico usa o ID da região cn-hangzhou, referente à região China (Hangzhou). Por padrão, o acesso aos recursos em um bucket ocorre via endpoint público. Para acessar os recursos do bucket a partir de outros serviços da Alibaba Cloud na mesma região, utilize um endpoint interno. Para mais informações sobre as regiões e endpoints compatíveis com o OSS, consulte Regiões e endpoints.

  • A obtenção de tags de objeto exige a permissão oss:GetObjectTagging. Para mais detalhes, consulte Conceder permissões personalizadas a um usuário RAM.

Nota
  • Um par chave-valor identifica os objetos. Para saber mais sobre marcação de objetos, consulte Marcar objetos.

  • Para mais informações sobre como obter tags de objeto, consulte GetObjectTagging.

Código de exemplo

O exemplo a seguir demonstra como consultar as tags de uma versão específica de um objeto em um bucket:

<?php

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

use AlibabaCloud\Oss\V2 as Oss;

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

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

// Parse the command-line arguments.
$options = getopt("", $longopts);

// Check whether required arguments are specified.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help']; // Obtain the help information of the argument.
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1); // If a required argument is not specified, exit the program.
    }
}

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

// Load the credentials 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"]); // If an endpoint is provided, set the endpoint.
}

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

// Create a GetObjectTaggingRequest object to obtain the tag information of the object.
$request = new Oss\Models\GetObjectTaggingRequest(
    bucket: $bucket,
    key: $key
);

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

// Print the result of obtaining object tags.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, 200 indicates that the request is successful.
    'request id:' . $result->requestId . PHP_EOL .   // The request ID, which is used to debug or track requests.
    'result:' . var_export($result, true) . PHP_EOL  // The detailed result of the obtained object tags.
);