全部產品
Search
文件中心

Object Storage Service:查詢Endpoint資訊(Python SDK V2)

更新時間:Jul 31, 2025

本文介紹如何使用Python SDK V2查詢所有支援地區或者指定地區對應的Endpoint資訊,包括外網訪問(IPv4)Endpoint、內網訪問(傳統網路或VPC網路)Endpoint和傳輸加速網域名稱(全地區上傳下載加速)Endpoint。

注意事項

  • 查詢所有支援地區或者指定地區對應的Endpoint資訊與OSS的地區支援情況相關,與您在該地區是否建立Bucket無關。

  • 本文範例程式碼以華東1(杭州)的地區IDcn-hangzhou為例,預設使用外網Endpoint,如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見OSS地區和訪問網域名稱

查詢所有支援地區對應的Endpoint資訊

以下代碼用於查詢所有支援地區對應的Endpoint資訊。

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:樣本展示如何查詢OSS支援的地區資訊
parser = argparse.ArgumentParser(description="describe regions sample")

# 添加命令列參數 --region,表示儲存空間所在的地區,必需參數
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# 添加命令列參數 --bucket,表示要操作的儲存空間名稱,必需參數
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# 添加命令列參數 --endpoint,表示其他服務可用來訪問OSS的網域名稱,非必需參數
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# 添加命令列參數 --regions,表示地區資訊,非必需參數
parser.add_argument('--regions', help='Regional information.')

def main():
    # 解析命令列提供的參數,擷取使用者輸入的值
    args = parser.parse_args()

    # 從環境變數中載入訪問OSS所需的認證資訊,用於身分識別驗證
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # 使用SDK的預設配置建立設定物件,並設定認證提供者
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # 如果提供了自訂endpoint,則更新設定物件中的endpoint屬性
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # 使用上述配置初始化OSS用戶端,準備與OSS互動
    client = oss.Client(cfg)

    # 發送請求以擷取地區資訊
    result = client.describe_regions(oss.DescribeRegionsRequest(
        regions=args.regions,
    ))

    # 列印請求的結果狀態代碼和請求ID
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}'
    )

    # 遍曆並列印每個地區的詳細資料
    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}'
        )

# 當此指令碼被直接執行時,調用main函數開始處理邏輯
if __name__ == "__main__":
    main()  # 指令碼進入點,控製程序流程從這裡開始

查詢指定地區對應的Endpoint資訊

以下代碼用於查詢指定地區對應的Endpoint資訊。

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:樣本展示如何查詢OSS支援的地區資訊
parser = argparse.ArgumentParser(description="describe regions sample")

# 添加命令列參數 --region,表示儲存空間所在的地區,必需參數
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# 添加命令列參數 --bucket,表示要操作的儲存空間名稱,必需參數
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# 添加命令列參數 --endpoint,表示其他服務可用來訪問OSS的網域名稱,非必需參數
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# 添加命令列參數 --regions,表示地區資訊,非必需參數
parser.add_argument('--regions', help='Regional information.')

def main():
    # 解析命令列提供的參數,擷取使用者輸入的值
    args = parser.parse_args()

    # 從環境變數中載入訪問OSS所需的認證資訊,用於身分識別驗證
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # 使用SDK的預設配置建立設定物件,並設定認證提供者
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # 如果提供了自訂endpoint,則更新設定物件中的endpoint屬性
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # 使用上述配置初始化OSS用戶端,準備與OSS互動
    client = oss.Client(cfg)

    # 發送請求以擷取地區資訊
    result = client.describe_regions(oss.DescribeRegionsRequest(
        regions=args.regions, # 以華東1(杭州)為例,填寫為oss-cn-hangzhou。其它Region請按實際情況填寫
    ))

    # 列印請求的結果狀態代碼和請求ID
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}'
    )

    # 遍曆並列印每個地區的詳細資料
    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}'
        )

# 當此指令碼被直接執行時,調用main函數開始處理邏輯
if __name__ == "__main__":
    main()  # 指令碼進入點,控製程序流程從這裡開始

相關文檔

  • 關於查詢地區對應Endpoint資訊的API介面描述,請參見DescribeRegions