Class: Google::Cloud::Storage::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/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
52
53
# File 'lib/google/cloud/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 = \
    Google::Cloud::Storage::VERSION
  @service.request_options.retries = retries || 3
  @service.request_options.timeout_sec      = timeout
  @service.request_options.open_timeout_sec = timeout
  @service.authorization = @credentials.client
end

Instance Attribute Details

#credentialsObject



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

def credentials
  @credentials
end

#mocked_serviceObject

Returns the value of attribute mocked_service.



59
60
61
# File 'lib/google/cloud/storage/service.rb', line 59

def mocked_service
  @mocked_service
end

#projectObject



34
35
36
# File 'lib/google/cloud/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.



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/google/cloud/storage/service.rb', line 204

def copy_file source_bucket_name, source_file_path,
              destination_bucket_name, destination_file_path,
              options = {}
  execute do
    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])
  end
end

#delete_bucket(bucket_name) ⇒ Object

Permanently deletes an empty bucket.



107
108
109
# File 'lib/google/cloud/storage/service.rb', line 107

def delete_bucket bucket_name
  execute { service.delete_bucket bucket_name }
end

#delete_bucket_acl(bucket_name, entity) ⇒ Object

Permanently deletes a bucket ACL.



127
128
129
# File 'lib/google/cloud/storage/service.rb', line 127

def delete_bucket_acl bucket_name, entity
  execute { service.delete_bucket_access_control bucket_name, entity }
end

#delete_default_acl(bucket_name, entity) ⇒ Object

Permanently deletes a default ACL.



149
150
151
152
153
# File 'lib/google/cloud/storage/service.rb', line 149

def delete_default_acl bucket_name, entity
  execute do
    service.delete_default_object_access_control bucket_name, entity
  end
end

#delete_file(bucket_name, file_path) ⇒ Object

Permanently deletes a file.



244
245
246
# File 'lib/google/cloud/storage/service.rb', line 244

def delete_file bucket_name, file_path
  execute { service.delete_object bucket_name, file_path }
end

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

Permanently deletes a file ACL.



267
268
269
270
271
272
# File 'lib/google/cloud/storage/service.rb', line 267

def delete_file_acl bucket_name, file_name, entity, options = {}
  execute do
    service.delete_object_access_control \
      bucket_name, file_name, entity, generation: options[:generation]
  end
end

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

Download contents of a file.



220
221
222
223
224
225
226
227
228
# File 'lib/google/cloud/storage/service.rb', line 220

def download_file bucket_name, file_path, target_path, generation: nil,
                  key: nil, key_sha256: nil
  execute do
    service.get_object \
      bucket_name, file_path,
      download_dest: target_path, generation: generation,
      options: key_options(key: key, key_sha256: key_sha256)
  end
end

#get_bucket(bucket_name) ⇒ Object

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



73
74
75
# File 'lib/google/cloud/storage/service.rb', line 73

def get_bucket bucket_name
  execute { service.get_bucket bucket_name }
end

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

Retrieves an object or its metadata.



192
193
194
195
196
197
198
199
200
# File 'lib/google/cloud/storage/service.rb', line 192

def get_file bucket_name, file_path, generation: nil, key: nil,
             key_sha256: nil
  execute do
    service.get_object \
      bucket_name, file_path,
      generation: generation,
      options: key_options(key: key, key_sha256: key_sha256)
  end
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/google/cloud/storage/service.rb', line 80

def insert_bucket bucket_gapi, options = {}
  execute do
    service.insert_bucket \
      @project, bucket_gapi,
      predefined_acl: options[:acl],
      predefined_default_object_acl: options[:default_acl]
  end
end

#insert_bucket_acl(bucket_name, entity, role) ⇒ Object

Creates a new bucket ACL.



119
120
121
122
123
# File 'lib/google/cloud/storage/service.rb', line 119

def insert_bucket_acl bucket_name, entity, role
  new_acl = Google::Apis::StorageV1::BucketAccessControl.new \
    entity: entity, role: role
  execute { service.insert_bucket_access_control bucket_name, new_acl }
end

#insert_default_acl(bucket_name, entity, role) ⇒ Object

Creates a new default ACL.



139
140
141
142
143
144
145
# File 'lib/google/cloud/storage/service.rb', line 139

def insert_default_acl bucket_name, entity, role
  new_acl = Google::Apis::StorageV1::ObjectAccessControl.new \
    entity: entity, role: role
  execute do
    service.insert_default_object_access_control bucket_name, new_acl
  end
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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/google/cloud/storage/service.rb', line 170

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)
  execute do
    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)
  end
end

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

Creates a new file ACL.



256
257
258
259
260
261
262
263
# File 'lib/google/cloud/storage/service.rb', line 256

def insert_file_acl bucket_name, file_name, entity, role, options = {}
  new_acl = Google::Apis::StorageV1::ObjectAccessControl.new \
    entity: entity, role: role
  execute do
    service.insert_object_access_control \
      bucket_name, file_name, new_acl, generation: options[:generation]
  end
end

#inspectObject



282
283
284
# File 'lib/google/cloud/storage/service.rb', line 282

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

#list_bucket_acls(bucket_name) ⇒ Object

Retrieves a list of ACLs for the given bucket.



113
114
115
# File 'lib/google/cloud/storage/service.rb', line 113

def list_bucket_acls bucket_name
  execute { service.list_bucket_access_controls bucket_name }
end

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

Retrieves a list of buckets for the given project.



63
64
65
66
67
68
# File 'lib/google/cloud/storage/service.rb', line 63

def list_buckets prefix: nil, token: nil, max: nil
  execute do
    service.list_buckets @project, prefix: prefix, page_token: token,
                                   max_results: max
  end
end

#list_default_acls(bucket_name) ⇒ Object

Retrieves a list of default ACLs for the given bucket.



133
134
135
# File 'lib/google/cloud/storage/service.rb', line 133

def list_default_acls bucket_name
  execute { service.list_default_object_access_controls bucket_name }
end

#list_file_acls(bucket_name, file_name) ⇒ Object

Retrieves a list of ACLs for the given file.



250
251
252
# File 'lib/google/cloud/storage/service.rb', line 250

def list_file_acls bucket_name, file_name
  execute { service.list_object_access_controls bucket_name, file_name }
end

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

Retrieves a list of files matching the criteria.



157
158
159
160
161
162
163
164
165
166
# File 'lib/google/cloud/storage/service.rb', line 157

def list_files bucket_name, options = {}
  execute do
    service.list_objects \
      bucket_name, delimiter: options[:delimiter],
                   max_results: options[:max],
                   page_token: options[:token],
                   prefix: options[:prefix],
                   versions: options[:versions]
  end
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.



277
278
279
# File 'lib/google/cloud/storage/service.rb', line 277

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/google/cloud/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

  execute do
    service.patch_bucket \
      bucket_name, bucket_gapi,
      predefined_acl: predefined_acl,
      predefined_default_object_acl: predefined_default_acl
  end
end

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

Updates a file’s metadata.



232
233
234
235
236
237
238
239
240
# File 'lib/google/cloud/storage/service.rb', line 232

def patch_file bucket_name, file_path, file_gapi = nil,
               predefined_acl: nil
  file_gapi ||= Google::Apis::StorageV1::Object.new
  execute do
    service.patch_object \
      bucket_name, file_path, file_gapi,
      predefined_acl: predefined_acl
  end
end

#serviceObject



55
56
57
58
# File 'lib/google/cloud/storage/service.rb', line 55

def service
  return mocked_service if mocked_service
  @service
end