Class: Condo::Strata::GoogleCloudStorage
- Inherits:
-
Object
- Object
- Condo::Strata::GoogleCloudStorage
- Defined in:
- lib/condo/strata/google_cloud_storage.rb
Instance Method Summary collapse
- #destroy(upload) ⇒ Object
-
#enable_cors(bucket, origin = '*') ⇒ Object
Enable CORS on a bucket for a domain.
- #fog_connection ⇒ Object
-
#get_object(options) ⇒ Object
Create a signed URL for accessing a private file.
-
#get_parts(options, setting_parts = false) ⇒ Object
Creates a request for the byte we were up to.
-
#initialize(options) ⇒ GoogleCloudStorage
constructor
A new instance of GoogleCloudStorage.
- #location ⇒ Object
- #name ⇒ Object
-
#new_upload(options) ⇒ Object
Creates a new upload request (either single shot or multi-part) => Passed: bucket_name, object_key, object_options, file_size.
-
#set_part(options) ⇒ Object
Returns the requests for uploading parts and completing a resumable upload.
Constructor Details
#initialize(options) ⇒ GoogleCloudStorage
Returns a new instance of GoogleCloudStorage.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 14 def initialize() @options = { :name => :GoogleCloudStorage, :location => :na, # US or Europe, set at bucket creation time :fog => { :provider => 'Google', :google_storage_access_key_id => [:fog_access_id] || [:access_id], :google_storage_secret_access_key => [:fog_secret_key] || [:secret_key] }, :api => 1 }.merge!() raise ArgumentError, 'Google Access ID missing' if @options[:access_id].nil? raise ArgumentError, 'Google Secret Key missing' if @options[:secret_key].nil? if @options[:api] == 2 @options[:secret_key] = OpenSSL::PKey::RSA.new(@options[:secret_key]) end @options[:location] = @options[:location].to_sym end |
Instance Method Details
#destroy(upload) ⇒ Object
222 223 224 225 226 227 228 229 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 222 def destroy(upload) connection = fog_connection directory = connection.directories.get(upload.bucket_name) # it is assumed this exists - if not then the upload wouldn't have taken place file = directory.files.get(upload.object_key) # NOTE:: I only assume this works with resumables... should look into it return true if file.nil? return file.destroy end |
#enable_cors(bucket, origin = '*') ⇒ Object
Enable CORS on a bucket for a domain
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 41 def enable_cors(bucket, origin = '*') data = <<-DATA <?xml version="1.0" encoding="UTF-8"?> <CorsConfig> <Cors> <Origins> <Origin>#{origin}</Origin> </Origins> <Methods> <Method>GET</Method> <Method>HEAD</Method> <Method>POST</Method> <Method>PUT</Method> </Methods> <ResponseHeaders> <ResponseHeader>origin</ResponseHeader> <ResponseHeader>content-md5</ResponseHeader> <ResponseHeader>authorization</ResponseHeader> <ResponseHeader>x-goog-date</ResponseHeader> <ResponseHeader>x-goog-acl</ResponseHeader> <ResponseHeader>content-type</ResponseHeader> <ResponseHeader>accept</ResponseHeader> <ResponseHeader>x-goog-api-version</ResponseHeader> <ResponseHeader>x-goog-resumable</ResponseHeader> <ResponseHeader>content-range</ResponseHeader> <ResponseHeader>x-requested-with</ResponseHeader> </ResponseHeaders> <MaxAgeSec>1800</MaxAgeSec> </Cors> </CorsConfig> DATA fog_connection.condo_request( :expects => 200, :body => data, :method => 'PUT', :headers => {}, :host => "#{bucket}.storage.googleapis.com", :idempotent => true, :path => '?cors' # There is an issue with Fog where this isn't included as a canonical_resource ) end |
#fog_connection ⇒ Object
216 217 218 219 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 216 def fog_connection @fog = @fog || Fog::Storage.new(@options[:fog]) return @fog end |
#get_object(options) ⇒ Object
Create a signed URL for accessing a private file
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 99 def get_object() = {}.merge!() # Need to deep copy here [:object_options] = { :expires => 5.minutes.from_now, :verb => :get, :headers => {}, :parameters => {}, :protocol => :https }.merge!([:object_options] || {}) .merge!(@options) # # provide the signed request # sign_request()[:url] end |
#get_parts(options, setting_parts = false) ⇒ Object
Creates a request for the byte we were up to
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 172 def get_parts(, setting_parts = false) [:object_options] = { :expires => 5.minutes.from_now, :verb => :put, # put for direct uploads :headers => {}, :parameters => {}, :protocol => :https }.merge!([:object_options] || {}) .merge!(@options) # # Set the upload and request the range of bytes we are after # if setting_parts [:object_options][:headers]['Content-Md5'] = [:file_id] if [:file_id].present? && [:object_options][:headers]['Content-Md5'].nil? [:object_options][:headers]['Content-Range'] = "bytes #{[:part]}-#{[:file_size] - 1}/#{[:file_size]}" else [:object_options][:headers]['Content-Range'] = "bytes */#{[:file_size]}" end [:object_options][:headers]['x-goog-api-version'] = @options[:api] [:object_options][:parameters]['upload_id'] = [:resumable_id] # # provide the signed request # { :expected => 308, :type => :status, :signature => sign_request() } end |
#location ⇒ Object
91 92 93 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 91 def location @options[:location] end |
#name ⇒ Object
86 87 88 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 86 def name @options[:name] end |
#new_upload(options) ⇒ Object
Creates a new upload request (either single shot or multi-part)
> Passed: bucket_name, object_key, object_options, file_size
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 121 def new_upload() = {}.merge!() # Need to deep copy here [:object_options] = { :permissions => :private, :expires => 5.minutes.from_now, :verb => :put, # put for direct uploads :headers => {}, :parameters => {}, :protocol => :https }.merge!([:object_options] || {}) .merge!(@options) [:object_options][:headers]['x-goog-api-version'] = @options[:api] if [:object_options][:headers]['x-goog-acl'].nil? [:object_options][:headers]['x-goog-acl'] = case [:object_options][:permissions] when :public :'public-read' else :private end end [:object_options][:headers]['Content-Type'] = 'binary/octet-stream' if [:object_options][:headers]['Content-Type'].nil? # # Decide what type of request is being sent # if [:file_size] > 1.megabytes # Resumables may not support the md5 header at this time - have to compare ETag and fail on the client side [:object_options][:verb] = :post [:object_options][:headers]['x-goog-resumable'] = 'start' return { :signature => sign_request(), :type => :chunked_upload # triggers resumable } else [:object_options][:headers]['Content-Md5'] = [:file_id] if [:file_id].present? && [:object_options][:headers]['Content-Md5'].nil? return { :signature => sign_request(), :type => :direct_upload } end end |
#set_part(options) ⇒ Object
Returns the requests for uploading parts and completing a resumable upload
208 209 210 211 212 213 |
# File 'lib/condo/strata/google_cloud_storage.rb', line 208 def set_part() resp = get_parts(, true) resp[:type] = :resume_upload resp[:type] = :resume_upload return resp end |