Use OSS SDK for Python to query bucket properties such as access tracking state, region, creation date, ACL, owner, storage class, redundancy type, endpoints, CRR state, versioning state, and encryption method.
Notes
-
The sample code uses the region ID
cn-hangzhou(China (Hangzhou)) and the public endpoint. To access the bucket from Alibaba Cloud services in the same region, use the internal endpoint. OSS Regions and Endpoints. -
Access credentials are loaded from environment variables. For more information about how to configure the access credentials, see Configure access credentials.
-
The
oss:GetBucketInfopermission is required to query information about a bucket. For more information, see Grant a custom policy.
Method
get_bucket_info(request: GetBucketInfoRequest, **kwargs) → GetBucketInfoResult
Request parameters
|
Parameter |
Type |
Description |
|
request |
GetBucketInfoRequest |
Request parameters. GetBucketInfoRequest. |
Response parameters
|
Type |
Description |
|
GetBucketInfoResult |
Response body. GetBucketInfoResult. |
Complete method definition: get_bucket_info.
Sample code
The following example queries bucket information.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line parameter parser and describe the purpose of the script. This example describes how to query bucket info.
parser = argparse.ArgumentParser(description="Get detailed information about a specified OSS bucket.")
# Specify the required command line parameter --region, which specifies the region in which the bucket is located.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the required command line parameter --bucket, which specifies the name of the bucket.
parser.add_argument('--bucket', help='The name of the bucket to get information for.', required=True)
# Specify the optional command line parameter --endpoint, which specifies the endpoint that other services can use to access OSS.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')
def main():
"""
The main function used to parse command line parameters and query detailed information about the specified bucket.
"""
args = parser.parse_args() # Parse command line parameters.
# Load access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configurations of the SDK and specify the credential provider and region.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If the endpoint parameter is provided, assign this endpoint to the configuration settings.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client using the configured information.
client = oss.Client(cfg)
# Construct a request to query detailed information about the specified bucket.
request = oss.GetBucketInfoRequest(bucket=args.bucket)
# Send the request and retrieve the response.
result = client.get_bucket_info(request)
# Display various components in the response.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' name: {result.bucket_info.name},'
f' access monitor: {result.bucket_info.access_monitor},'
f' location: {result.bucket_info.location},'
f' creation date: {result.bucket_info.creation_date},'
f' extranet endpoint: {result.bucket_info.extranet_endpoint},'
f' intranet endpoint: {result.bucket_info.intranet_endpoint},'
f' acl: {result.bucket_info.acl},'
f' data redundancy type: {result.bucket_info.data_redundancy_type},'
f' owner id: {result.bucket_info.owner.id},'
f' owner display name: {result.bucket_info.owner.display_name},'
f' storage class: {result.bucket_info.storage_class},'
f' resource group id: {result.bucket_info.resource_group_id},'
)
if __name__ == "__main__":
main() # Entry point of the script. The main function is invoked when the file is run directly.
Common parameters in query requests
|
Parameter |
Description |
|
BucketInfo.Name |
Name of the bucket. |
|
BucketInfo.AccessMonitor |
Access tracking status of the bucket. |
|
BucketInfo.Location |
Region in which the bucket is located. |
|
BucketInfo.CreationDate |
Date when the bucket was created. |
|
BucketInfo.ExtranetEndpoint |
Public endpoint of the bucket. |
|
BucketInfo.IntranetEndpoint |
Internal endpoint for access from ECS instances in the same region. |
|
BucketInfo.ACL |
ACL of the bucket. |
|
BucketInfo.RedundancyType |
Redundancy type of the bucket. |
|
BucketInfo.Owner |
Includes:
|
|
BucketInfo.StorageClass |
Storage class of the bucket. |
|
BucketInfo.SseRule |
Includes:
|
|
BucketInfo.Versioning |
Versioning status of the bucket. |
|
BucketInfo.CrossRegionReplication |
Cross-region replication (CRR) status of the bucket. |
References
-
For additional information about buckets, see Bucket overview.
-
Complete sample code: get_bucket_info.py.