Learn how to directly upload files from your Ruby web app to Amazon S3 storage using the Shrine gem in Part 2 of our series. Covers all the code and configuration files you need to upload efficiently as a background process.
Clever uploading to S3 from Ruby requires the use of a specific Ruby gem - Shrine. To use Amazon S3 with Shrine, in Part 1 - Setup & Configuration of our Store Your Files on S3 Using the Ruby Shrine Gem series, we installed and configured the gem in preparation for uploads. In today’s Part 2 - Direct File Uploads, we detail how to upload files directly from your web app, first to temp storage and then to a persistent location. Follow on for Part 3, Uploading Files from a Remote URL.
Direct file upload using a presigned URL
Our most desirable type of file uploading involves directly uploading files to AWS S3 from the frontend (FE) application (based on the URL provided - presigned - by the API), with the intermediate backend (BE) application taking care of finding an upload location on S3 and transferring the file from temporary to persistent storage.
A presigned URL is the location where the front-end app should upload the file; the URL is provided by the back-end app and contains the necessary authentication info so that the FE can upload the file directly to a temporary bucket without back-end being an intermediary.
The workflow for a direct file upload should look like:
1. FE application sends a request to the BE for a presign endpoint (provided by Shrine), which will be used to upload the file directly from the FE application to the S3.
2. BE application responds with JSON, which contains the data required to authenticate with S3 and upload a file.
3. FE application uploads the file by using the data obtained from the previous step.
4. FE generates JSON with the file cached on the S3 and sends it to the BE application.
5. BE application either creates or updates a record in the database and moves file between "cache" storage to "store" on S3.
Configuration for our Ruby application uploading files to Amazon S3 directly
To be able to complete this workflow, Shrine comes with the `presign_endpoint` plugin. This plugin should be enabled in your `config/initializers/shrine.rb`, by adding the following line of code:
The `presign_endpoint` plugin provides a Rack endpoint which generates presigned JSON data. Let's mount it in the routes:
The above will make a `GET` `/presign` endpoint available, which will return, in response, JSON as below:
The above data allows us to upload a file directly to S3. However, there are a few problems with this.
Configuration for sending directly to development and testing storage
So, how can we create a mechanism to upload a file to the development environment, which will be consistent with sending direct to S3 for staging and production? The solution for that is to emulate the presign data, where the URL will point to our backend server.
To do that, two things are needed:
1. Enable the `upload_endpoint` plugin
2. Create `presign` endpoint for getting the upload URL
We should enable the upload_endpoint plugin conditionally in our uploader, so that it will available only for the specific uploader, instead of globally for all of them. It should be active only in development and test. Here’s the additional code to do just that:
The upload endpoint is similar to the presign endpoint, in that it provides a Rack endpoint. It accepts file uploads and forwards them to specified storage, so specific routing need to be mounted in the routes configuration.
We also create a custom `/presign` endpoint for `development` and `test` environments using `Shrine.presign_endpoint` which simply returns the URL of the upload endpoint.
Now the `POST` `attachments` endpoint is available only in development and for test purposes.
Directly uploaded files are stored in a temporary location. When we assign the cached file to a database record, Shrine will copy it to a permanent location.
Background file processing
Shrine, by default, copies files from temporary to permanent storage in the foreground - but it comes also with the `backgrounding` plugin, which very simply moves this process to the background. To use this functionality, just enable the `backgrounding` plugin, create background workers and then show Shrine which workers to use.
Of course, a backgrounding library needs to be configured before all this. For our purposes, we use Sidekiq as an adapter for ActiveJob.
And that’s it! Now copying files between the cache and store, and deleting previously stored files will be queued and completed in the background.
Read on with Part 3, Uploading Files from a Remote URL to learn how to upload files to S3 from a remote URL, in the final installment of our Amazon S3 integration with Ruby series.
At iRonin, we strive to always configure the most efficient data flows and systems for our clients. If you would like us to take a look over your current project to see where we can make it more efficient, or have a new project you’d like developed, then make sure to get in touch. Whether it’s Ruby development using Amazon S3, web application development using Amazon S3, or something else entirely, we’d love to hear from you!