A sample Rails application using activestorage for file storage and upload.
Run the migrations, and create a .env file like this:
ACCESS_KEY_ID=youraccesskeyid
SECRET_ACCESS_KEY=yoursecretaccesskey
REGION=yourregion
BUCKET=yourbucket
Active Storage uses two database tables: one to store the filename, content type etc, and the other to join the attachment to the model that 'owns' it, for example User.
Running rails active_storage:install
when you set up a new project generates
the migrations for these tables.
Different service types are configured in config/storage.yml
. Examples of
possible service types are:
- Local Disk
- Amazon S3dw
- Microsoft Azure Storage
- Google Cloud Storage
You then set which service should be used in each environment. For example, in
development, config.active_storage.service = :local
.
Attachments are defined as model relations, see the User model in this example.
You can use has_one_attached
and has_many_attached
and they do exactly what
you might imagine.
There is support for direct uploads out of the box, just set direct_upload: true
on the form field. Uploads begin on form submission.
There are multiple javascript events you can then tap in to (start, initialize, progress, end etc) to make cool looking uploads.
You can create a download link using the view helper rails_blob_path
, setting
the disposition. The redirection has an HTTP expiration of 5 mins.