I have been working on my side project comix for a while now. I decided to move it to Edge Rails because I wanted to use ActiveStorage, a rails native library to upload files. Also, a major 5.2 release is closing in anyway, so it would be a good idea to make the application compatible with it.
DigitalOcean recently introduced Spaces. Their private object storage is one to look out for. Mainly due to pricing - 5$ a month for 250 GB storage and 1TB transfer a month!
The best thing about the service is, it’s API is fully compatible with S3. So, both can be used interchangeably. A special shout out to Chris Oliver from GoRails, from where I got this idea.
Moving the the app to Edge Rails:
We need to first move our app from Rails 5.1.4 to Edge Rails. If you don’t know what edge rails is, read more here.
So, point the rails branch to master in your Gemfile and run bundle install. This will update a few gems like Arel and add new ones to your Gemfile.lock like ActiveStorage.
In order to install ActiveStorage, we first need to grab the migration by running the install command.
From ActiveStorage documentation, here is what the two table fields do:
A key difference to how Active Storage works compared to other attachment solutions in Rails is through the use of built-in Blob and Attachment models (backed by Active Record). This means existing application models do not need to be modified with additional columns to associate with files. Active Storage uses polymorphic associations via the Attachment join model, which then connects to the actual Blob.
Blob models store attachment metadata (filename, content-type, etc.), and their identifier key in the storage service. Blob models do not store the actual binary data. They are intended to be immutable in spirit. One file, one blob. You can associate the same blob with multiple application models as well. And if you want to do transformations of a given Blob, the idea is that you’ll simply create a new one, rather than attempt to mutate the existing one (though of course you can delete the previous version later if you don’t need it).
Assuming you have a digitalocean account, first create a space.
To connect to DigitalOcean API, we need to create API key and secret for spaces.
For ActiveStorage to connect to the service, we first need to create storage.yml inside the config folder of the app. We need to setup our provider as amazon and S3 since, DigitalOcean API is fully compatible with S3.
In your environments file, config/environments/development.rb , add amazon as the service provider.
We’ll start by making our model aware that there will be just one attachment associated with it.
Next, we need to go on and whitelist our file param in the controller, so that we can accept the object from the form.
Finally, we will add the file upload field to our form.
We see the following when try to upload a file from our form.
Now, in order to display this in a page, we can access the entire object. using the following :
That’s it, now you can render your attachment, object in any page you want to. Logs show the following response from DigitalOcean.