qcs:ip 限制用户访问 IP,适用于所有请求。192.168.1.0/24 网段和 IP 为 101.226.100.185 或 101.226.100.186 时,拥有上传对象和下载对象的权限。{"version": "2.0","principal": {"qcs": ["qcs::cam::uin/100000000001:uin/100000000002"]},"statement": [{"effect": "allow","action": ["name/cos:PutObject","name/cos:GetObject"],"resource": ["qcs::cos:ap-beijing:uid/1250000000:examplebucket-bj-1250000000/*","qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-gz-1250000000/exampleobject"],"condition": {"ip_equal": {"qcs:ip": ["192.168.1.0/24","101.226.100.185","101.226.100.186"]}}}]}
versionid 表示对象的版本号,关于版本控制相关内容可查看 版本控制概述。您可以在下载对象(GetObject)、删除对象(DeleteObject)时使用请求参数 versionid 指定需要操作的对象版本。versionid 请求参数时,请求默认作用于对象的最新版本。versionid 请求参数为一个空字符串时,等同于不带 versionid 请求参数时。versionid 请求参数为字符串 "null" 的情况。对于一个存储桶在开启版本控制之前上传的对象,开启版本控制后,这批对象的版本号统一是字符串 "null"。cos:versionid 用于限制请求参数 versionid。{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:GetObject"],"condition":{"string_equal":{"cos:versionid":"MTg0NDUxNTc1NjIzMTQ1MDAwODg"}},"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"]}]}
{"statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:GetObject"],"condition":{"string_equal":{"cos:versionid":"MTg0NDUxNTc1NjIzMTQ1MDAwODg"}},"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"]},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:GetObject"],"condition":{"string_not_equal_if_exist":{"cos:versionid":"MTg0NDUxNTc1NjIzMTQ1MDAwODg"}},"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"]}],"version":"2.0"}
versionid 或 versionid 为空字符串时,GetObject 默认获取最新版本的对象。因此,我们可以在条件中使用 string_equal_if_exist:“”,同样会命中 allow 策略,只对获取最新版本的对象的请求进行授权。"condition": {"string_equal_if_exist": {"cos:versionid": ""}}
{"statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:GetObject"],"condition":{"string_equal_if_exist":{"cos:versionid":""}},"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"]},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:GetObject"],"condition":{"string_not_equal":{"cos:versionid":""}},"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"]}],"version":"2.0"}
{"statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:DeleteObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"]},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:DeleteObject"],"condition":{"string_equal":{"cos:versionid":"null"}},"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"]}],"version":"2.0"}
cos:content-length 限制请求头部 Content-Length,进而限制上传对象的文件大小,以方便您更加灵活管理存储空间,避免上传过大、过小文件浪费存储空间与网络带宽。cos:content-length条件键限制子用户(uin:100000000002)上传请求的 Content-Length 头的大小。{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:PutObject","name/cos:PostObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"numeric_less_than_equal":{"cos:content-length":10}}},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:PutObject","name/cos:PostObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"numeric_greater_than_if_exist":{"cos:content-length":10}}}]}
{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:PutObject","name/cos:PostObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"numeric_greater_than_equal":{"cos:content-length":2}}},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:PutObject","name/cos:PostObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"numeric_less_than_if_exist":{"cos:content-length":2}}}]}
cos:content-type 可以对请求的 Content-Type 头部进行限制。cos:content-type 条件键限制子用户(uin:100000000002)上传请求的 Content-Type 头的具体内容。string_equal_ignore_case 和 string_not_equal_ignore_case,原因是:如果使用 string_equal 和string_not_equal,当目的是禁止 text/html 类型上传文件,无法严格禁止设置 Content-Type 如 text/Html、tExt/html 的情况;使用不区分大小写的运算符,则可以起到严格禁止的效果。有关条件运算符,详情可参见 条件运算符。{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:PutObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_equal_ignore_case":{"cos:content-type":"image/jpeg"}}},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:PutObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_not_equal_ignore_case_if_exist":{"cos:content-type":"image/jpeg"}}}]}
response-content-type,用于设置响应中 Content-Type 头部的值。cos:response-content-type,您可以对请求是否必须携带请求参数 response-content-type 参数值做限制。response-content-type 是请求参数,发起请求时需要经过 urlencode,即 response-content-type=image%2Fjpeg,所以在设置 Policy 时,“image/jpeg”也需要经过 urlencode 填写"image%2Fjpeg"。{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:GetObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_equal":{"cos:response-content-type":"image%2Fjpeg"}}},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:GetObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_not_equal_if_exist":{"cos:response-content-type":"image%2Fjpeg"}}}]}
cos:secure-transport 限制请求必须使用 HTTPS 协议。{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:GetObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"bool_equal":{"cos:secure-transport":"true"}}}]}
{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["*"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"bool_equal":{"cos:secure-transport":"false"}}}]}
x-cos-storage-class 在上传对象时指定存储类型或修改对象的存储类型。cos:x-cos-storage-class 限制请求头部 x-cos-storage-class,进而限制可能修改存储类型的请求。STANDARD、MAZ_STANDARD、STANDARD_IA、MAZ_STANDARD_IA、INTELLIGENT_TIERING、MAZ_INTELLIGENT_TIERING、ARCHIVE、DEEP_ARCHIVE。STANDARD。{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:PutObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_equal":{"cos:x-cos-storage-class":"STANDARD"}}},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:PutObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_not_equal_if_exist":{"cos:x-cos-storage-class":"STANDARD"}}}]}
private、public-read、public-read-write、authenticated-read。default、private、public-read、authenticated-read、bucket-owner-read、bucket-owner-full-control。cos:x-cos-acl 限制请求的头部 x-cos-acl,进而限制可能修改对象或存储桶 ACL 的请求。private。{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:PutObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_equal":{"cos:x-cos-acl":"private"}}},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:PutObject"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_not_equal_if_exist":{"cos:x-cos-acl":"private"}}}]}
cos:prefix 限制请求参数 prefix。/ 等),写入存储桶策略前需要先经过 urlencode。folder1/,由于 prefix 的值含有特殊字符/ ,写入存储桶策略前需要先经过 urlencode。因此策略语法描述为folder1%2F。{"statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["name/cos:GetBucket"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_equal":{"cos:prefix":"folder1%2F"}}},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["name/cos:GetBucket"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"string_not_equal_if_exist":{"cos:prefix":"folder1%2F"}}}],"version":"2.0"}
cos:tls-version 限制 HTTPS 请求的 TLS 版本,该条件键为 Numeric 类型,支持输入浮点数,例如 1.0、1.1、1.2 等。请求场景 | 预期 |
HTTPS 请求,TLS 版本为1.0 | 403,失败 |
HTTPS 请求,TLS 版本为1.2 | 200,成功 |
{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["*"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"numeric_equal":{"cos:tls-version":1.2}}}]}
请求场景 | 预期 |
HTTPS 请求,TLS 版本为1.0 | 403,失败 |
HTTPS 请求,TLS 版本为1.2 | 200,成功 |
{"version":"2.0","statement":[{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"allow","action":["*"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"numeric_greater_than_equal":{"cos:tls-version":1.2}}},{"principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000002"]},"effect":"deny","action":["*"],"resource":["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition":{"numeric_less_than_if_exist":{"cos:tls-version":1.2}}}]}
qcs:request_tag 限制用户发起请求 PutBucket、PutBucketTagging 必须携带指定的存储桶标签。<a,b> 和 <c,d>,才能获得授权。for_any_value:string_equal 表示 A 和 B 存在交集时生效。for_all_value:string_equal 表示 A 是 B 的子集时生效。for_any_value:string_equal 时,对应的策略和请求表现如下:请求场景 | 预期 |
PutBucket,请求头部 x-cos-tagging: a=b&c=d | 200,成功 |
PutBucket,请求头部 x-cos-tagging: a=b | 200,成功 |
PutBucket,请求头部 x-cos-tagging: a=b&c=d&e=f | 200,成功 |
{"version": "2.0","statement": [{"principal": {"qcs": ["qcs::cam::uin/100000000001:uin/100000000002"]},"effect": "allow","action": ["name/cos:PutBucket"],"resource": "*","condition": {"for_any_value:string_equal": {"qcs:request_tag": ["a&b","c&d"]}}}]}
for_all_value:string_equal 时,对应的策略和请求表现如下:请求场景 | 预期 |
PutBucket,请求头部 x-cos-tagging: a=b&c=d | 200,成功 |
PutBucket,请求头部 x-cos-tagging: a=b | 200,成功 |
PutBucket,请求头部 x-cos-tagging: a=b&c=d&e=f | 403,失败 |
{"version": "2.0","statement": [{"principal": {"qcs": ["qcs::cam::uin/100000000001:uin/100000000002"]},"effect": "allow","action": ["name/cos:PutBucket"],"resource": "*","condition": {"for_all_value:string_equal": {"qcs:request_tag": ["a&b","c&d"]}}}]}
x-cos-forbid-overwrite 头部且值必须为字符串 true。{"version": "2.0","statement": [{"principal": {"qcs": ["qcs::cam::uin/100000000001:uin/100000000002"]},"effect": "allow","action": ["name/cos:PutObject"],"resource": ["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition": {"string_equal": {"cos:x-cos-forbid-overwrite": "true"}}},{"principal": {"qcs": ["qcs::cam::uin/100000000001:uin/100000000002"]},"effect": "deny","action": ["name/cos:PutObject"],"resource": ["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition": {"string_not_equal_if_exist": {"cos:x-cos-forbid-overwrite": "true"}}}]}
{"version": "2.0","statement": [{"principal": {"qcs": ["qcs::cam::uin/100000000001:uin/100000000002"]},"effect": "deny","action": ["*"],"resource": ["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition": {"string_equal": {"cos:host": "examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com"}}},{"principal": {"qcs": ["qcs::cam::uin/100000000001:uin/100000000002"]},"effect": "allow","action": ["*"],"resource": ["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"],"condition": {"string_not_equal": {"cos:host": "examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com"}}}]}
{"version": "2.0","statement": [{"principal": {"qcs": ["qcs::cam::uin/100000000001:uin/100000000002"]},"effect": "allow","action": ["name/cos:GetObject"],"resource": ["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/folder1/*"],"condition": {"string_equal": {"cos:host": "mydomain1.com"}}},{"principal": {"qcs": ["qcs::cam::uin/100000000001:uin/100000000002"]},"effect": "deny","action": ["name/cos:GetObject"],"resource": ["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/folder1/*"],"condition": {"string_not_equal": {"cos:host": "mydomain1.com"}}}]}
cos:object-lock-mode 限制用户上传对象必须设置对象锁定,并使用固定的模式。{"statement": [{"action": ["name/cos:PutObject","name/cos:InitiateMultipartUpload","name/cos:PutObjectRetention"],"effect": "allow","principal": {"qcs": ["qcs::cam::uin/1250000000:uin/1250000001"]},"resource": ["qcs::cos:ap-beijing:uid/1250000000:bjtest-1250000000/*"],"condition": {"string_equal": {"cos:object-lock-mode": "COMPLIANCE"}}}],"version": "2.0"}
cos:object-lock-remaining-retention-days 限制用户上传对象必须设置对象锁定,并设置固定的天数。保留天数(B) = 向下取整[(ts1 - ts2)/(3600*24)]
{"statement": [{"action": ["name/cos:PutObject","name/cos:InitiateMultipartUpload","name/cos:PutObjectRetention"],"effect": "allow","principal": {"qcs": ["qcs::cam::uin/1250000000:uin/1250000001"]},"resource": ["qcs::cos:ap-beijing:uid/1250000000:bjtest-1250000000/*"],"condition": {"numeric_greater_than": {"cos:object-lock-remaining-retention-days": 3}}}],"version": "2.0"}
条件键 | 含义 | 请求当前时间 | 入参:retain-until-date 的合法时间 | 备注 |
"numeric_equal": { "cos: x-cos-object-lock-remaining-retention-days": 3 } | 等于3天 | 2022-11-01T12:00:00Z | [ 2022-11-04T12:00:00Z, 2022-11-05T11:59:59Z ] | 闭区间 |
"numeric_greater_than": { "cos: x-cos-object-lock-remaining-retention-days": 3 } | 大于3天(不包括3天) | 2022-11-01T12:00:00Z | [ 2022-11-05T12:00:00Z, 之后 ] | 闭区间 |
"numeric_less_than": { "cos: x-cos-object-lock-remaining-retention-days": 3 } | 小于3天(不包括3天) | 2022-11-01T12:00:00Z | [ 2022-11-01T12:00:01Z, 2022-11-04T11:59:59Z ] | 闭区间 |
cos:object-lock-retain-until-date 限制用户上传对象必须设置对象锁定,并设置指定日期,最小支持设置到整秒。2022-11-11T12:00:00Z。{"statement": [{"action": ["name/cos:PutObject","name/cos:InitiateMultipartUpload","name/cos:PutObjectRetention"],"effect": "allow","principal": {"qcs": ["qcs::cam::uin/1250000000:uin/1250000001"]},"resource": ["qcs::cos:ap-beijing:uid/1250000000:bjtest-1250000000/*"],"condition": {"date_greater_than": {"cos:object-lock-retain-until-date": "2022-11-11T12:00:00Z"}}}],"version": "2.0"}
条件键 | 对应请求头部 |
cos:x-cos-grant-full-control | x-cos-grant-full-control |
cos:x-cos-grant-read | x-cos-grant-read |
cos:x-cos-grant-write | x-cos-grant-write |
cos:x-cos-grant-read-acp | x-cos-grant-read-acp |
cos:x-cos-grant-write-acp | x-cos-grant-write-acp |
"符号,在策略中使用时作为字符串传入,注意需要进行转义\\"。{"statement": [{"action": ["name/cos:PutObject","name/cos:PostObject","name/cos:AppendObject","name/cos:InitiateMultipartUpload"],"effect": "allow","principal": {"qcs": ["qcs::cam::uin/1250000000:uin/1250000001"]},"resource": ["qcs::cos:ap-beijing:uid/1250000000:bjtest-1250000000/*"],"condition": {"string_equal": {"cos:x-cos-grant-full-control": "id=\\"100000000001\\""}}},{"action": ["name/cos:PutObject","name/cos:PostObject","name/cos:AppendObject","name/cos:InitiateMultipartUpload"],"effect": "deny","principal": {"qcs": ["qcs::cam::uin/1250000000:uin/1250000001"]},"resource": ["qcs::cos:ap-beijing:uid/1250000000:bjtest-1250000000/*"],"condition": {"string_not_equal_if_exist": {"cos:x-cos-grant-full-control": "id=\\"100000000001\\""}}}],"version": "2.0"}
{"statement": [{"action": ["name/cos:PutObject","name/cos:PostObject","name/cos:AppendObject","name/cos:InitiateMultipartUpload"],"effect": "allow","principal": {"qcs": ["qcs::cam::uin/1250000000:uin/1250000001"]},"resource": ["qcs::cos:ap-beijing:uid/1250000000:bjtest-1250000000/*"],"condition": {"string_equal_if_exist": {"cos:x-cos-grant-full-control": ""}}},{"action": ["name/cos:PutObject","name/cos:PostObject","name/cos:AppendObject","name/cos:InitiateMultipartUpload"],"effect": "deny","principal": {"qcs": ["qcs::cam::uin/1250000000:uin/1250000001"]},"resource": ["qcs::cos:ap-beijing:uid/1250000000:bjtest-1250000000/*"],"condition": {"string_not_equal": {"cos:x-cos-grant-full-control": ""}}}],"version": "2.0"}
文档反馈