All Products
Search
Document Center

Object Storage Service:Obtain bucket information (Python SDK V1)

Last Updated:Aug 08, 2025

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 from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS 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 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.

Sample code

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 environment variables. Before you run the sample code, configure the environment variables.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region where the endpoint is located, for example, cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Obtain bucket information.
bucket_info = bucket.get_bucket_info()
# Obtain the bucket name.
print("name: " + bucket_info.name)
# Obtain the bucket storage class.
print("storage class: " + bucket_info.storage_class)
# Obtain the bucket creation time.
print("creation date: " + bucket_info.creation_date)
# Obtain the internal endpoint of the bucket.
print("intranet_endpoint: " + bucket_info.intranet_endpoint)
# Obtain the public endpoint of the bucket.
print("extranet_endpoint " + bucket_info.extranet_endpoint)
# Obtain owner information.
print("owner: " + bucket_info.owner.id)
# Obtain the access control list (ACL) of the bucket.
print("grant: " + bucket_info.acl.grant)
# Obtain the disaster recovery type.
print("data_redundancy_type:" + bucket_info.data_redundancy_type)
# Obtain the access tracking status of the bucket. You can obtain the access tracking status only in Python SDK 2.16.1 and later.
print("access_monitor:" + bucket_info.access_monitor)

References

  • For the complete sample code to obtain bucket information, see GitHub.

  • For more information about the API operation used to obtain bucket information, see GetBucketInfo.