Rails Active Storage with Backblaze B2
I wanted to use Blackblaze's B2 service to host uploads for this site using Active Storage. I wrote this up when I couldn't find a guide to configure the two to work together. B2 advertises itself as the most affordable cloud bucket storage. 🤞 here's to hoping it remains competitively priced.
Rails 5.2 introduced Active Storage, a simple way to add attachments in your Rails applications. Rails 6 introduce Action Text with simple built-in rich text editing experience.
Backblaze has a storage service B2 with an S3 compatible API.
I took some time to document the step to configure active storage to use B2.
The Rails guide has simple instructions for setting up Active Storage with S3.
After creating a storage bucket in B2, configure a new section in `config/storage.yml` to include your settings:
Rails 5.2 introduced Active Storage, a simple way to add attachments in your Rails applications. Rails 6 introduce Action Text with simple built-in rich text editing experience.
Backblaze has a storage service B2 with an S3 compatible API.
I took some time to document the step to configure active storage to use B2.
The Rails guide has simple instructions for setting up Active Storage with S3.
After creating a storage bucket in B2, configure a new section in `config/storage.yml` to include your settings:
b2: service: S3 endpoint: https://s3.us-west-004.backblazeb2.com access_key_id: <%= Rails.application.credentials.dig(:b2, :access_key_id) %> secret_access_key: <%= Rails.application.credentials.dig(:b2, :secret_access_key) %> region: us-west-004 bucket: YOUR_BUCKET_NAME
I used `Rails.application.credentials` to protect private keys for accessing the bucket, but any other environment variable extraction technique would work equally well here. Note that the region name is repeat in both the endpoint and the region fields. You'll likely have a different region name.
Each rails environment has an active storage service explicitly defined. To assign the production enviornment to Blackblaze, open `config/environments/production.rb` and search for: `config.active_storage.service` and replace it with this:
config.active_storage.service = :b2
Before the bucket can be used with Active Storage, it will need to be configured with CORS rules. This required downloading Backblaze's command line tool (b2) sending the following command:
# download b2-darwin and rename to b2 b2 update-bucket --corsRules '[ { "corsRuleName": "allowBrowserPut", "allowedOrigins": ["http://localhost:3000"], "allowedHeaders": ["*"], "allowedOperations": ["s3_put"], "maxAgeSeconds": 3600 }, { "corsRuleName": "allowBrowserGet", "allowedOrigins": ["http://localhost:3000"], "allowedHeaders": ["Origin", "Content-Type", "Content-MD5", "Content-Disposition"], "allowedOperations": ["s3_get"], "maxAgeSeconds": 3600 } ]' YOUR_BUCKET_NAME allPublic
I began by testing this locally so I used the host "http://localhost:3000" in the example above. To configure for production, change this to your production host name.
Finally, to test all of this in a Rails app, here are a few commands to generate a model that uses Action Text with Active Storage:
bin/rails active_storage:install bin/rails action_text:install bin/rails g scaffold Article body:rich_text bin/rails db:migrate open http://localhost:3000/articles/new
I hope this is helpful for anyone looking to tryout Active Storage and B2 together.