This topic describes how to retrieve information about a bucket using the Object Storage Service (OSS) Python SDK. The information includes the access tracking status, region, creation date, access control list (ACL), owner's name and ID, storage class, data redundancy type, public endpoint, internal endpoint, cross-region replication status, versioning status, and encryption method.
Notes
The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) as an example. The public endpoint is used by default. If you access OSS from other Alibaba Cloud services in the same region, use the internal same-region endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.The examples in this topic use access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To retrieve information about a bucket, you must have the
oss:GetBucketInfopermission. For more information, see Grant custom access policies to a RAM user.
Method definition
get_bucket_info(request: GetBucketInfoRequest, **kwargs) → GetBucketInfoResultRequest parameters
Parameter | Type | Description |
request | GetBucketInfoRequest | Set the request parameters. For more information, see GetBucketInfoRequest. |
Return values
Type | Description |
GetBucketInfoResult | The return value. For more information, see GetBucketInfoResult. |
For the complete method definition for retrieving bucket information, see get_bucket_info.
Sample code
Use the following code to retrieve information about a bucket.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser. This script gets detailed information about a specified bucket.
parser = argparse.ArgumentParser(description="Get detailed information about a specified OSS bucket.")
# Add the --region command-line argument. This argument specifies the region where the bucket is located. This is a required argument.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument. This argument specifies the name of the bucket. This is a required argument.
parser.add_argument('--bucket', help='The name of the bucket to get information for.', required=True)
# Add the --endpoint command-line argument. This argument specifies the endpoint that other services can use to access OSS. This is an optional argument.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')
def main():
"""
The main function, which parses command-line arguments and gets detailed information about the specified bucket.
"""
args = parser.parse_args() # Parse command-line arguments.
# Load credentials from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configurations of the SDK and set the credential provider and region.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If the endpoint argument is provided, set the endpoint in the configuration.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client with the specified configurations.
client = oss.Client(cfg)
# Construct a request to get detailed information about the specified bucket.
request = oss.GetBucketInfoRequest(bucket=args.bucket)
# Send the request and get the response.
result = client.get_bucket_info(request)
# Print the information from 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() # The entry point of the script. The main function is called when the file is run directly.List of common bucket information
Parameter | Description |
BucketInfo.Name | The bucket name. |
BucketInfo.AccessMonitor | The access tracking status of the bucket. |
BucketInfo.Location | The region where the bucket is located. |
BucketInfo.CreationDate | The date when the bucket was created. |
BucketInfo.ExtranetEndpoint | The public endpoint of the bucket. |
BucketInfo.IntranetEndpoint | The internal same-region endpoint that an ECS instance can use to access the bucket. |
BucketInfo.ACL | The access control list (ACL) of the bucket. |
BucketInfo.RedundancyType | The data redundancy type of the bucket. |
BucketInfo.Owner | Includes the following parameters:
|
BucketInfo.StorageClass | The storage class of the bucket. |
BucketInfo.SseRule | Includes the following parameters:
|
BucketInfo.Versioning | The versioning status of the bucket. |
BucketInfo.CrossRegionReplication | The cross-region replication status of the bucket. |
References
For more information about buckets, see Overview of buckets.
For the complete sample code for retrieving bucket information, see get_bucket_info.py.