全部產品
Search
文件中心

Object Storage Service:存取點層級阻止公用訪問(Python SDK V2)

更新時間:Jul 31, 2025

本文介紹如何使用Python SDK V2管理OSS存取點層級阻止公用訪問的功能。

注意事項

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

範例程式碼

為存取點開啟阻止公用訪問

您可以使用以下代碼開啟存取點阻止公用訪問。

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:設定存取點(Access Point)的阻止公用訪問配置(Public Access Block)
parser = argparse.ArgumentParser(description="put access point public access block sample")

# 定義命令列參數,包括必需的地區、儲存空間名稱、存取點名稱、可選的endpoint以及是否啟用阻止公用訪問
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--access_point_name', help='The name of the access point.', required=True)
parser.add_argument('--block_public_access',
                    help='Specifies whether to enable Block Public Access. '
                         'true: enables Block Public Access. '
                         'false (default): disables Block Public Access.',
                    default='false')

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

    # 從環境變數中載入訪問憑證資訊,用於身分識別驗證
    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.put_access_point_public_access_block(oss.PutAccessPointPublicAccessBlockRequest(
            bucket=args.bucket,  # 儲存空間名
            access_point_name=args.access_point_name,  # 存取點名稱
            public_access_block_configuration=oss.PublicAccessBlockConfiguration(
                block_public_access=args.block_public_access == 'true',  # 是否啟用阻止公用訪問
            ),
    ))

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

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

擷取指定存取點的阻止公用訪問配置資訊

您可以使用以下代碼擷取存取點阻止公用訪問的配置資訊。

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:擷取存取點(Access Point)的阻止公用訪問配置(Public Access Block)
parser = argparse.ArgumentParser(description="get access point public access block sample")

# 定義命令列參數,包括必需的地區、儲存空間名稱、存取點名稱以及可選的endpoint
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--access_point_name', help='The name of the access point.', required=True)

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

    # 從環境變數中載入訪問憑證資訊,用於身分識別驗證
    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.get_access_point_public_access_block(oss.GetAccessPointPublicAccessBlockRequest(
            bucket=args.bucket,  # 儲存空間名
            access_point_name=args.access_point_name,  # 存取點名稱
    ))

    # 列印操作結果的狀態代碼、請求ID和阻止公用訪問配置狀態,以便確認請求狀態和配置詳情
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' block public access: {getattr(result.public_access_block_configuration, "block_public_access", "Not set")},'
          )

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

刪除指定存取點的阻止公用訪問配置資訊

您可以使用以下代碼刪除存取點阻止公用訪問的配置資訊。

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:刪除存取點(Access Point)的阻止公用訪問配置(Public Access Block)
parser = argparse.ArgumentParser(description="delete access point public access block sample")

# 定義命令列參數,包括必需的地區、儲存空間名稱、存取點名稱以及可選的endpoint
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--access_point_name', help='The name of the access point.', required=True)

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

    # 從環境變數中載入訪問憑證資訊,用於身分識別驗證
    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.delete_access_point_public_access_block(oss.DeleteAccessPointPublicAccessBlockRequest(
            bucket=args.bucket,  # 儲存空間名
            access_point_name=args.access_point_name,  # 存取點名稱
    ))

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

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