获取高性能桶当前生效的存储桶策略(GET Bucket Policy)。返回内容为大写 key 的 JSON,
Version 固定为 2.0。该接口属于控制面,使用基础凭证签名,不走 session。注意事项
当桶上未设置任何策略时,接口会返回 404
NoSuchPolicy,可通过错误码判断。返回的
Policy 为大写 key 的 JSON 字符串,需要按 JSON 解析后使用。获取存储桶策略属于控制面接口,使用基础凭证签名,不走 session。
请求
方法原型
def get_bucket_policy(self, Bucket, **kwargs)
参数说明
参数名称 | 参数描述 | 类型 | 是否必选 |
Bucket | 高性能桶完整名称,格式为 <BucketName>-x--<APPID> | string | 是 |
kwargs | 可选请求参数,使用本文列出的参数 | dict | 否 |
响应
返回值为字典:
参数名称 | 参数描述 | 类型 |
Policy | 桶策略内容,JSON 字符串,与设置时的策略字段相同 | string |
错误码
实际案例
# -*- coding=utf-8 -*-import jsonimport osfrom qcloud_cos import CosConfig, CosS3Client# 高性能桶经 VPC 内网 HTTP 80 端口接入,SDK 的网关 DNS 负载均衡也仅对 HTTP 请求生效;SDK 默认 https,此处需显式指定 http。config = CosConfig(Scheme='http',Region='ap-guangzhou',SecretId=os.environ['COS_SECRET_ID'],SecretKey=os.environ['COS_SECRET_KEY'],Token=os.environ.get('COS_SECURITY_TOKEN'),EnableRapidDomain=True)client = CosS3Client(config)resp = client.get_bucket_policy(Bucket='examplebucket-x--1250000000')policy = json.loads(resp['Policy'])print('Version: %s' % policy.get('Version'))for stmt in policy.get('Statement', []):print('Sid: %s, Effect: %s, Action: %s, Resource: %s' % (stmt.get('Sid'), stmt.get('Effect'), stmt.get('Action'), stmt.get('Resource')))