[英]Permission denied when uploading image to Amazon S3 with Paperclip
[英]Access Denied when uploading files to Amazon using Paperclip and IAM policies
我無法使用S3 IAM策略使用Paperclip上傳。 我甚至遇到直接jQuery上傳的問題(沒有Paperclip)。 我的方案如下,我有一個應用程序,將有許多網站。 每個站點都有自己的桶,應該只能訪問自己的桶,沒有其他人可以訪問。 IAM示例策略文檔在“示例:允許每個IAM用戶訪問存儲桶中的文件夾”下准確說明了我想要執行的操作。 我為應用程序設置了一個IAM組,並且該組中的每個站點都有一個用戶。 這些IAM用戶屬於該組。 該小組的政策如下:
{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Action":[
"s3:PutObject",
"s3:GetObject",
"s3:GetObjectVersion",
"s3:DeleteObject",
"s3:DeleteObjectVersion"
],
"Resource":"arn:aws:s3:::my-app/${aws:username}/*"
}
]
}
這是我在存儲桶上的CORS配置,當然,dev會在以后被鎖定:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
這是我的Paperclip設置:
has_attached_file :background_image,
storage: :s3,
s3_credentials: {
access_key_id: "xxx",
secret_access_key: "xxx"
},
bucket: "my-app",
s3_permissions: "public-read",
path: "/background_images/:id/:filename"
我以前在使用策略時直接使用策略,這確實有效,但是當我進入具有許多“站點”的生產環境時,它不像我需要的那樣靈活。 據我所知,我已完全遵循文檔,但我所做的任何事都會導致“訪問被拒絕”。 此時我甚至不確定我的問題是我的IAM策略還是我的Paperclip配置。
編輯:澄清。
編輯2:最終解決方案
以下是基於本文的最終IAM策略:
{
"Version":"2012-10-17",
"Statement": [
{
"Sid": "AllowUserToSeeBucketListInTheConsole",
"Action": ["s3:ListAllMyBuckets", "s3:GetBucketLocation"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::*"]
},
{
"Sid": "AllowRootAndHomeListingOfCompanyBucket",
"Action": ["s3:ListBucket"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::my-app"],
"Condition":{"StringEquals":{"s3:prefix":["","home/"],"s3:delimiter":["/"]}}
},
{
"Sid": "AllowListingOfUserFolder",
"Action": ["s3:ListBucket"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::estimator-app"],
"Condition":{"StringLike":{"s3:prefix":["home/${aws:username}/*"]}}
},
{
"Sid": "AllowAllS3ActionsInUserFolder",
"Effect": "Allow",
"Action": ["s3:*"],
"Resource": ["arn:aws:s3:::my-app/home/${aws:username}/*"]
}
]
}
我更新的Paperclip設置:
has_attached_file :background_image,
storage: :s3,
s3_credentials: {
access_key_id: "xxx",
secret_access_key: "xxx"
},
bucket: "estimator-app",
s3_permissions: "public-read",
path: "/home/my_s3_username/background_images/:id/:filename"
在Paperclip路徑中包含用戶名非常重要。 我假設亞馬遜會從憑證推斷出這一點,但事實並非如此。
因為您嘗試為上載的對象設置權限,所以還需要為IAM用戶提供s3:PutObjectAcl
權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.