This topic describes how to query the endpoints of all regions supported by Object Storage Service (OSS) or a specific region, including public endpoints, internal endpoints, and acceleration endpoints.
Usage notes
Only OSS SDK for Python 2.18.0 and later support the query of endpoints.
You can query endpoints based on whether the regions or a specific region is supported by OSS, regardless of whether you have created a bucket in the regions or a specific region.
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, endpoints and open ports.
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.
Query the endpoints of all supported regions
The following code provides an example on how to query the endpoints of all supported regions:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run this sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
service = oss2.Service(auth, endpoint, region=region)
# Query the endpoints of all supported regions.
result = service.describe_regions()
for r in result.regions:
# Display information about all supported regions.
print('region: {0}'.format(r.region))
# Display the public endpoints of all supported regions.
print('internet_endpoint: {0}'.format(r.internet_endpoint))
# Display the internal endpoints of all supported regions.
print('internal_endpoint: {0}'.format(r.internal_endpoint))
# Display the acceleration endpoints of all supported regions.
print('accelerate_endpoint: {0}'.format(r.accelerate_endpoint))
Query the endpoints of a specific region
The following code provides an example on how to query the endpoints of a specific region:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run this sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
service = oss2.Service(auth, endpoint, region=region)
# In this example, the endpoints of the China (Hangzhou) region are queried. If you want to query the endpoints of other regions, replace the value of this parameter with the actual region.
result = service.describe_regions('oss-cn-hangzhou')
for r in result.regions:
# Display information about the region.
print('region: {0}'.format(r.region))
# Display the public endpoint of the region.
print('internet_endpoint: {0}'.format(r.internet_endpoint))
# Display the internal endpoint of the region.
print('internal_endpoint: {0}'.format(r.internal_endpoint))
# Display the acceleration endpoint of the region.
print('accelerate_endpoint: {0}'.format(r.accelerate_endpoint))
References
For more information about the API operation that you can call to query the endpoints of all regions supported by OSS or a specific region, see DescribeRegions.