LoginSignup
1
2

More than 1 year has passed since last update.

【Rails】CloudFrontの署名付きURLの作成方法

Last updated at Posted at 2020-12-14

準備

Gemの「AWS - SDK」を使用する。
※ CarrierWaveを使用している前提

gem "aws-sdk"

実装

KEY_PAIR_ID:CloudFrontで作成したキーペアのID
PRIVATE_KEY:CloudFrontで作成したキーペアのプライベートキーの内容
※ private_key_pathオプションを使用すれば、プライベートキーのファイルパスを指定すれば読み込んでくれる。
公式リファレンス

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

関連

【AWS】CloudFrontで署名付きURLの設定方法(プライベートコンテンツの配信)

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2