This topic describes how to determine whether a bucket exists.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhoufor the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you access resources from other Alibaba Cloud services in the same region as the bucket, use an internal endpoint. For more information about Object Storage Service (OSS) regions and endpoints, see Regions and endpoints.To determine whether a bucket exists, you must have the
oss:GetBucketAclpermission. For more information, see Attach a custom policy to a RAM user.
Examples
The following sample code provides an example on how to determine whether a bucket exists:
<?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.
// Determine whether the bucket exists.
$exist = $client->isBucketExist($bucket); // Use the isBucketExist method to determine whether the bucket exists.
// Display the results.
printf(
'bucket is exist:' . var_export($exist, true) // Display whether the bucket exists. A value of true indicates that the bucket exists. A value of false indicates that the bucket does not exist.
);
References
For the complete sample code that is used to determine whether a bucket exists, visit GitHub.