# -*- coding: utf-8 -*-
import os

from ks3.connection import Connection
from ks3.xmlParsers.publicNetworkBlock import PublicNetworkBlock, BucketPublicNetworkBlock

# 首先初始化AccessKeyId、AccessKeySecret、Endpoint等信息。
# 通过环境变量获取，或者把诸如"<你的AccessKeyId>"替换成真实的AccessKeyId等。
ak = os.getenv('KS3_IAM_ACCESS_KEY_ID', '<YOUR_ACCESS_KEY>')
sk = os.getenv('KS3_IAM_ACCESS_KEY_SECRET', '<YOUR_SECRET_KEY>')
bucket_name = os.getenv('KS3_BUCKET', '<KS3_TEST_BUCKET>')
endpoint = 'ks3-cn-beijing.ksyuncs.com'

conn = Connection(ak, sk, host=endpoint, is_secure=False)

def set_global_public_network_block():
    """设置全局公共网络访问阻止策略"""
    try:
        # 创建公共网络阻止配置
        pnb = PublicNetworkBlock(block_type='All')

        # 设置全局公共网络访问阻止
        resp = conn.set_public_network_block(pnb)
        print("设置全局公共网络访问阻止成功，request_id:", resp.response_metadata.request_id)
    except Exception as e:
        print("设置全局公共网络访问阻止失败:", e)
        if hasattr(e, 'request_id'):
            print("request_id:", e.request_id)

def get_global_public_network_block():
    """获取全局公共网络访问阻止策略"""
    try:
        # 获取全局公共网络访问阻止配置
        pnb = conn.get_public_network_block()
        print("获取全局公共网络访问阻止成功:")
        print("BlockType:", pnb.block_type)
        print("request_id:", pnb.response_metadata.request_id)
    except Exception as e:
        print("获取全局公共网络访问阻止失败:", e)
        if hasattr(e, 'request_id'):
            print("request_id:", e.request_id)

def delete_global_public_network_block():
    """删除全局公共网络访问阻止策略"""
    try:
        # 删除全局公共网络访问阻止配置
        resp = conn.delete_public_network_block()
        print("删除全局公共网络访问阻止成功，request_id:", resp.response_metadata.request_id)
    except Exception as e:
        print("删除全局公共网络访问阻止失败:", e)
        if hasattr(e, 'request_id'):
            print("request_id:", e.request_id)

def set_bucket_public_network_block():
    """设置Bucket级别的公共网络访问阻止策略"""
    try:
        # 获取Bucket对象
        bucket = conn.get_bucket(bucket_name)

        # 创建Bucket公共网络阻止配置
        bpnb = BucketPublicNetworkBlock(block_type='0')

        # 设置Bucket公共网络访问阻止
        resp = bucket.set_public_network_block(bpnb)
        print("设置Bucket公共网络访问阻止成功，request_id:", resp.response_metadata.request_id)
    except Exception as e:
        print("设置Bucket公共网络访问阻止失败:", e)
        if hasattr(e, 'request_id'):
            print("request_id:", e.request_id)

def get_bucket_public_network_block():
    """获取Bucket级别的公共网络访问阻止策略"""
    try:
        # 获取Bucket对象
        bucket = conn.get_bucket(bucket_name)

        # 获取Bucket公共网络访问阻止配置
        bpnb = bucket.get_public_network_block()
        print("获取Bucket公共网络访问阻止成功:")
        print("BlockType:", bpnb.block_type)
        print("request_id:", bpnb.response_metadata.request_id)
    except Exception as e:
        print("获取Bucket公共网络访问阻止失败:", e)
        if hasattr(e, 'request_id'):
            print("request_id:", e.request_id)

def delete_bucket_public_network_block():
    """删除Bucket级别的公共网络访问阻止策略"""
    try:
        # 获取Bucket对象
        bucket = conn.get_bucket(bucket_name)

        # 删除Bucket公共网络访问阻止配置
        resp = bucket.delete_public_network_block()
        print("删除Bucket公共网络访问阻止成功，request_id:", resp.response_metadata.request_id)
    except Exception as e:
        print("删除Bucket公共网络访问阻止失败:", e)
        if hasattr(e, 'request_id'):
            print("request_id:", e.request_id)

if __name__ == "__main__":
    print("=== Public Network Block 示例 ===")

    # 演示全局公共网络访问阻止功能
    print("\n1. 设置全局公共网络访问阻止:")
    set_global_public_network_block()

    print("\n2. 获取全局公共网络访问阻止:")
    get_global_public_network_block()

    print("\n3. 删除全局公共网络访问阻止:")
    delete_global_public_network_block()

    # 演示Bucket级别的公共网络访问阻止功能
    print("\n4. 设置Bucket公共网络访问阻止:")
    set_bucket_public_network_block()

    print("\n5. 获取Bucket公共网络访问阻止:")
    get_bucket_public_network_block()

    print("\n6. 删除Bucket公共网络访问阻止:")
    delete_bucket_public_network_block()

    print("\n=== 示例结束 ===")