Use getBucketInfo() to retrieve metadata for a bucket, including its name, region, creation date, storage class, and endpoints. This SDK method maps to the underlying GetBucketInfo REST API operation.
Prerequisites
Before you begin, ensure that you have:
The
oss:GetBucketInfopermission. For details, see Grant custom permissions to a RAM userThe
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with your access credentials
Usage notes
The example uses the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint instead. For supported regions and endpoints, see Regions and endpoints.
The example creates an
OSSClientinstance using an OSS endpoint. To create one using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.
Get bucket information
<?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\Core\OssException;
// Load access credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
$provider = new EnvironmentVariableCredentialsProvider();
// Replace with your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Replace with your bucket name.
$bucket = "examplebucket";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$info = $ossClient->getBucketInfo($bucket);
printf("Name: %s\n", $info->getName());
printf("Location: %s\n", $info->getLocation());
printf("Creation date: %s\n", $info->getCreateDate());
printf("Storage class: %s\n", $info->getStorageClass());
printf("Public endpoint: %s\n", $info->getExtranetEndpoint());
printf("Internal endpoint: %s\n", $info->getIntranetEndpoint());
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");Parameters
| Parameter | Type | Description |
|---|---|---|
$bucket | String | The name of the bucket to query |
Return value fields
getBucketInfo() returns an object with the following getter methods:
| Method | Field | Description |
|---|---|---|
getName() | Name | Bucket name |
getLocation() | Location | Region where the bucket is located |
getCreateDate() | CreateDate | Bucket creation time |
getStorageClass() | StorageClass | Storage class of the bucket |
getExtranetEndpoint() | ExtranetEndpoint | Public endpoint for internet access |
getIntranetEndpoint() | IntranetEndpoint | Internal endpoint for access within the same region |
Error handling
OssException is thrown when the request fails. Call $e->getMessage() to retrieve the error details.
What's next
For the complete sample code, see the GitHub example.
For the underlying API operation, see GetBucketInfo.