Todos os produtos
Search
Central de documentação

Object Storage Service:Consultar informações de um bucket usando o OSS SDK for PHP 2.0

Última atualização: Sep 24, 2026

Este tópico descreve como usar o Object Storage Service (OSS) SDK for PHP para consultar informações de um bucket, como status de rastreamento de acesso, região, data de criação, lista de controle de acesso (ACL), nome e ID do proprietário, classe de armazenamento, tipo de redundância, endpoint público, endpoint interno, status de replicação entre regiões (CRR), status de versionamento e método de criptografia. Isso facilita as operações subsequentes.

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, use um endpoint público para acessar recursos em um bucket. Se precisar acessar os recursos do bucket por meio de outros serviços da Alibaba Cloud na mesma região do bucket, use um endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.

  • Para consultar informações de um bucket, você deve ter a permissão oss:GetBucketInfo. Para mais informações, consulte Grant a custom policy.

Exemplos

O código de exemplo a seguir mostra como consultar informações de um bucket:

<?php

require_once __DIR__ . '/../vendor/autoload.php'; // Automatically load objects and dependency libraries.

use AlibabaCloud\Oss\V2 as Oss;

// Specify command line parameters.
$optsdesc = [
    "region" => ['help' => The region in which the bucket is located.', 'required' => True], // (Required) Specify the region in which the bucket is located. Example: oss-cn-hangzhou. 
    "endpoint" => ['help' => The domain names that other services can use to access OSS.', 'required' => False], // (Optional) Specify the endpoint that can be used by other services to access OSS. 
    "bucket" => ['help' => The name of the bucket, 'required' => True], // (Required) Specify the name of the bucket. 
];
$longopts = \array_map(function ($key) {
    return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));

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

// Check whether the required parameters are configured.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Specifies that the required parameters are not configured.
        exit(1); 
    }
}

// Obtain the values of the command line parameters.
$region = $options["region"]; // The region in which the bucket is located. 
$bucket = $options["bucket"]; // The name of the bucket. 

// Use environment variables to load the credential information (AccessKey ID and AccessKey secret).
$credentialsProvider=new Oss\Credentials\EnvironmentVariableCredentialsProvider(); // Obtain the credential information from the environment variables. 

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

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

// Create a request to query information about the bucket.
$request = new Oss\Models\GetBucketInfoRequest($bucket); // Create a request to query information about the bucket. 

// Use the getBucketInfo method to query information about the bucket.
$result = $client->getBucketInfo($request); // Use the getBucketInfo method to query information about the bucket. 

// Display the returned result.
printf(
    'status code:' . $result-> statusCode. PHP_EOL . // The HTTP response status code. 
    'request id:' . $result-> requestId. PHP_EOL . // The unique identifier of the request. 
    'bucket info:' . var_export($result->bucketInfo, true) // The detailed information about the bucket. 
);

Referências

  • Para obter mais informações sobre buckets, consulte Bucket overview.

  • Para acessar o código de exemplo completo usado para consultar informações de um bucket, visite o GitHub.

  • Para mais detalhes sobre a operação de API para consultar informações de um bucket, consulte GetBucketInfo.