Skip to content

Instantly share code, notes, and snippets.

@vojtad
Created October 25, 2020 21:59
Show Gist options
  • Save vojtad/9ee15e56fce3a91c1549c196d4c58209 to your computer and use it in GitHub Desktop.
Save vojtad/9ee15e56fce3a91c1549c196d4c58209 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class CreateSignedCloudfrontUrl < Service
attr_reader :url, :expires_at
def initialize(url:, expires_in:)
@url = url
@expires_at = expires_in.from_now.to_i
end
def call
Aws::CloudFront::UrlSigner
.new(
key_pair_id: ENV['AWS_PRIVATE_KEY_PAIR_ID'],
private_key: ENV['AWS_PRIVATE_KEY']
)
.signed_url(url, policy: cloudfront_policy.to_json)
end
private
def cloudfront_policy
{
"Statement": [
{
"Resource": url,
"Condition": {
"DateLessThan": {
"AWS:EpochTime": expires_at
}
}
}
]
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment