[Rails] How to create a signed URL for CloudFront

Preparation

Use Gem's "AWS --SDK".

gem "aws-sdk"

Implementation

KEY_PAIR_ID: ID of the key pair created by CloudFront PRIVATE_KEY: Contents of the private key of the key pair created by CloudFront

class ApplicationUploader < CarrierWave::Uploader::Base
  def signed_url
    if path.blank?
      url
    else
      signer = Aws::CloudFront::UrlSigner.new(key_pair_id: KEY_PAIR_ID, private_key: PRIVATE_KEY)
      signer.signed_url(url, expires: Time.zone.now + 5.minutes)
    end
  end
end

Relation

[AWS] How to set a signed URL in CloudFront (delivery of private content)

Recommended Posts

[Rails] How to create a signed URL for CloudFront
How to create a Maven repository for 2020
[rails] How to create a partial template
How to create a database for H2 Database anywhere
[Rails] How to create a graph using lazy_high_charts
How to create pagination for a "kaminari" array
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
How to create a method
How to create a JDBC URL (Oracle Database, Thin)
[Spring Boot] How to create a project (for beginners)
Preparing to create a Rails application
[Java] How to create a folder
Tutorial to create a blog with Rails for beginners Part 1
How to create a lightweight container image for Java apps
Tutorial to create a blog with Rails for beginners Part 2
How to create and launch a Dockerfile for Payara Micro
Tutorial to create a blog with Rails for beginners Part 0
[Rails 6] How to create a dynamic form input screen using cocoon
How to insert a video in Rails
[Rails] How to set a password for posts, url direct hit measures have been taken
[Swift5] How to create a splash screen
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Rails] How to create a table, add a column, and change the column type
Rails: How to write a rake task nicely
Convert to a tag to URL string in Rails
[Rails] How to write when making a subquery
[Rails] rails new to create a database with PostgreSQL
How to prevent direct URL typing in Rails
[Rails] How to implement unit tests for models
How to create a class that inherits class information
How to create a theme in Liferay 7 / DXP
How to make a lightweight JRE for distribution
[1st] How to create a Spring-MVC framework project
How to implement a like feature in Rails
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to use Gem'rails-i18n' for Japanese support
How to make a follow function in Rails
How to write Rails
How to uninstall Rails
How to create docker-compose
How to create a route directly from the URL you want to specify + α
[Introduction] Try to create a Ruby on Rails application
How to create a Java environment in just 3 seconds
How to implement login request processing (Rails / for beginners)
How to implement a like feature in Ajax in Rails
How to use an array for a TreeMap key
How to write a unit test for Spring Boot 2
Rails / Ruby: How to get HTML text for Mail
How to delete a new_record object built with Rails
How to create a Spring Boot project in IntelliJ
How to manually generate a JWT with Rails Knock
How to create a data URI (base64) in Java
[How to insert a video in haml with Rails]
[Rails] How to deal with URL changes after render
How to write a date comparison search in Rails
[For those who create portfolios] How to use font-awesome-rails
[Rails 6] How to set a background image in Rails [CSS]
How to make a mod for Slay the Spire