All Products
Search
Document Center

Object Storage Service:Query the region of a bucket (Python SDK V2)

Last Updated:Jul 31, 2025

This topic describes how to query the region where a bucket is located using the OSS SDK for Python V2.

Notes

  • The sample code in this topic uses the region ID cn-hangzhou for the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see OSS regions and endpoints.

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

  • To query the region where a bucket is located, you must have the oss:GetBucketLocation permission. For more information, see Grant custom policies to RAM users.

Method definition

get_bucket_location(request: GetBucketLocationRequest, **kwargs) → GetBucketLocationResult

Request parameters

Parameter

Type

Description

request

GetBucketLocationRequest

Specifies the request parameters. For more information, see GetBucketLocationRequest

Return values

Type

Description

GetBucketLocationResult

The return value. For more information, see GetBucketLocationResult

For more information about the complete definition of this method, see get_bucket_location.

Sample code

The following code provides an example of how to query the region where a bucket is located.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser. This script is used to obtain the location information of a specified bucket.
parser = argparse.ArgumentParser(description="Get the location of a specified OSS bucket.")

# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket to get the location for.', required=True)

# Add the --endpoint command-line argument, which specifies the domain names that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')

def main():
    """
    The main function, which is used to parse command-line arguments and obtain the location information of the specified bucket.
    """

    args = parser.parse_args()  # Parse command-line arguments.

    # Load credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK, and set the credentials provider and region information.
    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 using the configured information.
    client = oss.Client(cfg)

    # Construct a request to obtain the location information of the specified bucket.
    request = oss.GetBucketLocationRequest(bucket=args.bucket)

    # Execute the request and obtain the response.
    result = client.get_bucket_location(request)

    # Print the status code, request ID, and location information of the response.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' location: {result.location}'
    )

if __name__ == "__main__":
    main()  # The script entry point. The main function is called when the file is run directly.

References