google-cloud-storage 1.18.1 → 1.44.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +17 -30
  3. data/CHANGELOG.md +312 -0
  4. data/CONTRIBUTING.md +4 -5
  5. data/LOGGING.md +1 -1
  6. data/OVERVIEW.md +37 -5
  7. data/TROUBLESHOOTING.md +2 -8
  8. data/lib/google/cloud/storage/bucket/acl.rb +40 -40
  9. data/lib/google/cloud/storage/bucket/cors.rb +4 -1
  10. data/lib/google/cloud/storage/bucket/lifecycle.rb +259 -44
  11. data/lib/google/cloud/storage/bucket/list.rb +3 -3
  12. data/lib/google/cloud/storage/bucket.rb +1096 -172
  13. data/lib/google/cloud/storage/convert.rb +4 -3
  14. data/lib/google/cloud/storage/credentials.rb +16 -14
  15. data/lib/google/cloud/storage/errors.rb +7 -2
  16. data/lib/google/cloud/storage/file/acl.rb +181 -20
  17. data/lib/google/cloud/storage/file/list.rb +10 -8
  18. data/lib/google/cloud/storage/file/signer_v2.rb +36 -18
  19. data/lib/google/cloud/storage/file/signer_v4.rb +249 -61
  20. data/lib/google/cloud/storage/file/verifier.rb +2 -2
  21. data/lib/google/cloud/storage/file.rb +450 -84
  22. data/lib/google/cloud/storage/hmac_key/list.rb +182 -0
  23. data/lib/google/cloud/storage/hmac_key.rb +316 -0
  24. data/lib/google/cloud/storage/policy/binding.rb +246 -0
  25. data/lib/google/cloud/storage/policy/bindings.rb +196 -0
  26. data/lib/google/cloud/storage/policy/condition.rb +138 -0
  27. data/lib/google/cloud/storage/policy.rb +277 -24
  28. data/lib/google/cloud/storage/post_object.rb +20 -2
  29. data/lib/google/cloud/storage/project.rb +249 -50
  30. data/lib/google/cloud/storage/service.rb +479 -288
  31. data/lib/google/cloud/storage/version.rb +1 -1
  32. data/lib/google/cloud/storage.rb +86 -16
  33. data/lib/google-cloud-storage.rb +54 -7
  34. metadata +74 -27
@@ -0,0 +1,182 @@
1
+ # Copyright 2019 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "delegate"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Storage
21
+ class HmacKey
22
+ ##
23
+ # HmacKey::List is a special case Array with additional values.
24
+ class List < DelegateClass(::Array)
25
+ ##
26
+ # If not empty, indicates that there are more HMAC keys
27
+ # that match the request and this value should be passed to
28
+ # the next {Google::Cloud::Storage::Project#hmac_keys} to continue.
29
+ attr_accessor :token
30
+
31
+ ##
32
+ # @private Create a new HmacKey::List with an array of values.
33
+ def initialize arr = []
34
+ super arr
35
+ end
36
+
37
+ ##
38
+ # Whether there is a next page of HMAC keys.
39
+ #
40
+ # @return [Boolean]
41
+ #
42
+ # @example
43
+ # require "google/cloud/storage"
44
+ #
45
+ # storage = Google::Cloud::Storage.new
46
+ #
47
+ # hmac_keys = storage.hmac_keys
48
+ # if hmac_keys.next?
49
+ # next_hmac_keys = hmac_keys.next
50
+ # end
51
+ #
52
+ def next?
53
+ !token.nil?
54
+ end
55
+
56
+ ##
57
+ # Retrieve the next page of HMAC keys.
58
+ #
59
+ # @return [HmacKey::List]
60
+ #
61
+ # @example
62
+ # require "google/cloud/storage"
63
+ #
64
+ # storage = Google::Cloud::Storage.new
65
+ #
66
+ # hmac_keys = storage.hmac_keys
67
+ # if hmac_keys.next?
68
+ # next_hmac_keys = hmac_keys.next
69
+ # end
70
+ #
71
+ def next
72
+ return nil unless next?
73
+ ensure_service!
74
+ gapi = @service.list_hmac_keys \
75
+ service_account_email: @service_account_email,
76
+ show_deleted_keys: @show_deleted_keys, token: @token, max: @max,
77
+ user_project: @user_project
78
+
79
+ HmacKey::List.from_gapi \
80
+ gapi, @service,
81
+ service_account_email: @service_account_email,
82
+ show_deleted_keys: @show_deleted_keys, max: @max,
83
+ user_project: @user_project
84
+ end
85
+
86
+ ##
87
+ # Retrieves remaining results by repeatedly invoking {#next} until
88
+ # {#next?} returns `false`. Calls the given block once for each
89
+ # result, which is passed as the argument to the block.
90
+ #
91
+ # An Enumerator is returned if no block is given.
92
+ #
93
+ # This method will make repeated API calls until all remaining results
94
+ # are retrieved. (Unlike `#each`, for example, which merely iterates
95
+ # over the results returned by a single API call.) Use with caution.
96
+ #
97
+ # @param [Integer] request_limit The upper limit of API requests to
98
+ # make to load all HMAC keys. Default is no limit.
99
+ # @yield [key] The block for accessing each HMAC key.
100
+ # @yieldparam [HmacKey] key The HMAC key object.
101
+ #
102
+ # @return [Enumerator]
103
+ #
104
+ # @example Iterating each HMAC key by passing a block:
105
+ # require "google/cloud/storage"
106
+ #
107
+ # storage = Google::Cloud::Storage.new
108
+ #
109
+ # hmac_keys = storage.hmac_keys
110
+ # hmac_keys.all do |key|
111
+ # puts key.access_id
112
+ # end
113
+ #
114
+ # @example Using the enumerator by not passing a block:
115
+ # require "google/cloud/storage"
116
+ #
117
+ # storage = Google::Cloud::Storage.new
118
+ #
119
+ # hmac_keys = storage.hmac_keys
120
+ # all_names = hmac_keys.all.map do |key|
121
+ # key.access_id
122
+ # end
123
+ #
124
+ # @example Limit the number of API calls made:
125
+ # require "google/cloud/storage"
126
+ #
127
+ # storage = Google::Cloud::Storage.new
128
+ #
129
+ # hmac_keys = storage.hmac_keys
130
+ # hmac_keys.all(request_limit: 10) do |key|
131
+ # puts key.access_id
132
+ # end
133
+ #
134
+ def all request_limit: nil, &block
135
+ request_limit = request_limit.to_i if request_limit
136
+ unless block_given?
137
+ return enum_for :all, request_limit: request_limit
138
+ end
139
+ results = self
140
+ loop do
141
+ results.each(&block)
142
+ if request_limit
143
+ request_limit -= 1
144
+ break if request_limit.negative?
145
+ end
146
+ break unless results.next?
147
+ results = results.next
148
+ end
149
+ end
150
+
151
+ ##
152
+ # @private New HmacKey::List from a Google API Client
153
+ # Google::Apis::StorageV1::HmacKeysMetadata object.
154
+ def self.from_gapi gapi_list, service, service_account_email: nil,
155
+ show_deleted_keys: nil, max: nil, user_project: nil
156
+ hmac_keys = new(Array(gapi_list.items).map do |gapi_object|
157
+ HmacKey.from_gapi_metadata gapi_object, service,
158
+ user_project: user_project
159
+ end)
160
+ hmac_keys.instance_variable_set :@token, gapi_list.next_page_token
161
+ hmac_keys.instance_variable_set :@service, service
162
+ hmac_keys.instance_variable_set :@service_account_email,
163
+ service_account_email
164
+ hmac_keys.instance_variable_set :@show_deleted_keys,
165
+ show_deleted_keys
166
+ hmac_keys.instance_variable_set :@max, max
167
+ hmac_keys.instance_variable_set :@user_project, user_project
168
+ hmac_keys
169
+ end
170
+
171
+ protected
172
+
173
+ ##
174
+ # Raise an error unless an active connection is available.
175
+ def ensure_service!
176
+ raise "Must have active connection" unless @service
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,316 @@
1
+ # Copyright 2019 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "google/cloud/storage/hmac_key/list"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Storage
21
+ ##
22
+ # # HmacKey
23
+ #
24
+ # Represents the metadata for an HMAC key, and also includes the key's
25
+ # secret if returned by the {Project#create_hmac_key} creation method.
26
+ #
27
+ # @see https://cloud.google.com/storage/docs/migrating#migration-simple
28
+ # Migrating from Amazon S3 to Cloud Storage - Simple migration
29
+ #
30
+ # @attr_reader [String, nil] secret HMAC secret key material, or `nil` if
31
+ # the key was not returned by the {Project#create_hmac_key} creation
32
+ # method.
33
+ #
34
+ # @example
35
+ # require "google/cloud/storage"
36
+ #
37
+ # storage = Google::Cloud::Storage.new
38
+ #
39
+ # service_account_email = "my_account@developer.gserviceaccount.com"
40
+ # hmac_key = storage.create_hmac_key service_account_email
41
+ # hmac_key.secret # ...
42
+ #
43
+ # hmac_key = storage.hmac_key hmac_key.access_id
44
+ # hmac_key.secret # nil
45
+ #
46
+ # hmac_key.inactive!
47
+ # hmac_key.delete
48
+ #
49
+ class HmacKey
50
+ ##
51
+ # @private The Connection object.
52
+ attr_accessor :service
53
+
54
+ ##
55
+ # @private The Google API Client object.
56
+ attr_accessor :gapi
57
+
58
+ ##
59
+ # @private A boolean value or a project ID string to indicate the
60
+ # project to be billed for operations.
61
+ attr_accessor :user_project
62
+
63
+ attr_reader :secret
64
+
65
+ ##
66
+ # @private Creates an HmacKey object.
67
+ def initialize
68
+ @bucket = nil
69
+ @service = nil
70
+ @gapi = nil
71
+ @user_project = nil
72
+ end
73
+
74
+ ##
75
+ # The ID of the HMAC Key.
76
+ #
77
+ # @return [String]
78
+ #
79
+ def access_id
80
+ @gapi.access_id
81
+ end
82
+
83
+ ##
84
+ # A URL that can be used to access the HMAC key using the REST API.
85
+ #
86
+ # @return [String]
87
+ #
88
+ def api_url
89
+ @gapi.self_link
90
+ end
91
+
92
+ ##
93
+ # Creation time of the HMAC key.
94
+ #
95
+ # @return [String]
96
+ #
97
+ def created_at
98
+ @gapi.time_created
99
+ end
100
+
101
+ ##
102
+ # HTTP 1.1 Entity tag for the HMAC key.
103
+ #
104
+ # @return [String]
105
+ #
106
+ def etag
107
+ @gapi.etag
108
+ end
109
+
110
+ ##
111
+ # The ID of the HMAC key, including the Project ID and the Access ID.
112
+ #
113
+ # @return [String]
114
+ #
115
+ def id
116
+ @gapi.id
117
+ end
118
+
119
+ ##
120
+ # Project ID owning the service account to which the key authenticates.
121
+ #
122
+ # @return [String]
123
+ #
124
+ def project_id
125
+ @gapi.project_id
126
+ end
127
+
128
+ ##
129
+ # The email address of the key's associated service account.
130
+ #
131
+ # @return [String]
132
+ #
133
+ def service_account_email
134
+ @gapi.service_account_email
135
+ end
136
+
137
+ ##
138
+ # The state of the key. Can be one of `ACTIVE`, `INACTIVE`, or
139
+ # `DELETED`.
140
+ #
141
+ # @return [String]
142
+ #
143
+ def state
144
+ @gapi.state
145
+ end
146
+
147
+ ##
148
+ # Last modification time of the HMAC key metadata.
149
+ #
150
+ # @return [String]
151
+ #
152
+ def updated_at
153
+ @gapi.updated
154
+ end
155
+
156
+ ##
157
+ # Whether the state of the HMAC key is `ACTIVE`.
158
+ #
159
+ # @return [Boolean]
160
+ #
161
+ def active?
162
+ state == "ACTIVE"
163
+ end
164
+
165
+ ##
166
+ # Updates the state of the HMAC key to `ACTIVE`.
167
+ #
168
+ # @return [Google::Cloud::Storage::HmacKey] Returns the HMAC key for
169
+ # method chaining.
170
+ #
171
+ # @example
172
+ # require "google/cloud/storage"
173
+ #
174
+ # storage = Google::Cloud::Storage.new
175
+ #
176
+ # hmac_key = storage.hmac_keys.first
177
+ #
178
+ # hmac_key.active!
179
+ # hmac_key.state # "ACTIVE"
180
+ #
181
+ def active!
182
+ put_gapi! "ACTIVE"
183
+ self
184
+ end
185
+
186
+ ##
187
+ # Whether the state of the HMAC key is `INACTIVE`.
188
+ #
189
+ # @return [Boolean]
190
+ #
191
+ def inactive?
192
+ state == "INACTIVE"
193
+ end
194
+
195
+ ##
196
+ # Updates the state of the HMAC key to `INACTIVE`.
197
+ #
198
+ # @return [Google::Cloud::Storage::HmacKey] Returns the HMAC key for
199
+ # method chaining.
200
+ #
201
+ # @example
202
+ # require "google/cloud/storage"
203
+ #
204
+ # storage = Google::Cloud::Storage.new
205
+ #
206
+ # hmac_key = storage.hmac_keys.first
207
+ #
208
+ # hmac_key.inactive!
209
+ # hmac_key.state # "INACTIVE"
210
+ #
211
+ def inactive!
212
+ put_gapi! "INACTIVE"
213
+ self
214
+ end
215
+
216
+ ##
217
+ # Whether the state of the HMAC key is `DELETED`.
218
+ #
219
+ # @return [Boolean]
220
+ #
221
+ def deleted?
222
+ state == "DELETED"
223
+ end
224
+
225
+ ##
226
+ # Deletes the HMAC key, and loads the new state of the HMAC key,
227
+ # which will be `DELETED`.
228
+ #
229
+ # The API call to delete the HMAC key may be retried under certain
230
+ # conditions. See {Google::Cloud#storage} to control this behavior.
231
+ #
232
+ # @return [Google::Cloud::Storage::HmacKey] Returns the HMAC key for
233
+ # method chaining.
234
+ #
235
+ # @example
236
+ # require "google/cloud/storage"
237
+ #
238
+ # storage = Google::Cloud::Storage.new
239
+ #
240
+ # hmac_key = storage.hmac_keys.first
241
+ #
242
+ # hmac_key.inactive!.delete!
243
+ #
244
+ def delete!
245
+ ensure_service!
246
+ @service.delete_hmac_key access_id,
247
+ project_id: project_id,
248
+ user_project: @user_project
249
+ @gapi = @service.get_hmac_key access_id, project_id: project_id,
250
+ user_project: @user_project
251
+ self
252
+ end
253
+ alias delete delete!
254
+
255
+ ##
256
+ # Reloads the HMAC key with current data from the Storage service.
257
+ #
258
+ def reload!
259
+ ensure_service!
260
+ @gapi = service.get_hmac_key access_id, project_id: project_id,
261
+ user_project: user_project
262
+ self
263
+ end
264
+ alias refresh! reload!
265
+
266
+ ##
267
+ # @private New HmacKey from a Google::Apis::StorageV1::HmacKey object.
268
+ #
269
+ # @param [Google::Apis::StorageV1::HmacKey] gapi
270
+ #
271
+ #
272
+ def self.from_gapi gapi, service, user_project: nil
273
+ hmac_key = from_gapi_metadata gapi.metadata,
274
+ service,
275
+ user_project: user_project
276
+ hmac_key.tap do |f|
277
+ f.instance_variable_set :@secret, gapi.secret
278
+ end
279
+ end
280
+
281
+ ##
282
+ # @private New HmacKey from a Google::Apis::StorageV1::HmacKeyMetadata
283
+ # object.
284
+ #
285
+ # @param [Google::Apis::StorageV1::HmacKeyMetadata] gapi
286
+ #
287
+ #
288
+ def self.from_gapi_metadata gapi, service, user_project: nil
289
+ new.tap do |f|
290
+ f.gapi = gapi
291
+ f.service = service
292
+ f.user_project = user_project
293
+ end
294
+ end
295
+
296
+ protected
297
+
298
+ ##
299
+ # Raise an error unless an active service is available.
300
+ def ensure_service!
301
+ raise "Must have active connection" unless service
302
+ end
303
+
304
+ def put_gapi! new_state
305
+ ensure_service!
306
+ put_gapi = @gapi.dup
307
+ put_gapi.state = new_state
308
+ @gapi = service.update_hmac_key access_id, put_gapi,
309
+ project_id: project_id,
310
+ user_project: @user_project
311
+ self
312
+ end
313
+ end
314
+ end
315
+ end
316
+ end