・ Ruby 2.6.6 ・ Rails 5.2.4.1 ・ Refile has been introduced ・ Use devise
Gemfile
gem "refile-s3"
gem "detenv-rails"
Terminal
$ bundle install
Also, I think you will get an access key and a secret access key during the work process, Please keep it in a place where no one else can see it. It is used to set environment variables.
To describe the relationship between refile and AWS Create ** refile.rb ** in ** config / initializers **. In it
config/initializers/refile.rb
require "refile/s3"
aws = {
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
region: "Region name",
bucket: "Bucket name",
}
Refile.cache = Refile::S3.new(prefix: "cache", **aws)
Refile.store = Refile::S3.new(prefix: "store", **aws)
Please describe. For the region name, set it to ** ap-northeast-1 ** if the region is Tokyo. Also, ** AWS_ACCESS_KEY_ID ** and ** AWS_SECRET_ACCESS_KEY ** are set when AWS is set up. I will enter the value you wrote down, but it is not good to enter the value as it is for security reasons, so Define it as an environment setting in another file.
Environment variables are, roughly speaking, passwords that you can use when your computer is running. For more information, please feel free to relax. I think I introduced the gem ** dotenv-rails ** first, but this is in a file called **. env ** It will be reflected. I will write environment variables here. You can create .env in the terminal, but you can also create it manually. Either one is fine, so create it directly under your home directory (where the gemfile etc. is located). For reference, you can do the following in the terminal.
Terminal
$ touch .env
After creating ** .env **, write the contents.
.env
AWS_ACCESS_KEY_ID =access key
AWS_SECRET_ACCESS_KEY =Secret access key
For security reasons, I can't write my key, but with the access key I wrote down here Enter the value of the secret access key. Also, when uploading code to github, it would be bad if I could see this file. Write **. env ** in **. gitignore **.
.gitignore
/.env
Finally, we will set up heroku to use S3. Open the app you want to use S3, open ** Setting **, press ** Reveal Config Vars ** in ** Config Vars **, and enter ** KEY ** and ** VALUE ** I think there will be a column. Enter the value that was set as the environment variable there. AWS_ACCESS_KEY_ID for KEY, ** access key ** for VALUE AWS_SECRET_ACCESS_KEY for KEY, ** secret access key ** for VALUE Please put each.
Setup is completed.
Finally, let's deploy it on heroku and check it.
python
$ git add .
$ git commit -m "commit S3"
$ git push heroku master
$ heroku run rails db:migrate
$ heroku open
I think that the image that disappeared after a certain period of time will remain forever! When you actually click the bucket name from S3 on AWS There is a folder called store, and you can see that the images are in it.
With the above, you can upload images to S3 with the combination of heroku + S3 + refile. I hope it will be helpful to as many people as possible.
Recommended Posts