bucket_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.gitlab-ci.yml +70 -0
  4. data/.idea/bucket_client.iml +105 -0
  5. data/.idea/encodings.xml +4 -0
  6. data/.idea/misc.xml +7 -0
  7. data/.idea/modules.xml +8 -0
  8. data/.idea/runConfigurations/Integration_Test.xml +37 -0
  9. data/.idea/runConfigurations/Unit_Test.xml +37 -0
  10. data/.rspec +3 -0
  11. data/CODE_OF_CONDUCT.md +74 -0
  12. data/Gemfile +6 -0
  13. data/Gemfile.lock +114 -0
  14. data/LICENSE.txt +21 -0
  15. data/README.md +870 -0
  16. data/Rakefile +6 -0
  17. data/bin/console +14 -0
  18. data/bin/setup +8 -0
  19. data/bucket_client.gemspec +46 -0
  20. data/integration/aws_blob_spec.rb +134 -0
  21. data/integration/aws_bucket_spec.rb +145 -0
  22. data/integration/azure_blob_spec.rb +132 -0
  23. data/integration/azure_bucket_spec.rb +132 -0
  24. data/integration/dev_blob_spec.rb +131 -0
  25. data/integration/dev_bucket_spec.rb +140 -0
  26. data/integration/do_blob_spec.rb +134 -0
  27. data/integration/do_bucket_spec.rb +144 -0
  28. data/integration/gcp_blob_spec.rb +132 -0
  29. data/integration/gcp_bucket_spec.rb +132 -0
  30. data/integration/img.jpg +0 -0
  31. data/lib/bucket_client.rb +66 -0
  32. data/lib/bucket_client/aws/aws_bucket.rb +85 -0
  33. data/lib/bucket_client/aws/aws_client.rb +195 -0
  34. data/lib/bucket_client/aws/aws_http_client.rb +32 -0
  35. data/lib/bucket_client/aws/aws_policy_factory.rb +26 -0
  36. data/lib/bucket_client/aws4_request_signer.rb +133 -0
  37. data/lib/bucket_client/azure/azure_bucket.rb +83 -0
  38. data/lib/bucket_client/azure/azure_client.rb +197 -0
  39. data/lib/bucket_client/bucket.rb +388 -0
  40. data/lib/bucket_client/bucket_operation_exception.rb +8 -0
  41. data/lib/bucket_client/client.rb +408 -0
  42. data/lib/bucket_client/dev/local_bucket.rb +84 -0
  43. data/lib/bucket_client/dev/local_client.rb +148 -0
  44. data/lib/bucket_client/digital_ocean/digital_ocean_acl_factory.rb +39 -0
  45. data/lib/bucket_client/digital_ocean/digital_ocean_bucket.rb +81 -0
  46. data/lib/bucket_client/digital_ocean/digital_ocean_client.rb +275 -0
  47. data/lib/bucket_client/digital_ocean/digital_ocean_http_client.rb +31 -0
  48. data/lib/bucket_client/gcp/gcp_bucket.rb +79 -0
  49. data/lib/bucket_client/gcp/gcp_client.rb +171 -0
  50. data/lib/bucket_client/operation_result.rb +33 -0
  51. data/lib/bucket_client/version.rb +3 -0
  52. metadata +246 -0
@@ -0,0 +1,83 @@
1
+ module BucketClient
2
+ class AzureBucket < Bucket
3
+
4
+ attr_reader :key
5
+
6
+ # @param [AzureClient] parent
7
+ # @param [String] key
8
+ # @param [Azure::Storage::Blob::BlobService] client
9
+ def initialize(parent, key, client, master)
10
+ @bucket_client = parent
11
+ @client = client
12
+ @key = key
13
+ @master = master
14
+ end
15
+
16
+ def get_uri(key)
17
+ "https://#{@master}.blob.core.windows.net/#{@key}/#{key}"
18
+ end
19
+
20
+ def get_blob_with_uri(uri)
21
+ @bucket_client.get_blob uri
22
+ end
23
+
24
+ def exist_blob_with_uri(uri)
25
+ @bucket_client.exist_blob(uri)
26
+ end
27
+
28
+ def put_blob_with_uri(payload, uri)
29
+ @bucket_client.put_blob payload, uri
30
+ end
31
+
32
+ def update_blob_with_uri(payload, uri)
33
+ exist = exist_blob_with_uri uri
34
+ if exist
35
+ put_blob_with_uri payload, uri
36
+ else
37
+ OperationResult.new false, "Blob does not exist", nil, 400
38
+ end
39
+ end
40
+
41
+ def delete_blob_with_uri(uri)
42
+ @bucket_client.delete_blob(uri)
43
+ end
44
+
45
+ def delete_blob_if_exist_with_uri(uri)
46
+ @bucket_client.delete_blob_if_exist(uri)
47
+ end
48
+
49
+ def get_blob(key)
50
+ get_blob_with_uri(get_uri key)
51
+ end
52
+
53
+ def exist_blob(key)
54
+ exist_blob_with_uri(get_uri key)
55
+ end
56
+
57
+ def put_blob(payload, key)
58
+ put_blob_with_uri(payload, get_uri(key))
59
+ end
60
+
61
+ def create_blob(payload, key)
62
+ exist = exist_blob key
63
+ if exist
64
+ OperationResult.new false, "Blob already exist", nil, 400
65
+ else
66
+ put_blob payload, key
67
+ end
68
+ end
69
+
70
+ def update_blob(payload, key)
71
+ update_blob_with_uri(payload, get_uri(key))
72
+ end
73
+
74
+ def delete_blob(key)
75
+ delete_blob_with_uri(get_uri key)
76
+ end
77
+
78
+ def delete_blob_if_exist(key)
79
+ delete_blob_if_exist_with_uri(get_uri key)
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,197 @@
1
+ require "azure/storage/blob"
2
+ require "addressable/uri"
3
+ require "bucket_client/azure/azure_bucket"
4
+ require "bucket_client/operation_result"
5
+ require "mimemagic"
6
+ require "json"
7
+
8
+ module BucketClient
9
+ class AzureClient < Client
10
+
11
+ def initialize(account, secret)
12
+ @client = Azure::Storage::Blob::BlobService.create(
13
+ storage_account_name: account,
14
+ storage_access_key: secret
15
+ )
16
+ @acc = account
17
+ end
18
+
19
+ def exist_bucket(key)
20
+ begin
21
+ @client.get_container_metadata key
22
+ true
23
+ rescue Azure::Core::Http::HTTPError => e
24
+ if e.status_code === 404
25
+ false
26
+ else
27
+ raise e
28
+ end
29
+ end
30
+ end
31
+
32
+ def get_bucket!(key)
33
+ AzureBucket.new(self, key, @client, @acc)
34
+ end
35
+
36
+ def put_bucket(key)
37
+ exist = exist_bucket key
38
+ if exist
39
+ bucket = get_bucket! key
40
+ OperationResult.new(true, "OK", bucket, 200)
41
+ else
42
+ create_bucket key
43
+ end
44
+ end
45
+
46
+ def create_bucket(key)
47
+ exist = exist_bucket key
48
+ return OperationResult.new(false, "Bucket already exist", nil, 409) if exist
49
+ begin
50
+ @client.create_container key
51
+ value = get_bucket! key
52
+ OperationResult.new(true, "Azure Container created", value, 200)
53
+ rescue Azure::Core::Http::HTTPError => e
54
+ if e.status_code === 409
55
+ sleep(3)
56
+ create_bucket key
57
+ else
58
+ OperationResult.new(false, e.description, nil, e.status_code)
59
+ end
60
+
61
+ rescue StandardError => e
62
+ OperationResult.new(false, e.message, nil, 400)
63
+ end
64
+ end
65
+
66
+ def delete_bucket(key)
67
+ begin
68
+ @client.delete_container key
69
+ OperationResult.new(true, "Container deleted", nil, 200)
70
+ rescue Azure::Core::Http::HTTPError => e
71
+ OperationResult.new(false, e.description, nil, e.status_code)
72
+ rescue StandardError => e
73
+ OperationResult.new(false, e.message, nil, 400)
74
+ end
75
+ end
76
+
77
+ def delete_bucket_if_exist(key)
78
+ exist = exist_bucket key
79
+ if exist
80
+ delete_bucket key
81
+ else
82
+ OperationResult.new(true, "Bucket already deleted", nil, 200)
83
+ end
84
+ end
85
+
86
+ def set_read_policy(key, access)
87
+ raise ArgumentError.new("Read Policy not accepted") if access != :public && access != :private
88
+ begin
89
+ level = access === :public ? "blob" : ""
90
+ @client.set_container_acl key, level
91
+ OperationResult.new(true, "OK", nil, 200)
92
+ rescue Azure::Core::Http::HTTPError => e
93
+ OperationResult.new(false, e.description, nil, e.status_code)
94
+ rescue StandardError => e
95
+ OperationResult.new(false, e.message, nil, 400)
96
+ end
97
+ end
98
+
99
+ def set_get_cors(key, cors)
100
+ begin
101
+ cors_rule = Azure::Storage::BlobService::CorsRule.new
102
+ cors_rule.allowed_origins = cors
103
+ cors_rule.allowed_methods = ["GET"]
104
+ cors_rule.allowed_headers = ['*']
105
+ cors_rule.exposed_headers = ['*']
106
+ service_properties = Azure::Storage::Blob::StorageServiceProperties.new
107
+ service_properties.cors.cors_rules = [cors_rule]
108
+ @client.set_service_properties(service_properties)
109
+ OperationResult.new(true, "OK", nil, 200)
110
+ rescue Azure::Core::Http::HTTPError => e
111
+ OperationResult.new(false, e.description, nil, e.status_code)
112
+ rescue StandardError => e
113
+ OperationResult.new(false, e.message, nil, 400)
114
+ end
115
+ end
116
+
117
+ def get_blob(uri)
118
+ data = break_uri uri
119
+ begin
120
+ _, content = @client.get_blob data[:bucket], data[:blob]
121
+ OperationResult.new(true, "", content, 200)
122
+ rescue Azure::Core::Http::HTTPError => e
123
+ OperationResult.new(false, e.description, nil, e.status_code)
124
+ rescue StandardError => e
125
+ OperationResult.new(false, e.message, nil, 400)
126
+ end
127
+ end
128
+
129
+ def exist_blob(uri)
130
+ begin
131
+ data = break_uri uri
132
+ @client.get_blob_metadata data[:bucket], data[:blob]
133
+ true
134
+ rescue Azure::Core::Http::HTTPError => e
135
+ if e.status_code === 404
136
+ false
137
+ end
138
+ end
139
+ end
140
+
141
+ def put_blob(payload, uri)
142
+ mime = MimeMagic.by_magic payload
143
+ data = break_uri uri
144
+ begin
145
+ @client.create_block_blob data[:bucket], data[:blob], payload, {:content_type => mime.type}
146
+ OperationResult.new(true, "OK", uri, 204)
147
+ rescue Azure::Core::Http::HTTPError => e
148
+ OperationResult.new(false, e.description, nil, e.status_code)
149
+ rescue StandardError => e
150
+ OperationResult.new(false, e.message, nil, 400)
151
+ end
152
+ end
153
+
154
+ def update_blob(payload, uri)
155
+ exist = exist_blob uri
156
+ if exist
157
+ put_blob payload, uri
158
+ else
159
+ OperationResult.new(false, "Blob does not exist", nil, 404)
160
+ end
161
+ end
162
+
163
+ def delete_blob_if_exist(uri)
164
+ exist = exist_blob uri
165
+ if exist
166
+ delete_blob uri
167
+ else
168
+ OperationResult.new(true, "Blob already deleted", nil, 204)
169
+ end
170
+ end
171
+
172
+ def delete_blob(uri)
173
+ data = break_uri uri
174
+ begin
175
+ @client.delete_blob(data[:bucket], data[:blob])
176
+ OperationResult.new(true, "Deleted Blob", nil, 204)
177
+ rescue Azure::Core::Http::HTTPError => e
178
+ OperationResult.new(false, e.description, nil, e.status_code)
179
+ rescue StandardError => e
180
+ OperationResult.new(false, e.message, nil, 400)
181
+ end
182
+ end
183
+
184
+ private
185
+
186
+ def break_uri(uri)
187
+ url = Addressable::URI.parse uri
188
+ fragments = url.path.split("/").select {|x| !x.nil? && !x.empty?}
189
+ bucket = fragments[0]
190
+ blob = fragments.drop(1).join "/"
191
+ {bucket: bucket, blob: blob}
192
+ end
193
+
194
+ end
195
+ end
196
+
197
+
@@ -0,0 +1,388 @@
1
+ require "bucket_client/bucket_operation_exception"
2
+
3
+ module BucketClient
4
+ module KeyMethod
5
+ # Get blob as byte array
6
+ #
7
+ # result = bucket.get_blob "image.png"
8
+ # result.success #=> Whether the operation succeeded
9
+ # result.code #=> Status Code of the operation
10
+ # result.message #=> Error message if it failed
11
+ # result.value #=> the byte array of the blob
12
+ #
13
+ # @param [String] key the blob id or name
14
+ # @return [OperationResult]
15
+ def get_blob(key)
16
+ raise NotImplementedError key
17
+ end
18
+
19
+ # Get blob as byte array
20
+ #
21
+ # Raises exception if the operation fails.
22
+ #
23
+ # img = bucket.get_blob! "image.png"
24
+ #
25
+ # @param [String] key the blob id or name
26
+ # @return [Array<Byte>]
27
+ def get_blob!(key)
28
+ result = get_blob key
29
+ raise BucketOperationException.new(result) unless result.success
30
+ result.value
31
+ end
32
+
33
+ # Check if the blob exist
34
+ #
35
+ # Raises exception if operation fails
36
+ #
37
+ # exist = bucket.exist_blob "image.png" #=> true if exist, false if not
38
+ #
39
+ # @param [String] key the blob id or name
40
+ # @return [Boolean]
41
+ def exist_blob(key)
42
+ raise NotImplementedError key
43
+ end
44
+
45
+ # Create blob with payload
46
+ # Fails if blob already exist
47
+ #
48
+ # img = IO.binread "image.png"
49
+ # result = bucket.create_blob img, "image.png"
50
+ # result.success #=> Whether the operation succeeded
51
+ # result.code #=> Status Code of the operation
52
+ # result.message #=> Error message if it failed
53
+ # result.value #=> URI of the blob
54
+ #
55
+ # @param [Array<Byte>] payload the payload to create blob with
56
+ # @param [String] key the blob id or name
57
+ # @return [OperationResult]
58
+ def create_blob(payload, key)
59
+ raise NotImplementedError payload.to_s, key
60
+ end
61
+
62
+ # Creates blob with payload
63
+ # Fails if blob already exist
64
+ #
65
+ # Raises exception if operation fails
66
+ #
67
+ # img = IO.binread "image.png"
68
+ # uri = bucket.create_blob! img, "image.png" #=> URI of the created blob
69
+ #
70
+ # @param [Array<Byte>] payload the payload to create blob with
71
+ # @param [String] key the blob id or name
72
+ # @return [String]
73
+ def create_blob!(payload, key)
74
+ result = create_blob payload, key
75
+ raise BucketOperationException.new(result) unless result.success
76
+ result.value
77
+ end
78
+
79
+ # Updates the blob with new payload
80
+ # Fails if blob does not exist
81
+ #
82
+ # img = IO.binread "image.png"
83
+ # result = bucket.update_blob img, "image.png"
84
+ # result.success #=> whether the operation succeeded
85
+ # result.code #=> Status Code of the operation
86
+ # result.message #=> Error message if it failed
87
+ # result.value #=> URI of the blob
88
+ #
89
+ # @param [Array<Byte>] payload the payload to update
90
+ # @param [String] key the blob id or name
91
+ # @return [OperationResult]
92
+ def update_blob(payload, key)
93
+ raise NotImplementedError payload.to_s, key
94
+ end
95
+
96
+ # Updates the blob with new payload
97
+ # Fails if blob does not exist
98
+ # Raises exception if operation fails
99
+ #
100
+ # img = IO.binread "image.png"
101
+ # result = bucket.update_blob!(img, "image.png") #=> URI of updated blob
102
+ #
103
+ # @param [Array<Byte>] payload the payload to update
104
+ # @param [String] key the blob id or name
105
+ # @return [String]
106
+ def update_blob!(payload, key)
107
+ result = update_blob payload, key
108
+ raise BucketOperationException.new(result) unless result.success
109
+ result.value
110
+ end
111
+
112
+ # Creates a new blob with payload if blob does not exist
113
+ # Updates blob with new payload if blob exist
114
+ #
115
+ # img = IO.binread "image.png"
116
+ # result = bucket.put_blob(img, "image.png")
117
+ # result.success #=> whether the operation succeeded
118
+ # result.code #=> Status Code of the operation
119
+ # result.message #=> Error message if it failed
120
+ # result.value #=> URI of the blob
121
+ #
122
+ # @param [Array<Byte>] payload the payload to put
123
+ # @param [String] key the blob id or name
124
+ # @return [OperationResult]
125
+ def put_blob(payload, key)
126
+ raise NotImplementedError payload.to_s, key
127
+ end
128
+
129
+ # Creates a new blob with payload if blob does not exist
130
+ # Updates blob with new payload if blob exist
131
+ #
132
+ # Raises exception if operation fails
133
+ #
134
+ # img = IO.binread "image.png"
135
+ # uri = bucket.put_blob! img, "image.png" #=> uri of the blob
136
+ #
137
+ # @param [Array<Byte>] payload the payload to put
138
+ # @param [String] key the blob id or name
139
+ # @return [String]
140
+ def put_blob!(payload, key)
141
+ result = put_blob payload, key
142
+ raise BucketOperationException.new(result) unless result.success
143
+ result.value
144
+ end
145
+
146
+ # Deletes a blob
147
+ # Fails if the blob does not exist. To prevent this behaviour, use
148
+ # delete_blob_if_exist method
149
+ #
150
+ # result = bucket.delete_blob "image.png"
151
+ # result.success #=> whether the operation succeeded
152
+ # result.code #=> Status Code of the operation
153
+ # result.message #=> Error message if it failed
154
+ # result.value #=> nil
155
+ #
156
+ # @param [String] key the blob id or name
157
+ # @return [OperationResult]
158
+ def delete_blob(key)
159
+ raise NotImplementedError key
160
+ end
161
+
162
+ # Deletes a blob
163
+ # Fails if the blob does not exist. To prevent this behaviour, use
164
+ # delete_blob_if_exist method
165
+ #
166
+ # Raises exception if the operation fails
167
+ #
168
+ # bucket.delete_blob! "image.png"
169
+ # @param [String] key the blob id or name
170
+ def delete_blob!(key)
171
+ result = delete_blob key
172
+ raise BucketOperationException.new(result) unless result.success
173
+ end
174
+
175
+ # Deletes a blob if it exist
176
+ #
177
+ # result = bucket.delete_blob_if_exist "image.png"
178
+ # result.success #=> whether the operation succeeded
179
+ # result.code #=> Status Code of the operation
180
+ # result.message #=> Error message if it failed
181
+ # result.value #=> nil
182
+ #
183
+ # @param [String] key the blob id or name
184
+ # @return [OperationResult]
185
+ def delete_blob_if_exist(key)
186
+ raise NotImplementedError key
187
+ end
188
+
189
+ # Deletes a blob if it exist
190
+ # Raises exception if operation fails
191
+ #
192
+ # bucket.delete_blob_if_exist! "image.png"
193
+ #
194
+ # @param [String] key the blob id or name
195
+ def delete_blob_if_exist!(key)
196
+ result = delete_blob_if_exist key
197
+ raise BucketOperationException.new(result) unless result.success
198
+ end
199
+ end
200
+
201
+ module UriMethod
202
+ # Gets the blob in the target URL as byte array.
203
+ #
204
+ # blob = bucket.get_blob_with_uri "https://host.com/bucket"
205
+ # result.success #=> Whether the operation succeeded
206
+ # result.code #=> Status Code of the operation
207
+ # result.message #=> Error message if it failed
208
+ # result.value #=> payload of the blob as byte array
209
+ #
210
+ # @param [String] uri the endpoint of the blob you want to get
211
+ # @return [OperationResult]
212
+ def get_blob_with_uri(uri)
213
+ raise NotImplementedError uri
214
+ end
215
+
216
+ # Gets the blob in the target URL as byte array
217
+ #
218
+ # Raises exception if operation fails
219
+ #
220
+ # blob = bucket.get_blob_with_uri! "https://host.com/bucket" #=> [12,65,127,33] (some byte array)
221
+ #
222
+ # @param [String] uri the endpoint of the blob you want to get
223
+ # @return [Array<Byte>]
224
+ def get_blob_with_uri!(uri)
225
+ result = get_blob_with_uri(uri)
226
+ raise BucketOperationException.new(result) unless result.success
227
+ result.value
228
+ end
229
+
230
+ # Check if blob exist
231
+ #
232
+ # Raises exception if the operation fails
233
+ #
234
+ # exist = bucket.exist_blob_with_uri "https://host.com/folder/blob.ext"
235
+ # exist #=> true if exist, false if does not exist
236
+ #
237
+ # @param [String] uri the URL of the blob
238
+ # @return [Boolean]
239
+ def exist_blob_with_uri(uri)
240
+ raise NotImplementedError uri
241
+ end
242
+
243
+ # Updates a blob with new payload in byte array
244
+ # Fails if blob with the URI doesn't exist
245
+ #
246
+ # img = IO.binread "pic.png"
247
+ # uri = "https://host.com/folder/pic.png"
248
+ # result = bucket.update_blob_with_uri img, uri
249
+ # result.success #=> Whether the operation succeeded
250
+ # result.code #=> Status Code of the operation
251
+ # result.message #=> Error message if it failed
252
+ # result.value #=> Uri of update blob
253
+ #
254
+ # @param [Array<Byte>] payload the payload to update the blob
255
+ # @param [String] uri the URL of the blob
256
+ # @return [OperationResult]
257
+ def update_blob_with_uri(payload, uri)
258
+ raise NotImplementedError payload.to_s + uri
259
+ end
260
+
261
+ # Updates a blob with new payload in byte array
262
+ # Fails if blob with the URI doesn't exist
263
+ # Raises exception when fails
264
+ #
265
+ # img = IO.binread "pic.png"
266
+ # uri = "https://host.com/folder/pic.png"
267
+ # result = bucket.update_blob_with_uri!(img, uri) #=> URI of update blob
268
+ #
269
+ # @param [Array<Byte>] payload the payload to update the blob
270
+ # @param [String] uri the URL of the blob
271
+ def update_blob_with_uri!(payload, uri)
272
+ result = update_blob_with_uri payload, uri
273
+ raise BucketOperationException.new(result) unless result.success
274
+ result.value
275
+ end
276
+
277
+ # Creates the blob with the payload if it does not exist
278
+ # Updates the blob with the new payload if it exist
279
+ #
280
+ # img = IO.binread "pic.png"
281
+ # uri = "https://host.com/folder/pic.png"
282
+ # result = bucket.put_blob_with_uri img, uri
283
+ # result.success #=> whether the operation succeeded
284
+ # result.code #=> Status code of the operation
285
+ # result.message #=> Error message if the operation failed
286
+ # result.value #=> Uri of blob
287
+ #
288
+ # @param [Array<Byte>] payload the payload to put
289
+ # @param [String] uri the URL of the blob
290
+ # @return [OperationResult]
291
+ def put_blob_with_uri(payload, uri)
292
+ raise NotImplementedError payload.to_s + uri
293
+ end
294
+
295
+ # Creates the blob with the payload if it does not exist,
296
+ # Updates the blob with the new payload if it exist
297
+ #
298
+ # Raises exception if the operation fails
299
+ #
300
+ # img = IO.binread "pic.png"
301
+ # uri = "https://host.com/folder/pic.png"
302
+ # result = bucket.put_blob_with_uri!(img,uri) #=> returns URI of updated blob
303
+ #
304
+ # @param [Array<Byte>] payload the payload to put
305
+ # @param [String] uri the URL of the blob
306
+ # @return [String]
307
+ def put_blob_with_uri!(payload, uri)
308
+ result = put_blob_with_uri payload, uri
309
+ raise BucketOperationException.new(result) unless result.success
310
+ result.value
311
+ end
312
+
313
+ # Deletes the blob in the provided URI
314
+ # Fails if the blob does not exist. Use delete_blob_if_exist if you
315
+ # do not want this behaviour
316
+ #
317
+ # uri = "https://host.com/folder/pic.png"
318
+ # result = bucket.delete_blob_with_uri uri
319
+ # result.success #=> whether the operation succeeded
320
+ # result.code #=> Status code of the operation
321
+ # result.message #=> Error message if the operation failed
322
+ # result.value #=> nil
323
+ #
324
+ # @param [String] uri the URL of the blob
325
+ # @return [OperationResult]
326
+ def delete_blob_with_uri(uri)
327
+ raise NotImplementedError uri
328
+ end
329
+
330
+ # Deletes the blob in the provided URI
331
+ # Fails if the blob does not exist. Use delete_blob_if_exist if you
332
+ # do not want this behaviour
333
+ #
334
+ # Raises exception if the operation fails
335
+ #
336
+ # uri = "https://host.com/folder/pic.png"
337
+ # bucket.delete_blob_with_uri! uri
338
+ #
339
+ # @param [String] uri URL of the blob
340
+ def delete_blob_with_uri!(uri)
341
+ result = delete_blob_with_uri uri
342
+ raise BucketOperationException.new(result) unless result.success
343
+ end
344
+
345
+ # Deletes the blob if it exist, else does nothing
346
+ #
347
+ # uri = "https://host.com/folder/pic.png"
348
+ # result = bucket.delete_blob_with_uri uri
349
+ # result.success #=> whether the operation succeeded
350
+ # result.code #=> Status code of the operation
351
+ # result.message #=> Error message if the operation failed
352
+ # result.value #=> nil
353
+ #
354
+ # @param [String] uri the URL of the blob
355
+ # @return [OperationResult]
356
+ def delete_blob_if_exist_with_uri(uri)
357
+ raise NotImplementedError uri
358
+ end
359
+
360
+ # Deletes the blob if it exist, else does nothing
361
+ # Raises exception if the operation fails
362
+ #
363
+ # uri = "https://host.com/folder/pic.png"
364
+ # bucket.delete_blob_with_uri! uri
365
+ #
366
+ # @param [String] uri the URL of the blob
367
+ def delete_blob_if_exist_with_uri!(uri)
368
+ result = delete_blob_if_exist_with_uri uri
369
+ raise BucketOperationException.new(result) unless result.success
370
+ end
371
+ end
372
+
373
+ class Bucket
374
+
375
+ include KeyMethod, UriMethod
376
+
377
+ # Obtains the URI of the blob from key of the blob
378
+ # Does not raise exception even if blob does not exist
379
+ #
380
+ # uri = bucket.get_uri("image.png") #=> https://host.com/bucket/image.png
381
+ #
382
+ # @param [String] key the blob id or name
383
+ # @return [String]
384
+ def get_uri(key)
385
+ raise NotImplementedError key
386
+ end
387
+ end
388
+ end