class DirectUpload::Gcs

Constants

GCS_API_ENDPOINT
VERSION

Attributes

bucket[R]
content_type[R]
expires_in[R]
file_name[R]
key_file[R]
method[R]

Public Class Methods

new(method, file_name, expires_in, content_type = '', bucket = ENV.fetch('GOOGLE_CLOUD_STORAGE_BUCKET'), key_file = ENV.fetch('GOOGLE_CLOUD_KEYFILE_JSON')) click to toggle source
# File lib/direct_upload/gcs.rb, line 11
def initialize(method, file_name, expires_in, content_type = '', bucket = ENV.fetch('GOOGLE_CLOUD_STORAGE_BUCKET'), key_file = ENV.fetch('GOOGLE_CLOUD_KEYFILE_JSON'))
  @method = method
  @file_name = file_name
  @expires_in = expires_in
  @content_type = content_type
  @bucket = bucket
  @key_file = key_file
end

Public Instance Methods

signed_url() click to toggle source
# File lib/direct_upload/gcs.rb, line 20
def signed_url
  signature = [method.to_s.upcase, '', content_type, expires_in.to_i, full_path].join("\n")
  digest = OpenSSL::Digest::SHA256.new
  signer = OpenSSL::PKey::RSA.new(storage_configuration['private_key'])
  signature = Base64.strict_encode64(signer.sign(digest, signature))
  signature = CGI.escape(signature)
  "#{GCS_API_ENDPOINT}#{full_path}#{query_params(signature)}"
end

Private Instance Methods

full_path() click to toggle source
# File lib/direct_upload/gcs.rb, line 31
def full_path
  @_full_path = "/#{bucket}/#{file_name}"
end
query_params(signature) click to toggle source
# File lib/direct_upload/gcs.rb, line 39
def query_params(signature)
  "?GoogleAccessId=#{storage_configuration['client_email']}&Expires=#{expires_in.to_i}&Signature=#{signature}"
end
storage_configuration() click to toggle source
# File lib/direct_upload/gcs.rb, line 35
def storage_configuration
  @_storage_configuration ||= JSON.parse(File.read(key_file))
end