I struggled quite a bit, so I will write it as a memorandum.
Please read around here. https://github.com/localstack/localstack https://qiita.com/rio_matsui/items/e0c2c772d4579d00a312
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
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
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
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