Gathering detailed insights and metrics for activestorage
Gathering detailed insights and metrics for activestorage
Gathering detailed insights and metrics for activestorage
Gathering detailed insights and metrics for activestorage
@rails/activestorage
Attach cloud and local files in Rails applications
@activeledger/activestorage
This package is Activeledger's built-in data storage engine
@types/activestorage
TypeScript definitions for activestorage
@rikas/mui-activestorage-upload
React MUI package for direct upload with activestorage
npm install activestorage
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Active Storage makes it simple to upload and reference files in cloud services like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage, and attach those files to Active Records. Supports having one main service and mirrors in other services for redundancy. It also provides a disk service for testing or local deployments, but the focus is on cloud storage.
Files can be uploaded from the server to the cloud or directly from the client to the cloud.
Image files can furthermore be transformed using on-demand variants for quality, aspect ratio, size, or any other MiniMagick supported transformation.
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).
Run rails active_storage:install
to copy over active_storage migrations.
One attachment:
1class User < ApplicationRecord 2 # Associates an attachment and a blob. When the user is destroyed they are 3 # purged by default (models destroyed, and resource files deleted). 4 has_one_attached :avatar 5end 6 7# Attach an avatar to the user. 8user.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/jpg") 9 10# Does the user have an avatar? 11user.avatar.attached? # => true 12 13# Synchronously destroy the avatar and actual resource files. 14user.avatar.purge 15 16# Destroy the associated models and actual resource files async, via Active Job. 17user.avatar.purge_later 18 19# Does the user have an avatar? 20user.avatar.attached? # => false 21 22# Generate a permanent URL for the blob that points to the application. 23# Upon access, a redirect to the actual service endpoint is returned. 24# This indirection decouples the public URL from the actual one, and 25# allows for example mirroring attachments in different services for 26# high-availability. The redirection has an HTTP expiration of 5 min. 27url_for(user.avatar) 28 29class AvatarsController < ApplicationController 30 def update 31 # params[:avatar] contains a ActionDispatch::Http::UploadedFile object 32 Current.user.avatar.attach(params.require(:avatar)) 33 redirect_to Current.user 34 end 35end
Many attachments:
1class Message < ApplicationRecord 2 has_many_attached :images 3end
1<%= form_with model: @message, local: true do |form| %> 2 <%= form.text_field :title, placeholder: "Title" %><br> 3 <%= form.text_area :content %><br><br> 4 5 <%= form.file_field :images, multiple: true %><br> 6 <%= form.submit %> 7<% end %>
1class MessagesController < ApplicationController 2 def index 3 # Use the built-in with_attached_images scope to avoid N+1 4 @messages = Message.all.with_attached_images 5 end 6 7 def create 8 message = Message.create! params.require(:message).permit(:title, :content) 9 message.images.attach(params[:message][:images]) 10 redirect_to message 11 end 12 13 def show 14 @message = Message.find(params[:id]) 15 end 16end
Variation of image attachment:
1<%# Hitting the variant URL will lazy transform the original blob and then redirect to its new service location %> 2<%= image_tag user.avatar.variant(resize: "100x100") %>
Active Storage, with its included JavaScript library, supports uploading directly from the client to the cloud.
Include activestorage.js
in your application's JavaScript bundle.
Using the asset pipeline:
1//= require activestorage
Using the npm package:
1import * as ActiveStorage from "activestorage" 2ActiveStorage.start()
Annotate file inputs with the direct upload URL.
1<%= form.file_field :attachments, multiple: true, direct_upload: true %>
That's it! Uploads begin upon form submission.
Event name | Event target | Event data (event.detail ) | Description |
---|---|---|---|
direct-uploads:start | <form> | None | A form containing files for direct upload fields was submitted. |
direct-upload:initialize | <input> | {id, file} | Dispatched for every file after form submission. |
direct-upload:start | <input> | {id, file} | A direct upload is starting. |
direct-upload:before-blob-request | <input> | {id, file, xhr} | Before making a request to your application for direct upload metadata. |
direct-upload:before-storage-request | <input> | {id, file, xhr} | Before making a request to store a file. |
direct-upload:progress | <input> | {id, file, progress} | As requests to store files progress. |
direct-upload:error | <input> | {id, file, error} | An error occurred. An alert will display unless this event is canceled. |
direct-upload:end | <input> | {id, file} | A direct upload has ended. |
direct-uploads:end | <form> | None | All direct uploads have ended. |
Active Storage is released under the MIT License.
API documentation is at:
Bug reports for the Ruby on Rails project can be filed here:
Feature requests should be discussed on the rails-core mailing list here:
No vulnerabilities found.
No security vulnerabilities found.