Un bucket est un conteneur pour les objets stockés dans Object Storage Service (OSS). Tous les objets d'OSS résident dans des buckets. Ces derniers sont répertoriés par ordre alphabétique. Vous pouvez lister les buckets appartenant au compte Alibaba Cloud actuel, dans toutes les régions et selon des conditions spécifiques.
Notes
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus de détails sur les régions et endpoints pris en charge, consultez Régions et endpoints.
Cette rubrique explique comment créer une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Créer un OssClient.
Pour répertorier les buckets, vous devez disposer de l'autorisation
oss:ListBuckets. Pour plus d'informations, consultez Accorder une politique d'accès personnalisée à un utilisateur RAM.
Répertorier tous les buckets d'un compte Alibaba Cloud
L'exemple de code suivant montre comment répertorier les buckets de toutes les régions associées au compte Alibaba Cloud actuel :
<?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");
}
Répertorier les buckets dont le nom contient un préfixe spécifié
L'exemple de code suivant montre comment répertorier les buckets dont le nom contient le préfixe « example » dans toutes les régions associées au compte Alibaba Cloud actuel :
<?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");
}
Répertorier les buckets dont le nom suit alphabétiquement celui indiqué par le marqueur
L'exemple de code suivant montre comment répertorier les buckets dont le nom suit alphabétiquement celui du bucket nommé « examplebucket » dans toutes les régions associées au compte Alibaba Cloud actuel :
<?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");
}
Répertorier un nombre spécifique de buckets
L'exemple de code suivant montre comment répertorier les buckets de toutes les régions associées au compte Alibaba Cloud actuel et limiter le nombre maximal de buckets répertoriés à 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");
}
Références
Pour obtenir l'exemple de code complet relatif au répertoire des buckets, consultez l'exemple GitHub.
Pour plus d'informations sur l'opération API permettant de répertorier les buckets, consultez ListBuckets (GetService).