[RUBY] Note: Image storage using AWS S3

Introduction

I am making an original app with Rails. We have implemented an image posting function in this app. However, when I deployed it on Heroku, the image disappeared after 24 hours (by design). To solve this problem, we used S3 (Simple Storage Service) of AWS (Amazon Web Service). I will write it as a memorandum because I think I will have a chance to use it in the future. In addition, I will omit the account creation and security measures on AWS (summarized in another article).

procedure

  1. Preparing a bucket (data storage location) with S3
  2. Data (image this time) save destination setting

1. Preparing a bucket (data storage location) with S3

Bucket preparation is roughly divided into the following two steps.

** 1-1. Creating a bucket ** Click the Services tab on the AWS official website and select S3. Then click "Create Bucket". Note that the bucket name cannot be saved if someone has already used it.

AWS official website

** 1-2. Bucket policy settings ** Decide what kind of access you want to allow to save to S3 and read data. (This time, set by IAM user.)

-Enter IAM in the search window from the service tag and click the displayed menu. -Click the user on the far left, and click the displayed user name. -Copy the ARN. -Select S3 from the service tab again and select the target bucket. -Click the Permissions tab, Edit bucket policy item. (See below)

{
    "Version": "2012-10-17",
    "Id": "Policy1544152951996",
    "Statement": [
        {
            "Sid": "Stmt1544152948221",
            "Effect": "Allow",
            "Principal": {
                "AWS": "Paste the ARN you copied earlier"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::Target bucket"
        }
    ]
}

2. Data (image this time) save destination setting

This also requires the following four steps.

** 2-1. Required Gem installation ** Add the following Gem to the Gem file and install it on the terminal.

Gemfile


gem "aws-sdk-s3", require: false

Terminal


bundle install

** 2-2. Specify the save destination ** Normally, the save destination is local. Rewrite this to amazon. Also note that the files to be described differ between the local environment and the production environment.

config/environments/development.rb(Local environment)


config.active_storage.service = :amazon

config/environments/production.rb(Production environment)


config.active_storage.service = :amazon

** 2-3. Set environment variables **

config/storage.yml


local:
 service: Disk
 root: <%= Rails.root.join("storage") %>
-------------------------------Add the following----------------------------------
amazon:
 service: S3
 access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
 secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
 region: ap-northeast-1
 bucket:Target bucket name

Set environment variables on the terminal.

Terminal


vim ~/.zshrc

Terminal


export AWS_ACCESS_KEY_ID="Copy the value of Access key ID"
export AWS_SECRET_ACCESS_KEY="Copy the value of Secret access key"

After setting, apply the setting again in the terminal.

Terminal


source ~/.zshrc

** 2-4. Operation check ** Try saving it and it's OK if the object is added to the AWS S3 page target bucket

that's all

Recommended Posts

Note: Image storage using AWS S3
[AWS Lambda] Resize the image saved in S3 and save it in another S3 (using Ruby)
A mistake made when displaying an image using Active Storage
Register your own Docker image with ECR using AWS CLI
Encrypt data uploaded to S3 using AWS SDK for Java / SSE-KMS
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to upload images to AWS S3 using refile and refile-s3