Obromios
Obromios

Reputation: 16393

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?

Upvotes: 3

Views: 1046

Answers (1)

Related Questions