Class: Gcloud::Storage::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/storage/service.rb

Overview

as well as expose the API calls.

Constant Summary collapse

API =

Alias to the Google Client API module

Google::Apis::StorageV1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials, retries: nil, timeout: nil) ⇒ Service

Creates a new Service instance.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gcloud/storage/service.rb', line 41

def initialize project, credentials, retries: nil, timeout: nil
  @project = project
  @credentials = credentials
  @credentials = credentials
  @service = API::StorageService.new
  @service.client_options.application_name    = "gcloud-ruby"
  @service.client_options.application_version = Gcloud::VERSION
  @service.request_options.retries = retries || 3
  @service.request_options.timeout_sec = timeout if timeout
  @service.authorization = @credentials.client
end

Instance Attribute Details

#credentialsObject



37
38
39
# File 'lib/gcloud/storage/service.rb', line 37

def credentials
  @credentials
end

#mocked_serviceObject

Returns the value of attribute mocked_service.



57
58
59
# File 'lib/gcloud/storage/service.rb', line 57

def mocked_service
  @mocked_service
end

#projectObject



34
35
36
# File 'lib/gcloud/storage/service.rb', line 34

def project
  @project
end

Instance Method Details

#copy_file(source_bucket_name, source_file_path, destination_bucket_name, destination_file_path, options = {}) ⇒ Object

Copy a file from source bucket/object to a destination bucket/object.



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/gcloud/storage/service.rb', line 212

def copy_file source_bucket_name, source_file_path,
              destination_bucket_name, destination_file_path, options = {}
  service.copy_object \
    source_bucket_name, source_file_path,
    destination_bucket_name, destination_file_path,
    destination_predefined_acl: options[:acl],
    source_generation: options[:generation],
    options: key_options(key: options[:key],
                         key_sha256: options[:key_sha256])
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#delete_bucket(bucket_name) ⇒ Object

Permanently deletes an empty bucket.



107
108
109
110
111
# File 'lib/gcloud/storage/service.rb', line 107

def delete_bucket bucket_name
  service.delete_bucket bucket_name
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#delete_bucket_acl(bucket_name, entity) ⇒ Object

Permanently deletes a bucket ACL.



133
134
135
136
137
# File 'lib/gcloud/storage/service.rb', line 133

def delete_bucket_acl bucket_name, entity
  service.delete_bucket_access_control bucket_name, entity
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#delete_default_acl(bucket_name, entity) ⇒ Object

Permanently deletes a default ACL.



159
160
161
162
163
# File 'lib/gcloud/storage/service.rb', line 159

def delete_default_acl bucket_name, entity
  service.delete_default_object_access_control bucket_name, entity
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#delete_file(bucket_name, file_path) ⇒ Object

Permanently deletes a file.



251
252
253
254
255
# File 'lib/gcloud/storage/service.rb', line 251

def delete_file bucket_name, file_path
  service.delete_object bucket_name, file_path
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#delete_file_acl(bucket_name, file_name, entity, options = {}) ⇒ Object

Permanently deletes a file ACL.



278
279
280
281
282
283
# File 'lib/gcloud/storage/service.rb', line 278

def delete_file_acl bucket_name, file_name, entity, options = {}
  service.delete_object_access_control \
    bucket_name, file_name, entity, generation: options[:generation]
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#download_file(bucket_name, file_path, target_path, generation: nil, key: nil, key_sha256: nil) ⇒ Object

Download contents of a file.



227
228
229
230
231
232
233
234
235
# File 'lib/gcloud/storage/service.rb', line 227

def download_file bucket_name, file_path, target_path, generation: nil,
                  key: nil, key_sha256: nil
  service.get_object \
    bucket_name, file_path,
    download_dest: target_path, generation: generation,
    options: key_options(key: key, key_sha256: key_sha256)
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#get_bucket(bucket_name) ⇒ Object

Retrieves bucket by name. Returns Google::Apis::StorageV1::Bucket.



71
72
73
74
75
# File 'lib/gcloud/storage/service.rb', line 71

def get_bucket bucket_name
  service.get_bucket bucket_name
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#get_file(bucket_name, file_path, generation: nil, key: nil, key_sha256: nil) ⇒ Object

Retrieves an object or its metadata.



200
201
202
203
204
205
206
207
208
# File 'lib/gcloud/storage/service.rb', line 200

def get_file bucket_name, file_path, generation: nil, key: nil,
             key_sha256: nil
  service.get_object \
    bucket_name, file_path,
    generation: generation,
    options: key_options(key: key, key_sha256: key_sha256)
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#insert_bucket(bucket_gapi, options = {}) ⇒ Object

Creates a new bucket. Returns Google::Apis::StorageV1::Bucket.



80
81
82
83
84
85
86
87
# File 'lib/gcloud/storage/service.rb', line 80

def insert_bucket bucket_gapi, options = {}
  service.insert_bucket \
    @project, bucket_gapi,
    predefined_acl: options[:acl],
    predefined_default_object_acl: options[:default_acl]
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#insert_bucket_acl(bucket_name, entity, role) ⇒ Object

Creates a new bucket ACL.



123
124
125
126
127
128
129
# File 'lib/gcloud/storage/service.rb', line 123

def insert_bucket_acl bucket_name, entity, role
  new_acl = Google::Apis::StorageV1::BucketAccessControl.new \
    entity: entity, role: role
  service.insert_bucket_access_control bucket_name, new_acl
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#insert_default_acl(bucket_name, entity, role) ⇒ Object

Creates a new default ACL.



149
150
151
152
153
154
155
# File 'lib/gcloud/storage/service.rb', line 149

def insert_default_acl bucket_name, entity, role
  new_acl = Google::Apis::StorageV1::ObjectAccessControl.new \
    entity: entity, role: role
  service.insert_default_object_access_control bucket_name, new_acl
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#insert_file(bucket_name, source, path = nil, acl: nil, cache_control: nil, content_disposition: nil, content_encoding: nil, content_language: nil, content_type: nil, crc32c: nil, md5: nil, metadata: nil, key: nil, key_sha256: nil) ⇒ Object

Inserts a new file for the given bucket



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/gcloud/storage/service.rb', line 178

def insert_file bucket_name, source, path = nil, acl: nil,
                cache_control: nil, content_disposition: nil,
                content_encoding: nil, content_language: nil,
                content_type: nil, crc32c: nil, md5: nil, metadata: nil,
                key: nil, key_sha256: nil
  file_obj = Google::Apis::StorageV1::Object.new \
    cache_control: cache_control, content_type: content_type,
    content_disposition: content_disposition, md5_hash: md5,
    content_encoding: content_encoding, crc32c: crc32c,
    content_language: content_language, metadata: 
  content_type ||= mime_type_for(Pathname(source).to_path)
  service.insert_object \
    bucket_name, file_obj,
    name: path, predefined_acl: acl, upload_source: source,
    content_encoding: content_encoding, content_type: content_type,
    options: key_options(key: key, key_sha256: key_sha256)
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#insert_file_acl(bucket_name, file_name, entity, role, options = {}) ⇒ Object

Creates a new file ACL.



267
268
269
270
271
272
273
274
# File 'lib/gcloud/storage/service.rb', line 267

def insert_file_acl bucket_name, file_name, entity, role, options = {}
  new_acl = Google::Apis::StorageV1::ObjectAccessControl.new \
    entity: entity, role: role
  service.insert_object_access_control \
    bucket_name, file_name, new_acl, generation: options[:generation]
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#inspectObject



293
294
295
# File 'lib/gcloud/storage/service.rb', line 293

def inspect
  "#{self.class}(#{@project})"
end

#list_bucket_acls(bucket_name) ⇒ Object

Retrieves a list of ACLs for the given bucket.



115
116
117
118
119
# File 'lib/gcloud/storage/service.rb', line 115

def list_bucket_acls bucket_name
  service.list_bucket_access_controls bucket_name
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#list_buckets(prefix: nil, token: nil, max: nil) ⇒ Object

Retrieves a list of buckets for the given project.



61
62
63
64
65
66
# File 'lib/gcloud/storage/service.rb', line 61

def list_buckets prefix: nil, token: nil, max: nil
  service.list_buckets @project, prefix: prefix, page_token: token,
                                 max_results: max
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#list_default_acls(bucket_name) ⇒ Object

Retrieves a list of default ACLs for the given bucket.



141
142
143
144
145
# File 'lib/gcloud/storage/service.rb', line 141

def list_default_acls bucket_name
  service.list_default_object_access_controls bucket_name
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#list_file_acls(bucket_name, file_name) ⇒ Object

Retrieves a list of ACLs for the given file.



259
260
261
262
263
# File 'lib/gcloud/storage/service.rb', line 259

def list_file_acls bucket_name, file_name
  service.list_object_access_controls bucket_name, file_name
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#list_files(bucket_name, options = {}) ⇒ Object

Retrieves a list of files matching the criteria.



167
168
169
170
171
172
173
174
# File 'lib/gcloud/storage/service.rb', line 167

def list_files bucket_name, options = {}
  service.list_objects \
    bucket_name, delimiter: options[:delimiter],
                 max_results: options[:max], page_token: options[:token],
                 prefix: options[:prefix], versions: options[:versions]
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#mime_type_for(path) ⇒ Object

Retrieves the mime-type for a file path. An empty string is returned if no mime-type can be found.



288
289
290
# File 'lib/gcloud/storage/service.rb', line 288

def mime_type_for path
  MIME::Types.of(path).first.to_s
end

#patch_bucket(bucket_name, bucket_gapi = nil, predefined_acl: nil, predefined_default_acl: nil) ⇒ Object

Updates a bucket, including its ACL metadata.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gcloud/storage/service.rb', line 91

def patch_bucket bucket_name, bucket_gapi = nil, predefined_acl: nil,
                 predefined_default_acl: nil
  bucket_gapi ||= Google::Apis::StorageV1::Bucket.new
  bucket_gapi.acl = nil if predefined_acl
  bucket_gapi.default_object_acl = nil if predefined_default_acl

  service.patch_bucket \
    bucket_name, bucket_gapi,
    predefined_acl: predefined_acl,
    predefined_default_object_acl: predefined_default_acl
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#patch_file(bucket_name, file_path, file_gapi = nil, predefined_acl: nil) ⇒ Object

Updates a file’s metadata.



239
240
241
242
243
244
245
246
247
# File 'lib/gcloud/storage/service.rb', line 239

def patch_file bucket_name, file_path, file_gapi = nil,
               predefined_acl: nil
  file_gapi ||= Google::Apis::StorageV1::Object.new
  service.patch_object \
    bucket_name, file_path, file_gapi,
    predefined_acl: predefined_acl
rescue Google::Apis::Error => e
  raise Gcloud::Error.from_error(e)
end

#serviceObject



53
54
55
56
# File 'lib/gcloud/storage/service.rb', line 53

def service
  return mocked_service if mocked_service
  @service
end