How to Stream Video Using Amazon S3 and CloudFront with Ruby on Rails

How to Stream Video Using Amazon S3 and CloudFront with Ruby on Rails
In the realm of data-driven applications, streaming video content is a common requirement. Thankfully, Amazon Web Services (AWS) provides robust solutions for this: Amazon S3 for storing the videos and Amazon CloudFront for fast, secure delivery. In this post, we’ll explore how to utilize these services for video streaming in a Ruby on Rails application.
What are Amazon S3 and CloudFront?
Amazon S3 (Simple Storage Service) is an object storage service that offers scalability, data availability, security, and performance. It’s used for backup, archiving, content distribution, and much more.
Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency and high transfer speeds within an AWS environment.
Prerequisites
To follow along, you need:
- Basic knowledge of Ruby on Rails
- AWS Account
- AWS SDK for Ruby
aws-sdk-s3
gem for interacting with S3
Step 1: Setting up Amazon S3 Bucket
After logging into your AWS account, navigate to S3 and create a new bucket. Be sure to enable public read access for this bucket.
s3 = Aws::S3::Resource.new(region: 'us-west-2')
bucket = s3.bucket('my-bucket')
bucket.acl.put({acl: 'public-read'})
Step 2: Uploading Videos to S3
We’ll create an upload form in our Rails app using form_with
helper. The videos will be uploaded directly to S3 using aws-sdk-s3
gem.
<%= form_with(url: videos_path, local: true) do |form| %>
<%= form.label :file, 'Select a video' %>
<%= form.file_field :file %>
<%= form.submit 'Upload' %>
<% end %>
In the controller, we handle the file upload to S3.
def create
s3 = Aws::S3::Resource.new(region: 'us-west-2')
obj = s3.bucket('my-bucket').object(params[:file].original_filename)
obj.upload_file(params[:file].tempfile, acl:'public-read')
end
Step 3: Setting up CloudFront
Next, we’ll setup a CloudFront distribution. In the AWS Console, go to the CloudFront service and create a new web distribution. Select the S3 bucket you created earlier as the origin.
distribution = Aws::CloudFront::Distribution.new(id: 'my-distribution')
distribution.create({
distribution_config: {
origins: [{ domain_name: 'my-bucket.s3.amazonaws.com', id: 'my-origin' }],
default_cache_behavior: { target_origin_id: 'my-origin', viewer_protocol_policy: 'redirect-to-https' },
enabled: true
}
})
Step 4: Streaming Videos
We’ll use the HTML5 video
tag for video playback. The src
attribute will point to the CloudFront distribution URL.
<%= video_tag "#{ENV['CLOUDFRONT_URL']}/#{video.filename}", controls: true %>
Conclusion
In this tutorial, we’ve seen how to stream videos using Amazon S3 and CloudFront in a Ruby on Rails application. By leveraging AWS infrastructure, we can deliver high-quality, scalable video content to our users with ease.
Remember, while we enabled public read access for simplicity here, you should always secure your S3 buckets and CloudFront distributions. AWS provides numerous options like signed URLs or OAI (Origin Access Identity) to securely deliver content.
Keep exploring and happy coding!
Keywords
- Amazon S3
- Amazon CloudFront
- Ruby on Rails
- Video Streaming
- AWS SDK for Ruby
- Content Delivery Network
- AWS
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.