Get bucket information (PHP SDK V1)

更新时间:
复制 MD 格式

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:GetBucketInfo permission. For details, see Grant custom permissions to a RAM user

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment 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 OSSClient instance 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

ParameterTypeDescription
$bucketStringThe name of the bucket to query

Return value fields

getBucketInfo() returns an object with the following getter methods:

MethodFieldDescription
getName()NameBucket name
getLocation()LocationRegion where the bucket is located
getCreateDate()CreateDateBucket creation time
getStorageClass()StorageClassStorage class of the bucket
getExtranetEndpoint()ExtranetEndpointPublic endpoint for internet access
getIntranetEndpoint()IntranetEndpointInternal 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