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)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/condo/strata/google_cloud_storage.rb', line 13

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



205
206
207
208
209
210
211
212
# File 'lib/condo/strata/google_cloud_storage.rb', line 205

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



38
39
40
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
# File 'lib/condo/strata/google_cloud_storage.rb', line 38

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



199
200
201
202
# File 'lib/condo/strata/google_cloud_storage.rb', line 199

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/condo/strata/google_cloud_storage.rb', line 94

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/condo/strata/google_cloud_storage.rb', line 161

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



88
89
90
# File 'lib/condo/strata/google_cloud_storage.rb', line 88

def location
    @options[:location]
end

#nameObject



83
84
85
# File 'lib/condo/strata/google_cloud_storage.rb', line 83

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



114
115
116
117
118
119
120
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
# File 'lib/condo/strata/google_cloud_storage.rb', line 114

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



191
192
193
194
195
196
# File 'lib/condo/strata/google_cloud_storage.rb', line 191

def set_part(options)
    resp = get_parts(options, true)
    resp[:type] = :resume_upload
    resp[:type] = :resume_upload
    return resp
end