Class: Gcloud::Storage::File::Signer

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/storage/file.rb

Overview

Create a signed_url for a file.

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Signer

:nodoc



238
239
240
# File 'lib/gcloud/storage/file.rb', line 238

def initialize file
  @file = file
end

Instance Method Details

#apply_option_defaults(options) ⇒ Object



254
255
256
257
258
259
# File 'lib/gcloud/storage/file.rb', line 254

def apply_option_defaults options
  adjusted_expires = (Time.now.utc + (options[:expires] || 300)).to_i
  options[:expires] = adjusted_expires
  options[:method]  ||= "GET"
  options
end

#ext_pathObject

The external path to the file.



244
245
246
# File 'lib/gcloud/storage/file.rb', line 244

def ext_path
  "/#{@file.bucket}/#{@file.name}"
end

#ext_urlObject

The external url to the file.



250
251
252
# File 'lib/gcloud/storage/file.rb', line 250

def ext_url
  "https://storage.googleapis.com#{ext_path}"
end

#issuerObject



271
272
273
# File 'lib/gcloud/storage/file.rb', line 271

def issuer
  @file.connection.credentials.issuer
end

#signature_str(options) ⇒ Object



261
262
263
264
265
# File 'lib/gcloud/storage/file.rb', line 261

def signature_str options
  [options[:method], options[:content_md5],
   options[:content_type], options[:expires],
   ext_path].join "\n"
end

#signed_url(options) ⇒ Object



275
276
277
278
279
280
281
282
283
# File 'lib/gcloud/storage/file.rb', line 275

def signed_url options
  options = apply_option_defaults options
  signed_string = signing_key.sign OpenSSL::Digest::SHA256.new,
                                   signature_str(options)
  signature = Base64.encode64(signed_string).gsub("\n", "")
  "#{ext_url}?GoogleAccessId=#{CGI.escape issuer}" \
            "&Expires=#{options[:expires]}" \
            "&Signature=#{CGI.escape signature}"
end

#signing_keyObject



267
268
269
# File 'lib/gcloud/storage/file.rb', line 267

def signing_key
  @file.connection.credentials.signing_key
end