This topic explains how to use the Python SDK V2 to query the endpoints for all supported regions or a specific region. It covers the Internet access (IPv4) endpoint, the internal access (classic network or VPC network) endpoint, and the acceleration endpoint (global upload and download acceleration).
Notes
-
You can query endpoints for all supported regions or a specific region, even if you have not created a bucket in those regions.
-
The sample code provided uses the region ID
cn-hangzhou
for China (Hangzhou) as an example and defaults to the Internet endpoint. To access OSS through other Alibaba Cloud products within the same region, use the internal endpoint. For details on the mapping between OSS-supported regions and endpoints, see OSS Regions and Endpoints.
Query the endpoints of all supported regions
Below is an example of how to query the endpoints for all supported regions.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line argument parser and describe the purpose of the script: The sample demonstrates how to query the region information supported by OSS
parser = argparse.ArgumentParser(description="describe regions sample")
# Specify the --region parameter, which indicates the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which indicates the name of the bucket. This parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the domain names that other services can use to access OSS. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --regions parameter, which indicates regional information. This parameter is optional.
parser.add_argument('--regions', help='Regional information.')
def main():
# Parse the parameters provided in the command line to obtain the values entered by the user
args = parser.parse_args()
# Load the authentication information required to access OSS from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configurations of the SDK to create a configuration object and specify the credential provider
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If a custom endpoint is provided, modify the endpoint parameter in the configuration object
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS
client = oss.Client(cfg)
# Send a request to obtain region information
result = client.describe_regions(oss.DescribeRegionsRequest(
regions=args.regions,
))
# Print the status code and request ID of the request
print(f'status code: {result.status_code},'
f' request id: {result.request_id}'
)
# Traverse and print the details of each region
for rg in result.region_info:
print(f'region: {rg.region},'
f' internet endpoint: {rg.internet_endpoint},'
f' internal endpoint: {rg.internal_endpoint},'
f' accelerate endpoint: {rg.accelerate_endpoint}'
)
# Call the main function to start the processing logic when the script is directly run
if __name__ == "__main__":
main() # Entry point of the script, where the program flow starts
Query the endpoints of a specific region
The sample code below demonstrates how to query the endpoints for a specific region.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line argument parser and describe the purpose of the script: The sample demonstrates how to query the region information supported by OSS
parser = argparse.ArgumentParser(description="describe regions sample")
# Specify the --region parameter, which indicates the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which indicates the name of the bucket. This parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the domain names that other services can use to access OSS. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --regions parameter, which indicates regional information. This parameter is optional.
parser.add_argument('--regions', help='Regional information.')
def main():
# Parse the parameters provided in the command line to obtain the values entered by the user
args = parser.parse_args()
# Load the authentication information required to access OSS from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configurations of the SDK to create a configuration object and specify the credential provider
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If a custom endpoint is provided, modify the endpoint parameter in the configuration object
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS
client = oss.Client(cfg)
# Send a request to obtain region information
result = client.describe_regions(oss.DescribeRegionsRequest(
regions=args.regions, # Use oss-cn-hangzhou as an example for China (Hangzhou). For other regions, specify the actual region.
))
# Print the status code and request ID of the request
print(f'status code: {result.status_code},'
f' request id: {result.request_id}'
)
# Traverse and print the details of each region
for rg in result.region_info:
print(f'region: {rg.region},'
f' internet endpoint: {rg.internet_endpoint},'
f' internal endpoint: {rg.internal_endpoint},'
f' accelerate endpoint: {rg.accelerate_endpoint}'
)
# Call the main function to start the processing logic when the script is directly run
if __name__ == "__main__":
main() # Entry point of the script, where the program flow starts
References
-
For the API interface description of querying the endpoint information for a region, see DescribeRegions.