All Products
Search
Document Center

Object Storage Service:Query bucket information (Java SDK)

Last Updated:Jul 31, 2025

A bucket is a container used to store objects. This topic describes how to query information about a bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configuration examples for common scenarios.

  • To query information about a bucket, you must have the oss:GetBucketInfo permission. For more information, see Attach a custom policy to a RAM user.

Examples

The following code provides an example of how to query information about a bucket, such as its region and creation date.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.BucketInfo; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name. For example, examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // The bucket information includes the region (Region or Location), creation date (CreationDate), and owner (Owner). // Specify the bucket name. For example, examplebucket. BucketInfo info = ossClient.getBucketInfo(bucketName); // Obtain the region. System.out.println(info.getBucket().getLocation()); // Obtain the creation date. System.out.println(info.getBucket().getCreationDate()); // Obtain the owner information. System.out.println(info.getBucket().getOwner()); // Obtain the disaster recovery type. System.out.println(info.getDataRedundancyType()); // Obtain the access tracking status. You can obtain the access tracking status of a bucket only if you use OSS SDK for Java 3.16.0 or later. System.out.println(info.getBucket().getAccessMonitor()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }

References

For more information about the API operation that you can call to query information about a bucket, see GetBucketInfo.