Class: Aws::CloudFront::UrlSigner

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-sdk-core/cloudfront/url_signer.rb

Overview

Allows you to create signed URLs for Amazon CloudFront resources

signer = Aws::CloudFront::UrlSigner.new(
  key_pair_id: "cf-keypair-id",
  private_key_path: "./cf_private_key.pem"
)
url = signer.signed_url(url,
  policy: policy.to_json
)

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UrlSigner

Returns a new instance of UrlSigner.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :key_pair_id (String)
  • :private_key (String)
  • :private_key_path (String)


25
26
27
28
# File 'lib/aws-sdk-core/cloudfront/url_signer.rb', line 25

def initialize(options = {})
  @key_pair_id = key_pair_id(options)
  @private_key = private_key(options)
end

Instance Method Details

#signed_url(url, params = {}) ⇒ Object

create a signed Amazon CloudFront URL

Parameters:

  • url (String)
  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :expires (Time, DateTime, Date, String, Integer<timestamp>)
  • :policy (String<JSON>)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/aws-sdk-core/cloudfront/url_signer.rb', line 34

def signed_url(url, params = {})
  url_sections = url.split('://')
  if url_sections.length < 2
    raise ArgumentError, "Invaild URL:#{url}"
  end
  # removing wildcard character to get real scheme
  scheme = url_sections[0].gsub('*', '')
  uri = "#{scheme}://#{url_sections[1]}"
  signed_content = signature(
    :resource => resource(scheme, uri),
    :expires => time(params[:expires]),
    :policy => params[:policy]
  )

  start_flag = URI.parse(uri).query ? '&' : '?'
  uri = "#{uri}#{start_flag}#{signed_content}"

  if scheme == 'rtmp'
    rtmp_url(URI(uri))
  else
    uri
  end
end