upload images to refile heroku S3

Prerequisites

・ Ruby 2.6.6 ・ Rails 5.2.4.1 ・ Refile has been introduced ・ Use devise

Overview Upload images to S3 with a combination of heroku + S3 + refile I will write it so as not to forget the method. I've had a lot of trouble implementing it, so I hope it helps those people.

Introduction of refile-s3 There is a gem called'refile-s3'in refile that works with AWS S3. First, install this gem and do a bundle install. Also, since we will use environment variables later, we will also include a gem called'detenv-rails'.

Gemfile


gem "refile-s3"
gem "detenv-rails"

Terminal


$ bundle install

Creating AWS accounts and buckets In order to handle AWS S3, you have to set up AWS. [How to upload images to AWS S3 using Carrierwave and fog-aws](https://qiita.com/matsubishi5/items/c2abdd7375a4c683392a) Please refer to and perform up to "making the key into an environment variable" in the AWS settings and implementation.

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.

Create refile.rb

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.

Setting environment variables

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

Set to heroku

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.

Deploy to heroku

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.

Finally

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.

Reference article https://qiita.com/matsubishi5/items/c2abdd7375a4c683392a

Recommended Posts

upload images to refile heroku S3
Save images to Amazon S3 (Local / Heroku)
Can't upload to heroku
[Rails] How to upload images to AWS S3 using refile and refile-s3
How to post images on Heroku + CarrierWave + S3
[Note] Download from S3, upload to S3
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
Upload multiple images to Cloudinary on Active Storage and publish to Heroku
[Rails] How to upload images using Carrierwave
Upload Rails app image file to S3
[Rails] How to upload multiple images using Carrierwave
[Ruby on Rails] Upload multiple images with refile
Steps to deploy to Heroku
Heroku + ActiveStorage + Amazon S3
How to download images from AWS S3 (rails, carrierwave)
[rails] How to post images
Deploy Vapor Project to Heroku
How to handle uploaded images
How to deploy on heroku
How to minimize Java images
S3 Presigner added to aws-sdk-java-v2
[Rails] I can post S3 images in my local environment, but I can't switch to S3 on Heroku.
Save twitter icon to s3
[Java] Upload images and base64