Class: Gcloud::Storage::File::Signer
- Inherits:
-
Object
- Object
- Gcloud::Storage::File::Signer
- Defined in:
- lib/gcloud/storage/file.rb
Overview
Create a signed_url for a file.
Instance Method Summary collapse
- #apply_option_defaults(options) ⇒ Object
-
#ext_path ⇒ Object
The external path to the file.
-
#ext_url ⇒ Object
The external url to the file.
-
#initialize(file) ⇒ Signer
constructor
:nodoc.
- #issuer ⇒ Object
- #signature_str(options) ⇒ Object
- #signed_url(options) ⇒ Object
- #signing_key ⇒ Object
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 adjusted_expires = (Time.now.utc + ([:expires] || 300)).to_i [:expires] = adjusted_expires [:method] ||= "GET" end |
#ext_path ⇒ Object
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_url ⇒ Object
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 |
#issuer ⇒ Object
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 [[:method], [:content_md5], [:content_type], [: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 = apply_option_defaults signed_string = signing_key.sign OpenSSL::Digest::SHA256.new, signature_str() signature = Base64.encode64(signed_string).gsub("\n", "") "#{ext_url}?GoogleAccessId=#{CGI.escape issuer}" \ "&Expires=#{options[:expires]}" \ "&Signature=#{CGI.escape signature}" end |
#signing_key ⇒ Object
267 268 269 |
# File 'lib/gcloud/storage/file.rb', line 267 def signing_key @file.connection.credentials.signing_key end |