接入点级别阻止公共访问

更新时间:2025-03-07 06:32:59

本文介绍如何使用Python SDK V2管理OSS接入点级别阻止公共访问的功能。

注意事项

  • 本文示例代码以华东1(杭州)的地域IDcn-hangzhou为例,默认使用外网Endpoint,如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的RegionEndpoint的对应关系,请参见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()  # 脚本入口点,控制程序流程从这里开始
  • 本页导读 (1)
  • 注意事项
  • 示例代码
  • 为接入点开启阻止公共访问
  • 获取指定接入点的阻止公共访问配置信息
  • 删除指定接入点的阻止公共访问配置信息