Class: Condo::Strata::GoogleCloudStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/condo/strata/google_cloud_storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GoogleCloudStorage

Returns a new instance of GoogleCloudStorage.

Raises:

  • (ArgumentError)


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)
	@options = {
		:name => :GoogleCloudStorage,
		:location => :na,				# US or Europe, set at bucket creation time
		:fog => {
			:provider => 'Google',
			:google_storage_access_key_id => options[:fog_access_id] || options[:access_id],
			:google_storage_secret_access_key => options[:fog_secret_key] || options[:secret_key]
		},
		:api => 1
	}.merge!(options)
	
	
	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_connectionObject



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(options)
	options = {}.merge!(options)	# Need to deep copy here
	options[:object_options] = {
		:expires => 5.minutes.from_now,
		:verb => :get,
		:headers => {},
		:parameters => {},
		:protocol => :https
	}.merge!(options[:object_options] || {})
	options.merge!(@options)
	
	#
	# provide the signed request
	#
	sign_request(options)[: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(options, setting_parts = false)
	options[:object_options] = {
		:expires => 5.minutes.from_now,
		:verb => :put,				# put for direct uploads
		:headers => {},
		:parameters => {},
		:protocol => :https
	}.merge!(options[:object_options] || {})
	options.merge!(@options)
	
	#
	# Set the upload and request the range of bytes we are after
	#
	if setting_parts
		options[:object_options][:headers]['Content-Md5'] = options[:file_id] if options[:file_id].present? && options[:object_options][:headers]['Content-Md5'].nil?
		options[:object_options][:headers]['Content-Range'] = "bytes #{options[:part]}-#{options[:file_size] - 1}/#{options[:file_size]}"
	else
		options[:object_options][:headers]['Content-Range'] = "bytes */#{options[:file_size]}"
	end
	options[:object_options][:headers]['x-goog-api-version'] = @options[:api]
	options[:object_options][:parameters]['upload_id'] = options[:resumable_id]
	
	#
	# provide the signed request
	#
	{
		:expected => 308,
		:type => :status,
		:signature => sign_request(options)
	}
end

#locationObject



91
92
93
# File 'lib/condo/strata/google_cloud_storage.rb', line 91

def location
	@options[:location]
end

#nameObject



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(options)
	options = {}.merge!(options)	# Need to deep copy here
	options[:object_options] = {
		:permissions => :private,
		:expires => 5.minutes.from_now,
		:verb => :put,				# put for direct uploads
		:headers => {},
		:parameters => {},
		:protocol => :https
	}.merge!(options[:object_options] || {})
	options.merge!(@options)


	options[:object_options][:headers]['x-goog-api-version'] = @options[:api]
	
	if options[:object_options][:headers]['x-goog-acl'].nil?
		options[:object_options][:headers]['x-goog-acl'] = case options[:object_options][:permissions]
		when :public
			:'public-read'
		else
			:private
		end
	end
	
	options[:object_options][:headers]['Content-Type'] = 'binary/octet-stream' if options[:object_options][:headers]['Content-Type'].nil?

	
	#
	# Decide what type of request is being sent
	#
	if options[:file_size] > 1.megabytes
		# Resumables may not support the md5 header at this time - have to compare ETag and fail on the client side
		options[:object_options][:verb] = :post
		options[:object_options][:headers]['x-goog-resumable'] = 'start'
		return {
			:signature => sign_request(options),
			:type => :chunked_upload				# triggers resumable
		}
	else
		options[:object_options][:headers]['Content-Md5'] = options[:file_id] if options[:file_id].present? && options[:object_options][:headers]['Content-Md5'].nil?
		return {
			:signature => sign_request(options),
			: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(options)
	resp = get_parts(options, true)
	resp[:type] = :resume_upload
	resp[:type] = :resume_upload
	return resp
end