Rails Active Storage
This guide will show you how to connect ActiveStorage in Rails 7 to Storj (an AWS S3 compatible object storage). You can also look at the source code of this example.
We have a webinar walking you through it, but you can also refer to the steps below:
Prerequisites:
Installed ruby 3.1.2 or later
Installed rails 7.0.4.2 or later
Storj S3 compatiable access and secret key (see Getting started)
A bucket created on Storj (see Create buckets)
Configure ActiveStorage to Storj
Create a new rails app
Install ActiveStorage
Edit Gemfile
, add the aws-sdk-s3
gem and run bundle install
Edit the rails credentials
Add to the credentials your access key and secret key (see Getting started) under storj:
Edit config/storage.yml
, enter the following to configure ActiveStorage to use Storj.
Be sure to replace the bucket with the one created earlier (see Create buckets)
Edit config/environments/development.rb
, change the ActiveStorage service to :storj
Use active storage images hosted by Storj
First we'll need a basic app to interact with generated with Rails scaffolding
Edit config/routes.rb
set root
to "names#index
Edit app/models/name.rb
to have has_one_attached
Edit app/views/names/_form.html.erb
to include a field_field
to allow the user to choose a file to upload
Navigate to http://localhost:3000/names/new and you should see the following
Click "Create Name"
Edit app/views/names/show.html.erb
to include <%= image_tag @name.main_image %>
to display the image
Edit app/assets/stylesheets/application.css
to style the image to fix the size of the screen
Navigate to http://localhost:3000/names/1 to see your image
Enable direct to Storj upload
Edit config/importmap.rb
to include the @rails/activestorage
package
Edit app/javascript/controllers/application.js
to initialize the ActiveStorage
Modify app/views/names/_form.html.erb
form.file_field
to include direct_upload: true