Um bucket é um contêiner para objetos armazenados no Object Storage Service (OSS). Todos os objetos do OSS residem em buckets, listados em ordem alfabética. Você pode listar os buckets da conta atual do Alibaba Cloud em todas as regiões que atendam a condições específicas.
Observações
Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços do Alibaba Cloud na mesma região, use um endpoint interno. Para obter detalhes sobre as regiões e endpoints compatíveis, consulte Regiões e endpoints.
O exemplo abaixo demonstra como criar uma instância OssClient com um endpoint do OSS. Para outras configurações, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Criar um OssClient.
Para listar buckets, você precisa da permissão
oss:ListBuckets. Para mais informações, consulte Conceder uma política de acesso personalizada a um usuário RAM.
Listar todos os buckets de uma conta Alibaba Cloud
O código de exemplo a seguir mostra como listar buckets em todas as regiões da conta atual do Alibaba Cloud:
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// This example uses the Endpoint for the China (Hangzhou) region. Replace the Endpoint with the one for your region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try{
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
// List all buckets in all regions within the current account.
$bucketListInfo = $ossClient->listBuckets();
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
$bucketList = $bucketListInfo->getBucketList();
foreach($bucketList as $bucket) {
print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
}
Listar buckets cujos nomes contenham um prefixo específico
O exemplo de código a seguir ilustra como listar buckets cujos nomes contenham o prefixo "example" em todas as regiões da conta atual do Alibaba Cloud:
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// This example uses the Endpoint for the China (Hangzhou) region. Replace the Endpoint with the one for your region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Specify a prefix.
$prefix = "example";
try{
$options = array(OssClient::OSS_QUERY_STRING => array(OssClient::OSS_PREFIX => $prefix));
// List buckets that have the specified prefix in all regions within the current account.
$bucketListInfo = $ossClient->listBuckets($options);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
$bucketList = $bucketListInfo->getBucketList();
foreach($bucketList as $bucket) {
print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
}
Listar buckets cujos nomes sejam alfabeticamente posteriores ao bucket indicado pelo marcador
O trecho de código a seguir exemplifica como listar buckets cujos nomes aparecem, em ordem alfabética, após o bucket chamado "examplebucket" em todas as regiões da conta atual do Alibaba Cloud:
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// This example uses the Endpoint for the China (Hangzhou) region. Replace the Endpoint with the one for your region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Specify the marker.
$marker = "examplebucket";
// List buckets that are alphabetically after examplebucket in all regions within the current account.
try{
$options = array(OssClient::OSS_QUERY_STRING => array(OssClient::OSS_MARKER => $marker));
$bucketListInfo = $ossClient->listBuckets($options);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
$bucketList = $bucketListInfo->getBucketList();
foreach($bucketList as $bucket) {
print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
}
Listar um número específico de buckets
O código a seguir demonstra como listar buckets em todas as regiões da conta atual do Alibaba Cloud e definir o número máximo de buckets retornados como 500:
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// This example uses the Endpoint for the China (Hangzhou) region. Replace the Endpoint with the one for your region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
// MAX_KEYS specifies the maximum number of buckets to return. The value of MAX_KEYS cannot be greater than 1,000. If you do not specify MAX_KEYS, a maximum of 100 buckets are returned by default.
// List 500 buckets.
try{
$options = array(OssClient::OSS_QUERY_STRING => array(OssClient::OSS_MAX_KEYS => 500));
$bucketListInfo = $ossClient->listBuckets($options);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
$bucketList = $bucketListInfo->getBucketList();
foreach($bucketList as $bucket) {
print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
}
Referências
Para obter o código completo de listagem de buckets, consulte o exemplo no GitHub.
Para mais detalhes sobre a operação de API de listagem de buckets, consulte ListBuckets (GetService).