[RAILS] Procedure to use S3 of LocalStack for Active Storage

I struggled quite a bit, so I will write it as a memorandum.

What is LocalStack?

Please read around here. https://github.com/localstack/localstack https://qiita.com/rio_matsui/items/e0c2c772d4579d00a312

LocalStack settings

For the time being, write as follows in docker-compose.yml

  localstack:
    image: localstack/localstack:0.12.1
    environment:
      SERVICES: s3
      AWS_ACCESS_KEY: 1234
      AWS_SECRET_ACCESS_KEY: 12345678
      DEFAULT_REGION: ap-northeast-1
      DATA_DIR: /data
      START_WEB: 0
      HOSTNAME_EXTERNAL: localstack
      INIT_SCRIPTS_PATH: /root/init
    volumes:
      - localstack-data:/data
      - ./docker/localstack:/root/init
    ports:
      - 4566:4566

volumes:
  localstack-data:
    driver: local

Explanation below

Environment variable

SERVICES: s3

Describe the service you want to use here, and if you want to use more than one, write them separated by commas.

AWS_ACCESS_KEY: 1234
AWS_SECRET_ACCESS_KEY: 12345678

This is appropriate, SECRET is angry if it is not more than 8 characters

DEFAULT_REGION: ap-northeast-1

For the time being, this is also your choice.

DATA_DIR: /data

Volume, the storage location for the data to be persisted, is created and mounted in this directory.

START_WEB: 0

LocalStack has something like a management screen, If you want to use it, you need to use the image localstack / localstack-full. If it is localstack / localstack, the management screen cannot be used, but when I look at the log, I am throwing an error when trying to start it, so disable it.

HOSTNAME_EXTERNAL: localstack

The reason is probably the same as written here. If you do not write this, you will not be able to access from the container on the Rails side. https://qiita.com/imunew/items/b74cecb7e12a9b9c4441

INIT_SCRIPTS_PATH: /root/init

Location of the initialization script If you write a script like the one below here, it will be executed at startup.

cd /root/init
#Since the container contains aws local, create a bucket using that
awslocal s3 mb s3://test-bucket

This time, the created script is mounted on the container side and executed.

      - ./docker/localstack:/root/init

See here for environment variables not listed here https://github.com/localstack/localstack#configurations

Port

    ports:
      - 4566:4566

LocalStack accepts access with 4566, so set port forwarding By the way, when hitting with awscli, specify Endpoint as shown below.

aws --endpoint=http://localhost:4566 s3 ls s3://test-bucket --recursive

If you are from inside the container, the aws local command is included, so it is easier to use that.

When you start it with this, Bucket should be created in S3.

aws --endpoint=http://localhost:4566 s3 ls                                                                            
2020-11-02 10:12:10 test-bucket

Rails side settings

See this page for the settings for using S3 with Active Storage. https://qiita.com/tsubasan1122/items/0171fe04754a760f7e4a

Only the settings required to use LocalStack are described here.

storage.yml

localstack:
  service: S3
  access_key_id: 1234
  secret_access_key: 12345678
  region: ap-northeast-1
  endpoint: http://localstack:4566
  force_path_style: true
  bucket: test-bucket

Explanation below

  service: S3
  access_key_id: 1234
  secret_access_key: 12345678
  region: ap-northeast-1

This should match the settings on the LocalStack side

  endpoint: http://localstack:4566
  force_path_style: true

Set to change the tap destination of aws api to the localstack side. However, as it is, when accessing S3, it will try to access with http: // # {endpoint}. # {Bucket name}. Avoid by setting the option force_path_style to true.

It should now be saved on the LocalStack side. Check below

#Save the image properly
file = "1.jpg "
content_type = 'image/jpg'
image = Image.first
image.image.attach(io: File.open(file), filename: File.basename(file), content_type: content_type)

#Check the key of the image of S3
image.image.attachment.blob.key
"p1fp3sd0mqxscrvcx960nqa7caal"

#Check if the image is saved on the localstack side
aws --endpoint=http://localhost:4566 s3 ls s3://gpa-dev-image                                                              
2020-11-02 11:59:17      14845 p1fp3sd0mqxscrvcx960nqa7caal #The same key exists

Other

I can save the file, but the domain of the URL that I get when I try to display the image in the browser is http: // localstack: 4566. This area was supported by creating a method that returns a URL as shown in the page below. https://qiita.com/shouta-dev/items/0b9569bf5522cca4cf3c

Recommended Posts

Procedure to use S3 of LocalStack for Active Storage
[Rails] How to use Active Storage
The story of migrating from Paperclip to Active Storage
[Rails] Create API to download files with Active Storage [S3]
Move Active Storage on S3 locally
How to use setDefaultCloseOperation () of JFrame
From introduction to use of ActiveHash
Util to use for some reason
[For Rails beginners] Summary of how to use RSpec (get an overview)
I want you to use Enum # name () for the Key of SharedPreference
How to use binding.pry for view files
About the procedure for java to work
[Ruby] How to use slice for beginners
[Java] [Maven3] Summary of how to use Maven3
Problem of slow processing when using Active Storage with cloud storage (GCS, S3, etc)
Use jenv to enable multiple versions of Java
Procedure for loading JDBC of MySQL using JAVA-Eclipse
How to use an array for HashMap keys
Procedure to change lower_case_table_names = 1 in MySQL 8.0 of CentOS 8.3
Output of how to use the slice method
Criteria for proper use of render and redirect_to
How to use enum (introduction of Japanese notation)
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
How to link images using FactoryBot Active Storage
How to use nginx-ingress-controller with Docker for Mac
[For super beginners] How to use autofocus: true
[Introduction to Java] Basics of java arithmetic (for beginners)
How to specify index of JavaScript for statement
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~