LoginSignup
2
1

More than 5 years have passed since last update.

Rails5.2のActiveStorageでS3ダイレクトアップロードするときのS3のCORSとIAM設定メモ

Posted at

細かな点で誤ってるかもしれませんが、メモ書きです。

確認バージョン

  • Rails 5.2.1
  • ActiveStorage 5.2.2

S3のCORS設定

ダイレクトアップロード時はPUT、ダイレクトダウンロード時はGETが必要になります。
本番環境では、AllowedOriginに本番環境のホストをいれるぽいです。(Ex: https:://www.service.co.jpなど)

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

IAM Policy

最低限Put,Get,List,Deleteが必要。
アップロード時にACLを設定したい場合は、GetObjectAclが必要かも?

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:ListBucket",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::<バケット名>",
                "arn:aws:s3:::<バケット名>/*"
            ]
        }
    ]
}

参考: https://guides.rubyonrails.org/active_storage_overview.html#amazon-s3-service

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1