Created
October 25, 2020 21:59
-
-
Save vojtad/9ee15e56fce3a91c1549c196d4c58209 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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