我试图设置,因此存储桶可以容纳的唯一文件类型是png,jpeg和gif图像。我试图像这样放入一个桶政策
{
"conditions": [
{"bucket": "bucketname"},
["starts-with", "$Content-Type", "image/jpeg"],
["starts-with", "$Content-Type", "image/png"],
["starts-with", "$Content-Type", "image/gif"],
["content-length-range", 0, 10485760]
]
}
然后我也试图限制大小,但当我尝试更新我的政策时,我收到错误"无效的政策要素 - 条件"
我尝试使用此处的答案 - s3 direct upload restricting file size and type以便我从中制作代码,但我不确定这样做的正确方法,因为我的政策并非如此甚至被亚马逊接受。
答案 0 :(得分:10)
如果你不确定如何写,你可以使用策略生成器,例如你会有类似的东西
{
"Id": "Policy1464968545158",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1464968483619",
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::<yourbucket>/*.jpg",
"Principal": "*"
},
{
"Sid": "Stmt1464968543787",
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::<yourbucket>/*.png",
"Principal": "*"
}
]
}
如doc所述,您可以指定多个资源并聚合此部分,因此无需将该语句相乘
"Resource": [
"arn:aws:s3:::<yourbucket>/*.jpg",
"arn:aws:s3:::<yourbucket>/*.png",
"arn:aws:s3:::<yourbucket>/*.gif",
],
所以你得到像
这样的东西{
"Id": "Policy1464968545158",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1464968483619",
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::<yourbucket>/*.jpg",
"arn:aws:s3:::<yourbucket>/*.png",
"arn:aws:s3:::<yourbucket>/*.gif",
],
"Principal": "*"
}
]
}
您可以在创建存储桶策略时访问策略生成器
答案 1 :(得分:0)
我与AWS支持工程师交谈,conditions.starts-with
限制仅受HTTP POST策略(例如:浏览器表单字段上载请求的策略)支持。使用此策略,当您或您的用户使用HTTP PUT请求上传文件时,应该不可能限制mineType
。
对于通用政策,您可以在此处https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazons3.html看到可用的Condition
键
我发现还有另一种解决方案可以限制mineType,
<input type="file" accept="image/bmp,image/jpeg,image/png,image/gif"/>
bmp
,jpeg
,png
和gif
,并且可以在上传之前将它们设置为S3对象的文件URL扩展名。最后,您可以添加一般策略来限制文件URL扩展,如下所示。参考:https://aws.amazon.com/premiumsupport/knowledge-center/s3-allow-certain-file-types/
{
"Sid": "Stmt1464968483619",
"Effect": "Allow",
"Principal": {
"AWS": "IAM-USER-ARN"
},
"Action": "s3:PutObject",
"Resource": [
"arn:aws:s3:::bucket-name/*.bmp",
"arn:aws:s3:::bucket-name/*.jpeg",
"arn:aws:s3:::bucket-name/*.png",
"arn:aws:s3:::bucket-name/*.gif"
]
}
以便您从浏览器端限制mimeType