TL;博士
您必须将file://
协议方案添加到策略文件路径
$ aws s3api put-bucket-policy --bucket kryptonite \
--policy file:///home/superman/aws-example/public-bucket-policy.json
或(
视窗)
$ aws s3api put-bucket-policy --bucket kryptonite \
--policy file://C:\Temp\public-bucket-policy.json
或(
相对路径)
$ aws s3api put-bucket-policy --bucket kryptonite \
--policy file://public-bucket-policy.json
完整故事:如何通过 CLI 设置公共存储桶策略
首先让我们确保桶上没有其他策略:
$ s3api get-bucket-policy --bucket kryptonite
A client error (NoSuchBucketPolicy) occurred when calling the GetBucketPolicy
operation: The bucket policy does not exist
现在让我们确保我们在当前目录中有策略文件并且它包含有效的 json(kryptonite
存储桶的名字
$ ls
public-bucket-policy.json
$ cat public-bucket-policy.json
{
"Statement": [
{
"Resource": "arn:aws:s3:::kryptonite/*",
"Action": "s3:GetObject",
"Principal": "*",
"Effect": "Allow",
"Sid": "AddPerm"
}
],
"Version": "2012-10-17"
}
现在让我们尝试通过仅指定文件名来放置策略
$ s3api put-bucket-policy --bucket kryptonite --policy public-bucket-policy.json
A client error (MalformedPolicy) occurred when calling the PutBucketPolicy
operation: Policies must be valid JSON and the first byte must be '{'
现在让我们再次尝试并指定完整路径 $ s3api put-bucket-policy --bucket kryptonite \ --policy /home/superman/aws-example/public-bucket-policy.json
A client error (MalformedPolicy) occurred when calling the PutBucketPolicy
operation: Policies must be valid JSON and the first byte must be '{'
现在让我们添加file://
前缀,它将起作用
$ s3api put-bucket-policy --bucket kryptonite \
--policy file:///home/superman/aws-example/public-bucket-policy.json
我们现在可以验证是否已应用此策略
$ s3api get-bucket-policy --bucket kryptonite
{
"Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"AddPerm\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::kryptonite/*\"}]}"
}
作为特别的奖励,让我们通过jq
实用程序(两次)管道策略以提取正确的字段并很好地格式化 JSON
$ s3api get-bucket-policy --bucket kryptonite | jq .Policy --raw-output | jq .
{
"Statement": [
{
"Resource": "arn:aws:s3:::kryptonite/*",
"Action": "s3:GetObject",
"Principal": "*",
"Effect": "Allow",
"Sid": "AddPerm"
}
],
"Version": "2012-10-17"
}
如您所见,该政策是正确的