Upload Videos to Amazon S3 With Ruby on Rails

Active Storage Overview

This guide covers how to attach files to your Active Record models.

After reading this guide, yous will know:

  • How to attach one or many files to a record.
  • How to delete an attached file.
  • How to link to an fastened file.
  • How to use variants to transform images.
  • How to generate an image representation of a not-image file, such as a PDF or a video.
  • How to send file uploads direct from browsers to a storage service, bypassing your application servers.
  • How to clean up files stored during testing.
  • How to implement back up for boosted storage services.

Chapters

  1. What is Active Storage?
    • Requirements
  2. Setup
    • Disk Service
    • S3 Service (Amazon S3 and S3-compatible APIs)
    • Microsoft Azure Storage Service
    • Google Cloud Storage Service
    • Mirror Service
    • Public access
  3. Attaching Files to Records
    • has_one_attached
    • has_many_attached
    • Attaching File/IO Objects
  4. Removing Files
  5. Serving Files
    • Redirect mode
    • Proxy way
    • Authenticated Controllers
  6. Downloading Files
  7. Analyzing Files
  8. Displaying Images, Videos, and PDFs
    • Lazy vs Immediate Loading
    • Transforming Images
    • Previewing Files
  9. Directly Uploads
    • Usage
    • Cross-Origin Resource Sharing (CORS) configuration
    • Directly upload JavaScript events
    • Example
    • Integrating with Libraries or Frameworks
  10. Testing
    • Discarding files created during tests
    • Adding attachments to fixtures
  11. Implementing Back up for Other Cloud Services
  12. Purging Unattached Uploads

1 What is Active Storage?

Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects. It comes with a local deejay-based service for development and testing and supports mirroring files to subordinate services for backups and migrations.

Using Active Storage, an awarding tin transform image uploads or generate image representations of non-image uploads like PDFs and videos, and extract metadata from capricious files.

ane.1 Requirements

Various features of Active Storage depend on third-party software which Rails volition not install, and must be installed separately:

  • libvips v8.6+ or ImageMagick for image analysis and transformations
  • ffmpeg v3.4+ for video previews and ffprobe for video/audio analysis
  • poppler or muPDF for PDF previews

Image analysis and transformations also require the image_processing gem. Uncomment it in your Gemfile, or add it if necessary:

                          gem              "image_processing"              ,              ">= i.2"                      

Compared to libvips, ImageMagick is better known and more widely available. However, libvips can exist up to 10x faster and consume 1/10 the memory. For JPEG files, this can be further improved by replacing libjpeg-dev with libjpeg-turbo-dev, which is two-7x faster.

Before yous install and utilise third-party software, make sure you understand the licensing implications of doing and then. MuPDF, in detail, is licensed nether AGPL and requires a commercial license for some employ.

2 Setup

Agile Storage uses 3 tables in your application's database named active_storage_blobs, active_storage_variant_records and active_storage_attachments. After creating a new application (or upgrading your awarding to Rails 5.ii), run bin/rails active_storage:install to generate a migration that creates these tables. Apply bin/rails db:migrate to run the migration.

active_storage_attachments is a polymorphic join table that stores your model's class proper noun. If your model'south class name changes, y'all will need to run a migration on this table to update the underlying record_type to your model'southward new class name.

If you lot are using UUIDs instead of integers every bit the chief fundamental on your models you will need to change the column type of active_storage_attachments.record_id and active_storage_variant_records.id in the generated migration accordingly.

Declare Agile Storage services in config/storage.yml. For each service your application uses, provide a proper name and the requisite configuration. The example below declares three services named local, test, and amazon:

                          local              :              service              :              Disk              root              :              <%= Rails.root.join("storage") %>              test              :              service              :              Disk              root              :              <%= Rails.root.join("tmp/storage") %>              amazon              :              service              :              S3              access_key_id              :              "              "              secret_access_key              :              "              "              bucket              :              "              "              region              :              "              "              # e.g. 'us-east-1'                      

Tell Agile Storage which service to utilise by setting Rail.application.config.active_storage.service. Because each environment volition likely utilize a different service, it is recommended to do this on a per-environment ground. To employ the deejay service from the previous example in the development environment, you lot would add together the following to config/environments/evolution.rb:

                          # Store files locally.              config              .              active_storage              .              service              =              :local                      

To use the S3 service in production, you add the following to config/environments/production.rb:

                          # Store files on Amazon S3.              config              .              active_storage              .              service              =              :amazon                      

To use the test service when testing, you add together the following to config/environments/test.rb:

                          # Store uploaded files on the local file system in a temporary directory.              config              .              active_storage              .              service              =              :examination                      

Go on reading for more information on the born service adapters (e.grand. Deejay and S3) and the configuration they require.

Configuration files that are environment-specific volition take precedence: in production, for example, the config/storage/product.yml file (if existent) will accept precedence over the config/storage.yml file.

It is recommended to use Runway.env in the bucket names to farther reduce the risk of accidentally destroying production data.

                          amazon              :              service              :              S3              # ...              bucket              :              your_own_bucket-<%= Rails.env %>              google              :              service              :              GCS              # ...              bucket              :              your_own_bucket-<%= Runway.env %>              azure              :              service              :              AzureStorage              # ...              container              :              your_container_name-<%= Track.env %>                      

2.i Deejay Service

Declare a Deejay service in config/storage.yml:

                          local              :              service              :              Disk              root              :              <%= Rail.root.bring together("storage") %>                      

2.2 S3 Service (Amazon S3 and S3-compatible APIs)

To connect to Amazon S3, declare an S3 service in config/storage.yml:

                          amazon              :              service              :              S3              access_key_id              :              "              "              secret_access_key              :              "              "              region              :              "              "              saucepan              :              "              "                      

Optionally provide client and upload options:

                          amazon              :              service              :              S3              access_key_id              :              "              "              secret_access_key              :              "              "              region              :              "              "              bucket              :              "              "              http_open_timeout              :              0              http_read_timeout              :              0              retry_limit              :              0              upload              :              server_side_encryption              :              "              "              # 'aws:kms' or 'AES256'                      

Gear up sensible customer HTTP timeouts and retry limits for your application. In certain failure scenarios, the default AWS customer configuration may crusade connections to exist held for upward to several minutes and lead to asking queuing.

Add together the aws-sdk-s3 gem to your Gemfile:

                          gem              "aws-sdk-s3"              ,              crave:                            false                      

The core features of Agile Storage require the following permissions: s3:ListBucket, s3:PutObject, s3:GetObject, and s3:DeleteObject. Public admission additionally requires s3:PutObjectAcl. If yous have additional upload options configured such as setting ACLs then additional permissions may be required.

If you lot want to use environment variables, standard SDK configuration files, profiles, IAM case profiles or task roles, you lot can omit the access_key_id, secret_access_key, and region keys in the example above. The S3 Service supports all of the hallmark options described in the AWS SDK documentation.

To connect to an S3-compatible object storage API such every bit DigitalOcean Spaces, provide the endpoint:

                          digitalocean              :              service              :              S3              endpoint              :              https://nyc3.digitaloceanspaces.com              access_key_id              :              ...              secret_access_key              :              ...              # ...and other options                      

There are many other options available. You can check them in AWS S3 Client documentation.

2.3 Microsoft Azure Storage Service

Declare an Azure Storage service in config/storage.yml:

                          azure              :              service              :              AzureStorage              storage_account_name              :              "              "              storage_access_key              :              "              "              container              :              "              "                      

Add the azure-storage-blob gem to your Gemfile:

                          gem              "azure-storage-blob"              ,              crave:                            simulated                      

ii.4 Google Cloud Storage Service

Declare a Google Deject Storage service in config/storage.yml:

                          google              :              service              :              GCS              credentials              :              <%= Rails.root.join("path/to/keyfile.json") %>              project              :              "              "              bucket              :              "              "                      

Optionally provide a Hash of credentials instead of a keyfile path:

                          google              :              service              :              GCS              credentials              :              type              :              "              service_account"              project_id              :              "              "              private_key_id              :              <%= Rails.awarding.credentials.dig(:gcs, :private_key_id) %>              private_key              :              <%= Rails.application.credentials.dig(:gcs, :private_key).dump %>              client_email              :              "              "              client_id              :              "              "              auth_uri              :              "              https://accounts.google.com/o/oauth2/auth"              token_uri              :              "              https://accounts.google.com/o/oauth2/token"              auth_provider_x509_cert_url              :              "              https://www.googleapis.com/oauth2/v1/certs"              client_x509_cert_url              :              "              "              project              :              "              "              bucket              :              "              "                      

Optionally provide a Cache-Control metadata to assault uploaded assets:

                          google              :              service              :              GCS              ...              cache_control              :              "              public,                                          max-age=3600"                      

Optionally use IAM instead of the credentials when signing URLs. This is useful if you are authenticating your GKE applications with Workload Identity, see this Google Deject weblog post for more data.

                          google              :              service              :              GCS              ...              iam              :              true                      

Optionally use a specific GSA when signing URLs. When using IAM, the metadata server will be contacted to get the GSA email, but this metadata server is non ever present (due east.g. local tests) and you may wish to use a not-default GSA.

                          google              :              service              :              GCS              ...              iam              :              truthful              gsa_email              :              "              foobar@baz.iam.gserviceaccount.com"                      

Add the google-cloud-storage gem to your Gemfile:

                          gem              "google-cloud-storage"              ,              "~> 1.eleven"              ,              require:                            faux                      

2.five Mirror Service

Y'all can keep multiple services in sync by defining a mirror service. A mirror service replicates uploads and deletes across two or more subordinate services.

A mirror service is intended to be used temporarily during a migration between services in production. You can start mirroring to a new service, copy pre-existing files from the one-time service to the new, then get all-in on the new service.

Mirroring is not atomic. Information technology is possible for an upload to succeed on the main service and fail on any of the subordinate services. Earlier going all-in on a new service, verify that all files have been copied.

Ascertain each of the services you'd like to mirror as described in a higher place. Reference them by proper name when defining a mirror service:

                          s3_west_coast              :              service              :              S3              access_key_id              :              "              "              secret_access_key              :              "              "              region              :              "              "              bucket              :              "              "              s3_east_coast              :              service              :              S3              access_key_id              :              "              "              secret_access_key              :              "              "              region              :              "              "              bucket              :              "              "              production              :              service              :              Mirror              chief              :              s3_east_coast              mirrors              :              -              s3_west_coast                      

Although all secondary services receive uploads, downloads are e'er handled by the principal service.

Mirror services are uniform with direct uploads. New files are directly uploaded to the primary service. When a directly-uploaded file is attached to a tape, a background chore is enqueued to copy information technology to the secondary services.

2.half-dozen Public access

By default, Agile Storage assumes private access to services. This means generating signed, single-use URLs for blobs. If you'd rather brand blobs publicly accessible, specify public: true in your app's config/storage.yml:

                          gcs              :              &gcs              service              :              GCS              project              :              "              "              private_gcs              :              <<              :              *gcs              credentials              :              <%= Rail.root.join("path/to/private_keyfile.json") %>              bucket              :              "              "              public_gcs              :              <<              :              *gcs              credentials              :              <%= Rails.root.join("path/to/public_keyfile.json") %>              bucket              :              "              "              public              :              true                      

Brand certain your buckets are properly configured for public access. Run into docs on how to enable public read permissions for Amazon S3, Google Cloud Storage, and Microsoft Azure storage services. Amazon S3 additionally requires that you have the s3:PutObjectAcl permission.

When converting an existing awarding to use public: true, make certain to update every individual file in the saucepan to exist publicly-readable earlier switching over.

3 Attaching Files to Records

3.1 has_one_attached

The has_one_attached macro sets upwards a one-to-i mapping betwixt records and files. Each record can have one file fastened to information technology.

For example, suppose your application has a User model. If you want each user to accept an avatar, define the User model as follows:

                          class              User              <              ApplicationRecord              has_one_attached              :avatar              end                      

or if you are using Rails 6.0+, you can run a model generator command like this:

                          bin              /              rails              generate              model              User              avatar              :attachment                      

You tin can create a user with an avatar:

                          <%=              class              .              file_field              :avatar              %>                      
                          class              SignupController              <              ApplicationController              def              create              user              =              User              .              create!              (              user_params              )              session              [              :user_id              ]              =              user              .              id              redirect_to              root_path              end              individual              def              user_params              params              .              require              (              :user              ).              let              (              :email_address              ,              :password              ,              :avatar              )              end              cease                      

Call avatar.attach to attach an avatar to an existing user:

                          user              .              avatar              .              attach              (              params              [              :avatar              ])                      

Call avatar.attached? to decide whether a detail user has an avatar:

In some cases y'all might want to override a default service for a specific attachment. Y'all can configure specific services per zipper using the service pick:

                          grade              User              <              ApplicationRecord              has_one_attached              :avatar              ,              service: :s3              end                      

Yous can configure specific variants per attachment by calling the variant method on yielded attachable object:

                          class              User              <              ApplicationRecord              has_one_attached              :avatar              do              |              attachable              |              attachable              .              variant              :thumb              ,              resize_to_limit:                            [              100              ,              100              ]              end              end                      

Telephone call avatar.variant(:pollex) to go a thumb variant of an avatar:

                          <%=              image_tag              user              .              avatar              .              variant              (              :thumb              )              %>                      

3.2 has_many_attached

The has_many_attached macro sets upwardly a one-to-many relationship between records and files. Each tape can accept many files attached to it.

For example, suppose your application has a Message model. If you want each bulletin to accept many images, ascertain the Message model as follows:

                          class              Message              <              ApplicationRecord              has_many_attached              :images              finish                      

or if y'all are using Rails 6.0+, yous can run a model generator command like this:

                          bin              /              track              generate              model              Message              images              :attachments                      

You can create a message with images:

                          form              MessagesController              <              ApplicationController              def              create              message              =              Bulletin              .              create!              (              message_params              )              redirect_to              message              end              private              def              message_params              params              .              require              (              :message              ).              permit              (              :title              ,              :content              ,              images:                            [])              end              end                      

Phone call images.attach to add together new images to an existing bulletin:

                          @message              .              images              .              attach              (              params              [              :images              ])                      

Call images.attached? to determine whether a item message has any images:

                          @message              .              images              .              attached?                      

Overriding the default service is done the same way as has_one_attached, by using the service option:

                          class              Message              <              ApplicationRecord              has_many_attached              :images              ,              service: :s3              terminate                      

Configuring specific variants is washed the same way as has_one_attached, by calling the variant method on the yielded attachable object:

                          class              Bulletin              <              ApplicationRecord              has_many_attached              :images              exercise              |              attachable              |              attachable              .              variant              :thumb              ,              resize_to_limit:                            [              100              ,              100              ]              cease              end                      

iii.iii Attaching File/IO Objects

Sometimes you need to attach a file that doesn't arrive via an HTTP request. For example, you lot may want to attach a file you generated on disk or downloaded from a user-submitted URL. You may also desire to attach a fixture file in a model examination. To do that, provide a Hash containing at least an open IO object and a filename:

                          @message              .              images              .              adhere              (              io:                            File              .              open              (              '/path/to/file'              ),              filename:                            'file.pdf'              )                      

When possible, provide a content blazon as well. Agile Storage attempts to determine a file's content type from its data. It falls back to the content blazon you provide if it can't exercise that.

                          @message              .              images              .              attach              (              io:                            File              .              open              (              '/path/to/file'              ),              filename:                            'file.pdf'              ,              content_type:                            'application/pdf'              )                      

Y'all can featherbed the content type inference from the data by passing in identify: false along with the content_type.

                          @message              .              images              .              attach              (              io:                            File              .              open              (              '/path/to/file'              ),              filename:                            'file.pdf'              ,              content_type:                            'awarding/pdf'              ,              identify:                            false              )                      

If you don't provide a content blazon and Agile Storage can't determine the file's content blazon automatically, it defaults to awarding/octet-stream.

4 Removing Files

To remove an attachment from a model, telephone call purge on the attachment. If your application is ready to use Active Chore, removal can be done in the background instead past calling purge_later. Purging deletes the blob and the file from the storage service.

                          # Synchronously destroy the avatar and actual resources files.              user              .              avatar              .              purge              # Destroy the associated models and actual resource files async, via Agile Job.              user              .              avatar              .              purge_later                      

5 Serving Files

Agile Storage supports ii ways to serve files: redirecting and proxying.

All Active Storage controllers are publicly attainable past default. The generated URLs are difficult to guess, simply permanent by design. If your files require a higher level of protection consider implementing Authenticated Controllers.

5.1 Redirect fashion

To generate a permanent URL for a blob, you tin laissez passer the blob to the url_for view helper. This generates a URL with the blob'south signed_id that is routed to the blob's RedirectController

                          url_for              (              user              .              avatar              )              # => /rails/active_storage/blobs/:signed_id/my-avatar.png                      

The RedirectController redirects to the actual service endpoint. This indirection decouples the service URL from the actual ane, and allows, for example, mirroring attachments in dissimilar services for high-availability. The redirection has an HTTP expiration of 5 minutes.

To create a download link, use the rails_blob_{path|url} helper. Using this helper allows yous to gear up the disposition.

                          rails_blob_path              (              user              .              avatar              ,              disposition:                            "attachment"              )                      

To foreclose XSS attacks, Agile Storage forces the Content-Disposition header to "zipper" for some kind of files. To change this behaviour run into the available configuration options in Configuring Rails Applications.

If you need to create a link from outside of controller/view context (Groundwork jobs, Cronjobs, etc.), yous can access the rails_blob_path similar this:

                          Runway              .              awarding              .              routes              .              url_helpers              .              rails_blob_path              (              user              .              avatar              ,              only_path:                            truthful              )                      

5.2 Proxy way

Optionally, files can exist proxied instead. This means that your application servers will download file information from the storage service in response to requests. This can be useful for serving files from a CDN.

You can configure Active Storage to use proxying by default:

                          # config/initializers/active_storage.rb              Rails              .              awarding              .              config              .              active_storage              .              resolve_model_to_route              =              :rails_storage_proxy                      

Or if you desire to explicitly proxy specific attachments at that place are URL helpers you can use in the form of rails_storage_proxy_path and rails_storage_proxy_url.

                          <%=              image_tag              rails_storage_proxy_path              (              @user              .              avatar              )              %>                      
5.ii.1 Putting a CDN in front of Active Storage

Additionally, in club to use a CDN for Agile Storage attachments, you volition demand to generate URLs with proxy style then that they are served past your app and the CDN will cache the attachment without whatsoever extra configuration. This works out of the box considering the default Active Storage proxy controller sets an HTTP header indicating to the CDN to cache the response.

Y'all should also make certain that the generated URLs use the CDN host instead of your app host. There are multiple means to reach this, but in general information technology involves tweaking your config/routes.rb file and then that you can generate the proper URLs for the attachments and their variations. As an case, y'all could add this:

                          # config/routes.rb              direct              :cdn_image              do              |              model              ,              options              |              expires_in              =              options              .              delete              (              :expires_in              )              {              ActiveStorage              .              urls_expire_in              }              if              model              .              respond_to?              (              :signed_id              )              route_for              (              :rails_service_blob_proxy              ,              model              .              signed_id              (              expires_in:                            expires_in              ),              model              .              filename              ,              options              .              merge              (              host:                            ENV              [              'CDN_HOST'              ])              )              else              signed_blob_id              =              model              .              blob              .              signed_id              (              expires_in:                            expires_in              )              variation_key              =              model              .              variation              .              cardinal              filename              =              model              .              blob              .              filename              route_for              (              :rails_blob_representation_proxy              ,              signed_blob_id              ,              variation_key              ,              filename              ,              options              .              merge              (              host:                            ENV              [              'CDN_HOST'              ])              )              end              terminate                      

and then generate routes like this:

                          <%=              cdn_image_url              (              user              .              avatar              .              variant              (              resize_to_limit:                            [              128              ,              128              ]))              %>                      

v.3 Authenticated Controllers

All Agile Storage controllers are publicly accessible by default. The generated URLs employ a plain signed_id, making them hard to guess but permanent. Anyone that knows the hulk URL will be able to access it, fifty-fifty if a before_action in your ApplicationController would otherwise require a login. If your files require a higher level of protection, you can implement your own authenticated controllers, based on the ActiveStorage::Blobs::RedirectController, ActiveStorage::Blobs::ProxyController, ActiveStorage::Representations::RedirectController and ActiveStorage::Representations::ProxyController

To simply let an business relationship to access their own logo you could practice the following:

                          # config/routes.rb              resources              :business relationship              exercise              resource              :logo              end                      
                          # app/controllers/logos_controller.rb              class              LogosController              <              ApplicationController              # Through ApplicationController:              # include Authenticate, SetCurrentAccount              def              prove              redirect_to              Current              .              business relationship              .              logo              .              url              cease              end                      
                          <%=              image_tag              account_logo_path              %>                      

And and so y'all might want to disable the Active Storage default routes with:

                          config              .              active_storage              .              draw_routes              =              false                      

to prevent files beingness accessed with the publicly accessible URLs.

half dozen Downloading Files

Sometimes you demand to procedure a blob after it'southward uploaded—for case, to catechumen it to a dissimilar format. Use the zipper's download method to read a blob's binary data into memory:

                          binary              =              user              .              avatar              .              download                      

You might desire to download a blob to a file on disk so an external program (e.g. a virus scanner or media transcoder) can operate on it. Apply the attachment's open method to download a hulk to a tempfile on disk:

                          message              .              video              .              open              do              |              file              |              arrangement              '/path/to/virus/scanner'              ,              file              .              path              # ...              end                      

It's important to know that the file is non yet bachelor in the after_create callback but in the after_create_commit only.

7 Analyzing Files

Agile Storage analyzes files in one case they've been uploaded by queuing a chore in Active Task. Analyzed files will store additional data in the metadata hash, including analyzed: truthful. You can check whether a blob has been analyzed by calling analyzed? on it.

Image analysis provides width and height attributes. Video analysis provides these, as well as duration, angle, display_aspect_ratio, and video and audio booleans to indicate the presence of those channels. Audio analysis provides elapsing and bit_rate attributes.

8 Displaying Images, Videos, and PDFs

Active Storage supports representing a multifariousness of files. You can telephone call representation on an attachment to display an epitome variant, or a preview of a video or PDF. Earlier calling representation, check if the attachment can exist represented by calling representable?. Some file formats tin can't exist previewed by Active Storage out of the box (e.g. Word documents); if representable? returns faux y'all may want to link to the file instead.

                          <ul>              <%              @bulletin              .              files              .              each              exercise              |              file              |              %>              <li>              <%              if              file              .              representable?              %>              <%=              image_tag              file              .              representation              (              resize_to_limit:                            [              100              ,              100              ])              %>              <%              else              %>              <%=              link_to              rails_blob_path              (              file              ,              disposition:                            "zipper"              )              do              %>              <%=              image_tag              "placeholder.png"              ,              alt:                            "Download file"              %>              <%              end              %>              <%              end              %>              </li>              <%              end              %>              </ul>                      

Internally, representation calls variant for images, and preview for previewable files. You lot can also telephone call these methods directly.

8.1 Lazy vs Immediate Loading

By default, Agile Storage will process representations lazily. This code:

                          image_tag              file              .              representation              (              resize_to_limit:                            [              100              ,              100              ])                      

Will generate an <img> tag with the src pointing to the ActiveStorage::Representations::RedirectController. The browser volition make a request to that controller, which will return a 302 redirect to the file on the remote service (or in proxy manner, return the file contents). Loading the file lazily allows features like single use URLs to work without slowing downward your initial page loads.

This works fine for most cases.

If you want to generate URLs for images immediately, you can call .processed.url:

                          image_tag              file              .              representation              (              resize_to_limit:                            [              100              ,              100              ]).              processed              .              url                      

The Active Storage variant tracker improves functioning of this, past storing a tape in the database if the requested representation has been processed before. Thus, the above code will only make an API call to the remote service (due east.k. S3) once, and once a variant is stored, volition utilise that. The variant tracker runs automatically, but can exist disabled through config.active_storage.track_variants.

If you're rendering lots of images on a page, the above example could result in N+one queries loading all the variant records. To avoid these N+ane queries, use the named scopes on ActiveStorage::Attachment.

                          bulletin              .              images              .              with_all_variant_records              .              each              practice              |              file              |              image_tag              file              .              representation              (              resize_to_limit:                            [              100              ,              100              ]).              processed              .              url              stop                      

8.2 Transforming Images

Transforming images allows you lot to display the image at your choice of dimensions. To create a variation of an image, call variant on the attachment. You lot tin laissez passer whatsoever transformation supported by the variant processor to the method. When the browser hits the variant URL, Active Storage will lazily transform the original hulk into the specified format and redirect to its new service location.

                          <%=              image_tag              user              .              avatar              .              variant              (              resize_to_limit:                            [              100              ,              100              ])              %>                      

If a variant is requested, Active Storage will automatically apply transformations depending on the image's format:

  1. Content types that are variable (as dictated by config.active_storage.variable_content_types) and not considered web images (every bit dictated past config.active_storage.web_image_content_types), will be converted to PNG.

  2. If quality is not specified, the variant processor's default quality for the format will exist used.

Agile Storage tin utilize either Vips or MiniMagick as the variant processor. The default depends on your config.load_defaults target version, and the processor tin be changed past setting config.active_storage.variant_processor.

The 2 processors are non fully compatible, and then when migrating an existing application between MiniMagick and Vips, some changes have to be made if using options that are format specific:

                          <!-- MiniMagick -->              <%=              image_tag              user              .              avatar              .              variant              (              resize_to_limit:                            [              100              ,              100              ],              format: :jpeg              ,              sampling_factor:                            "4:two:0"              ,              strip:                            true              ,              interlace:                            "JPEG"              ,              colorspace:                            "sRGB"              ,              quality:                            80              )              %>              <!-- Vips -->              <%=              image_tag              user              .              avatar              .              variant              (              resize_to_limit:                            [              100              ,              100              ],              format: :jpeg              ,              saver:                            {              subsample_mode:                            "on"              ,              strip:                            true              ,              interlace:                            truthful              ,              quality:                            80              })              %>                      

8.three Previewing Files

Some non-epitome files tin be previewed: that is, they can be presented equally images. For example, a video file tin can be previewed past extracting its first frame. Out of the box, Active Storage supports previewing videos and PDF documents. To create a link to a lazily-generated preview, use the attachment's preview method:

                          <%=              image_tag              message              .              video              .              preview              (              resize_to_limit:                            [              100              ,              100              ])              %>                      

To add support for another format, add your own previewer. Run into the ActiveStorage::Preview documentation for more information.

9 Direct Uploads

Active Storage, with its included JavaScript library, supports uploading directly from the client to the cloud.

ix.1 Usage

  1. Include activestorage.js in your application's JavaScript bundle.

    Using the asset pipeline:

                                      //= require activestorage                              

    Using the npm parcel:

                                      import                  *                  every bit                  ActiveStorage                  from                  "                  @runway/activestorage                  "                  ActiveStorage                  .                  start                  ()                              
  2. Add direct_upload: truthful to your file field:

                                      <%=                  course                  .                  file_field                  :attachments                  ,                  multiple:                                    true                  ,                  direct_upload:                                    true                  %>                              

    Or, if you aren't using a FormBuilder, add the data aspect directly:

                                      <input                  blazon=                  file                  data-direct-upload-url=                  "                  <%=                  rails_direct_uploads_url                  %>                  "                  />                              
  3. Configure CORS on tertiary-party storage services to allow direct upload requests.

  4. That'southward it! Uploads begin upon form submission.

9.2 Cross-Origin Resource Sharing (CORS) configuration

To make direct uploads to a tertiary-party service work, you'll need to configure the service to allow cross-origin requests from your app. Consult the CORS documentation for your service:

  • S3
  • Google Cloud Storage
  • Azure Storage

Take care to allow:

  • All origins from which your app is accessed
  • The PUT request method
  • The following headers:
    • Origin
    • Content-Type
    • Content-MD5
    • Content-Disposition (except for Azure Storage)
    • ten-ms-blob-content-disposition (for Azure Storage only)
    • x-ms-blob-type (for Azure Storage only)
    • Cache-Control (for GCS, only if cache_control is set up)

No CORS configuration is required for the Disk service since information technology shares your app's origin.

9.ii.one Case: S3 CORS configuration
                          [                                          {                                          "AllowedHeaders"              :                                          [                                          "*"                                          ],                                          "AllowedMethods"              :                                          [                                          "PUT"                                          ],                                          "AllowedOrigins"              :                                          [                                          "https://world wide web.case.com"                                          ],                                          "ExposeHeaders"              :                                          [                                          "Origin"              ,                                          "Content-Type"              ,                                          "Content-MD5"              ,                                          "Content-Disposition"                                          ],                                          "MaxAgeSeconds"              :                                          3600                                          }                                          ]                                                  
nine.ii.2 Example: Google Cloud Storage CORS configuration
                          [                                          {                                          "origin"              :                                          [              "https://www.example.com"              ],                                          "method"              :                                          [              "PUT"              ],                                          "responseHeader"              :                                          [              "Origin"              ,                                          "Content-Type"              ,                                          "Content-MD5"              ,                                          "Content-Disposition"              ],                                          "maxAgeSeconds"              :                                          3600                                          }                                          ]                                                  
9.ii.3 Case: Azure Storage CORS configuration
                          <Cors>              <CorsRule>              <AllowedOrigins>https://www.instance.com</AllowedOrigins>              <AllowedMethods>PUT</AllowedMethods>              <AllowedHeaders>Origin, Content-Type, Content-MD5, x-ms-blob-content-disposition, ten-ms-blob-blazon</AllowedHeaders>              <MaxAgeInSeconds>3600</MaxAgeInSeconds>              </CorsRule>              </Cors>                      

nine.iii Direct upload JavaScript events

Result name Event target Event information (event.detail) Description
direct-uploads:start <grade> None A course containing files for straight upload fields was submitted.
direct-upload:initialize <input> {id, file} Dispatched for every file afterward course submission.
direct-upload:get-go <input> {id, file} A direct upload is starting.
direct-upload:earlier-hulk-request <input> {id, file, xhr} Before making a asking to your awarding for direct upload metadata.
directly-upload:before-storage-request <input> {id, file, xhr} Earlier making a asking to shop a file.
straight-upload:progress <input> {id, file, progress} Equally requests to store files progress.
straight-upload:mistake <input> {id, file, fault} An error occurred. An alert volition display unless this event is canceled.
straight-upload:terminate <input> {id, file} A direct upload has ended.
direct-uploads:end <form> None All direct uploads have ended.

ix.4 Instance

You lot can utilize these events to testify the progress of an upload.

direct-uploads

To testify the uploaded files in a form:

                          // direct_uploads.js              addEventListener              (              "              direct-upload:initialize              "              ,              event              =>              {              const              {              target              ,              detail              }              =              result              const              {              id              ,              file              }              =              detail              target              .              insertAdjacentHTML              (              "              beforebegin              "              ,              `     <div id="straight-upload-              ${              id              }              " class="direct-upload direct-upload--pending">       <div id="direct-upload-progress-              ${              id              }              " class="directly-upload__progress" mode="width: 0%"></div>       <span course="straight-upload__filename"></span>     </div>   `              )              target              .              previousElementSibling              .              querySelector              (              `.direct-upload__filename`              ).              textContent              =              file              .              proper noun              })              addEventListener              (              "              directly-upload:starting time              "              ,              event              =>              {              const              {              id              }              =              event              .              detail              const              element              =              document              .              getElementById              (              `directly-upload-              ${              id              }              `              )              element              .              classList              .              remove              (              "              direct-upload--awaiting              "              )              })              addEventListener              (              "              direct-upload:progress              "              ,              event              =>              {              const              {              id              ,              progress              }              =              outcome              .              particular              const              progressElement              =              document              .              getElementById              (              `direct-upload-progress-              ${              id              }              `              )              progressElement              .              way              .              width              =              `              ${              progress              }              %`              })              addEventListener              (              "              direct-upload:fault              "              ,              event              =>              {              event              .              preventDefault              ()              const              {              id              ,              mistake              }              =              upshot              .              particular              const              element              =              certificate              .              getElementById              (              `direct-upload-              ${              id              }              `              )              element              .              classList              .              add              (              "              direct-upload--mistake              "              )              element              .              setAttribute              (              "              title              "              ,              fault              )              })              addEventListener              (              "              direct-upload:end              "              ,              event              =>              {              const              {              id              }              =              event              .              detail              const              chemical element              =              document              .              getElementById              (              `directly-upload-              ${              id              }              `              )              chemical element              .              classList              .              add together              (              "              directly-upload--complete              "              )              })                      

Add together styles:

                          /* direct_uploads.css */              .direct-upload              {              display              :              inline-block              ;              position              :              relative              ;              padding              :              2px              4px              ;              margin              :              0              3px              3px              0              ;              border              :              1px              solid              rgba              (              0              ,              0              ,              0              ,              0.3              );              edge-radius              :              3px              ;              font-size              :              11px              ;              line-height              :              13px              ;              }              .direct-upload--pending              {              opacity              :              0.6              ;              }              .straight-upload__progress              {              position              :              absolute              ;              top              :              0              ;              left              :              0              ;              bottom              :              0              ;              opacity              :              0.ii              ;              background              :              #0076ff              ;              transition              :              width              120ms              ease-out              ,              opacity              60ms              60ms              ease-in              ;              transform              :              translate3d              (              0              ,              0              ,              0              );              }              .straight-upload--complete              .direct-upload__progress              {              opacity              :              0.4              ;              }              .direct-upload--error              {              border-color              :              red              ;              }              input              [              type              =              file              ][              data-direct-upload-url              ][              disabled              ]              {              display              :              none              ;              }                      

9.v Integrating with Libraries or Frameworks

If y'all want to utilize the Direct Upload characteristic from a JavaScript framework, or you desire to integrate custom drag and drop solutions, you can use the DirectUpload class for this purpose. Upon receiving a file from your library of selection, instantiate a DirectUpload and telephone call its create method. Create takes a callback to invoke when the upload completes.

                          import              {              DirectUpload              }              from              "              @rails/activestorage              "              const              input              =              certificate              .              querySelector              (              '              input[type=file]              '              )              // Bind to file drop - use the ondrop on a parent chemical element or utilize a              //  library similar Dropzone              const              onDrop              =              (              event              )              =>              {              result              .              preventDefault              ()              const              files              =              event              .              dataTransfer              .              files              ;              Array              .              from              (              files              ).              forEach              (              file              =>              uploadFile              (              file              ))              }              // Demark to normal file option              input              .              addEventListener              (              '              change              '              ,              (              consequence              )              =>              {              Array              .              from              (              input              .              files              ).              forEach              (              file              =>              uploadFile              (              file              ))              // you might clear the selected files from the input              input              .              value              =              null              })              const              uploadFile              =              (              file              )              =>              {              // your form needs the file_field direct_upload: true, which              //  provides information-directly-upload-url              const              url              =              input              .              dataset              .              directUploadUrl              const              upload              =              new              DirectUpload              (              file              ,              url              )              upload              .              create              ((              error              ,              blob              )              =>              {              if              (              error              )              {              // Handle the mistake              }              else              {              // Add an appropriately-named hidden input to the form with a              //  value of hulk.signed_id so that the blob ids will exist              //  transmitted in the normal upload catamenia              const              hiddenField              =              document              .              createElement              (              '              input              '              )              hiddenField              .              setAttribute              (              "              type              "              ,              "              hidden              "              );              hiddenField              .              setAttribute              (              "              value              "              ,              blob              .              signed_id              );              hiddenField              .              proper noun              =              input              .              proper noun              document              .              querySelector              (              '              form              '              ).              appendChild              (              hiddenField              )              }              })              }                      

If you need to rail the progress of the file upload, yous can pass a 3rd parameter to the DirectUpload constructor. During the upload, DirectUpload will telephone call the object'due south directUploadWillStoreFileWithXHR method. Y'all can then bind your own progress handler on the XHR.

                          import              {              DirectUpload              }              from              "              @rail/activestorage              "              grade              Uploader              {              constructor              (              file              ,              url              )              {              this              .              upload              =              new              DirectUpload              (              this              .              file              ,              this              .              url              ,              this              )              }              upload              (              file              )              {              this              .              upload              .              create              ((              error              ,              blob              )              =>              {              if              (              mistake              )              {              // Handle the fault              }              else              {              // Add an appropriately-named subconscious input to the course              // with a value of hulk.signed_id              }              })              }              directUploadWillStoreFileWithXHR              (              request              )              {              asking              .              upload              .              addEventListener              (              "              progress              "              ,              upshot              =>              this              .              directUploadDidProgress              (              event              ))              }              directUploadDidProgress              (              event              )              {              // Employ result.loaded and event.total to update the progress bar              }              }                      

Using Direct Uploads can sometimes result in a file that uploads, just never attaches to a tape. Consider purging unattached uploads.

x Testing

Use fixture_file_upload to test uploading a file in an integration or controller test. Track handles files similar whatever other parameter.

                          form              SignupController              <              ActionDispatch              ::              IntegrationTest              test              "tin can sign upward"              do              post              signup_path              ,              params:                            {              proper name:                            "David"              ,              avatar:                            fixture_file_upload              (              "david.png"              ,              "image/png"              )              }              user              =              User              .              order              (              :created_at              ).              last              assert              user              .              avatar              .              attached?              finish              end                      

ten.1 Discarding files created during tests

10.ane.i System tests

Arrangement tests clean up test data by rolling back a transaction. Because destroy is never chosen on an object, the attached files are never cleaned upwardly. If you desire to clear the files, you tin can do information technology in an after_teardown callback. Doing information technology here ensures that all connections created during the test are complete and you won't receive an error from Active Storage saying it can't notice a file.

                          class              ApplicationSystemTestCase              <              ActionDispatch              ::              SystemTestCase              # ...              def              after_teardown              super              FileUtils              .              rm_rf              (              ActiveStorage              ::              Blob              .              service              .              root              )              terminate              # ...              end                      

If you're using parallel tests and the DiskService, you should configure each process to apply its own folder for Agile Storage. This mode, the teardown callback will only delete files from the relevant process' tests.

                          grade              ApplicationSystemTestCase              <              ActionDispatch              ::              SystemTestCase              # ...              parallelize_setup              do              |              i              |              ActiveStorage              ::              Blob              .              service              .              root              =              "              #{              ActiveStorage              ::              Hulk              .              service              .              root              }              -              #{              i              }              "              stop              # ...              terminate                      

If your organization tests verify the deletion of a model with attachments and you're using Agile Task, set your test environment to use the inline queue adapter so the purge job is executed immediately rather at an unknown time in the future.

                          # Apply inline job processing to make things happen immediately              config              .              active_job              .              queue_adapter              =              :inline                      
10.1.2 Integration tests

Similarly to Organization Tests, files uploaded during Integration Tests will not be automatically cleaned up. If you want to clear the files, you tin do it in an teardown callback.

                          class              ActionDispatch::IntegrationTest              def              after_teardown              super              FileUtils              .              rm_rf              (              ActiveStorage              ::              Blob              .              service              .              root              )              finish              terminate                      

If you're using parallel tests and the Disk service, y'all should configure each process to utilise its own folder for Active Storage. This way, the teardown callback volition merely delete files from the relevant process' tests.

                          class              ActionDispatch::IntegrationTest              parallelize_setup              practise              |              i              |              ActiveStorage              ::              Blob              .              service              .              root              =              "              #{              ActiveStorage              ::              Blob              .              service              .              root              }              -              #{              i              }              "              end              end                      

x.2 Adding attachments to fixtures

You can add together attachments to your existing fixtures. First, you lot'll want to create a split up storage service:

                          # config/storage.yml              test_fixtures              :              service              :              Disk              root              :              <%= Rails.root.join("tmp/storage_fixtures") %>                      

This tells Active Storage where to "upload" fixture files to, and so information technology should be a temporary directory. By making information technology a different directory to your regular test service, you can carve up fixture files from files uploaded during a test.

Next, create fixture files for the Active Storage classes:

                          # active_storage/attachments.yml              david_avatar              :              name              :              avatar              record              :              david (User)              hulk              :              david_avatar_blob                      
                          # active_storage/blobs.yml              david_avatar_blob              :              <%= ActiveStorage::FixtureSet.blob filename              :              "              david.png"              ,              service_name              :              "              test_fixtures"              %              >                      

Then put a file in your fixtures directory (the default path is test/fixtures/files) with the corresponding filename. See the ActiveStorage::FixtureSet docs for more data.

One time everything is set up, you'll be able to access attachments in your tests:

                          grade              UserTest              <              ActiveSupport              ::              TestCase              def              test_avatar              avatar              =              users              (              :david              ).              avatar              assert              avatar              .              attached?              assert_not_nil              avatar              .              download              assert_equal              m              ,              avatar              .              byte_size              terminate              end                      
10.2.1 Cleaning upwardly fixtures

While files uploaded in tests are cleaned up at the finish of each test, you only need to clean up fixture files once: when all your tests consummate.

If yous're using parallel tests, call parallelize_teardown:

                          course              ActiveSupport::TestCase              # ...              parallelize_teardown              practice              |              i              |              FileUtils              .              rm_rf              (              ActiveStorage              ::              Hulk              .              services              .              fetch              (              :test_fixtures              ).              root              )              end              # ...              end                      

If you're not running parallel tests, employ Minitest.after_run or the equivalent for your test framework (e.g. after(:suite) for RSpec):

                          # test_helper.rb              Minitest              .              after_run              do              FileUtils              .              rm_rf              (              ActiveStorage              ::              Blob              .              services              .              fetch              (              :test_fixtures              ).              root              )              stop                      

xi Implementing Back up for Other Cloud Services

If you need to support a deject service other than these, you will demand to implement the Service. Each service extends ActiveStorage::Service by implementing the methods necessary to upload and download files to the deject.

12 Purging Unattached Uploads

There are cases where a file is uploaded but never fastened to a record. This can happen when using Straight Uploads. You lot tin can query for unattached records using the unattached scope. Below is an example using a custom rake job.

                          namespace              :active_storage              do              desc              "Purges unattached Agile Storage blobs. Run regularly."              chore              purge_unattached: :environs              practise              ActiveStorage              ::              Blob              .              unattached              .              where              (              "active_storage_blobs.created_at <= ?"              ,              2              .              days              .              ago              ).              find_each              (              &              :purge_later              )              end              end                      

The query generated past ActiveStorage::Blob.unattached tin be boring and potentially disruptive on applications with larger databases.

Feedback

You're encouraged to aid improve the quality of this guide.

Please contribute if y'all see whatsoever typos or factual errors. To get started, you tin read our documentation contributions section.

You may besides find incomplete content or stuff that is non up to engagement. Please exercise add any missing documentation for main. Make sure to check Edge Guides get-go to verify if the issues are already fixed or not on the chief branch. Cheque the Scarlet on Rail Guides Guidelines for style and conventions.

If for whatever reason you spot something to set only cannot patch it yourself, delight open an issue.

And terminal but not to the lowest degree, any kind of give-and-take regarding Ruby on Rails documentation is very welcome on the rubyonrails-docs mailing list.

watterscomple1992.blogspot.com

Source: https://edgeguides.rubyonrails.org/active_storage_overview.html

0 Response to "Upload Videos to Amazon S3 With Ruby on Rails"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel