S3 IAM ポリシーを使用して、ペーパークリップでアップロードを行うことができません。jQuery の直接アップロード (ペーパークリップなし) にも問題があります。私のシナリオは次のとおりです。多くのサイトを持つアプリケーションがあります。各サイトには独自のバケットがあり、他のサイトではなく、独自のバケットにのみアクセスできる必要があります。IAM サンプル ポリシーのドキュメントでは、「例: 各 IAM ユーザーにバケット内のフォルダーへのアクセスを許可する」の下で何をしたいのかを正確に説明しています。アプリケーション用に設定された IAM グループがあり、グループ内のサイトごとに 1 人のユーザーがいます。これらの 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 構成は次のとおりです。もちろん開発用です。後でロックダウンされます。
<?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>
ペーパークリップの設定は次のとおりです。
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}/*"]
}
]
}
そして私の更新されたペーパークリップ設定:
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"
ペーパークリップ パスにユーザー名を含めることが重要でした。私は Amazon が認証情報からそれを推測すると思っていましたが、そうではありません。