Use o OSS SDK for Python V2 para consultar endpoints de todas as regiões suportadas ou de uma região específica, incluindo o endpoint de Internet (IPv4), o endpoint interno (rede clássica ou VPC) e o endpoint de aceleração de transferência.
Observações
É possível consultar endpoints de todas as regiões suportadas ou de uma região específica, mesmo sem buckets criados nessas regiões.
O código de exemplo neste tópico usa o ID da região
cn-hangzhou, referente à China (Hangzhou), e adota o endpoint público por padrão. Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use o endpoint interno. Para obter mais informações sobre o mapeamento entre regiões e endpoints suportados pelo OSS, consulte Regiões e endpoints do OSS.
Consultar os endpoints de todas as regiões suportadas
O exemplo de código a seguir consulta os endpoints de todas as regiões suportadas.
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
Consultar os endpoints de uma região específica
O exemplo de código a seguir consulta os endpoints de uma região específica.
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
Referências
Para obter a referência completa da API, consulte DescribeRegions.