All Products
Search
Document Center

Object Storage Service:Query bucket information

Last Updated:Aug 21, 2023

A bucket is a container used to store objects in Object Storage Service (OSS). 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. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 Initialization.

  • 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 on how to query information about a bucket, including the region and creation date of the bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Query information about the bucket. 
bucket_info = bucket.get_bucket_info()
# Query the name of the bucket. 
print('name: ' + bucket_info.name)
# Query the storage class of the bucket. 
print('storage class: ' + bucket_info.storage_class)
# Query the time when the bucket was created. 
print('creation date: ' + bucket_info.creation_date)
# Query the internal endpoint of the bucket. 
print('intranet_endpoint: ' + bucket_info.intranet_endpoint)
# Query the public endpoint of the bucket. 
print('extranet_endpoint ' + bucket_info.extranet_endpoint)
# Query the information about the owner of the bucket. 
print('owner: ' + bucket_info.owner.id)
# Query the access control list (ACL) of the bucket. 
print('grant: ' + bucket_info.acl.grant)
# Query the redundancy type of the bucket. 
print('data_redundancy_type:' + bucket_info.data_redundancy_type)
# Query the access tracking status of the bucket. Only OSS SDK for Python 2.16.1 or later allows you to query the access tracking status. 
print('access_monitor:' + bucket_info.access_monitor)

References

  • For the complete sample code for querying information about a bucket, visit GitHub.

  • For more information about the API operation for querying information about a bucket, see GetBucketInfo.