Search code examples
ruby-on-railsrails-activestorage

Use ActiveStorage to serve videos directly uploaded to S3


I am using ActiveStorage to allow users to upload videos to Amazon S3 and for them to view them at a later date. The videos are an attachment in a model Lesson.

#app/models/lesson.rb
class Lesson
  has_one_attached :video
end

and the html to stream the video is

<video controls class='video_size'>
  <source src=<%= rails_blob_path(lesson.video) %> type='video/mp4' />
</video>

However, I have a number of videos that I have created that I want to allow users to view. These videos have already be uploaded directly to the same S3 bucket, so I know the amazon url of the videos.

Is there a way of directly creating an ActiveStorage blob using these known urls ie.

@special_blob = create_blob_from_amazon_url(amazon_url)

and then in the view

<video controls class='video_size'>
   <source src=<%= rails_blob_path(@special_blob) %> type='video/mp4' />
</video>

or do I have to upload the videos using ActiveStorage to create a blob?


Solution

  • I think what you're looking for is #build/create_after_upload! :

    https://medium.com/weareevermore/manual-uploads-using-activestorage-47808dab1b65

    https://apidock.com/rails/v6.0.0/ActiveStorage/Blob/create_after_upload%21/class