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.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/google/cloud/storage/service.rb', line 42

def initialize project, credentials, retries: nil, timeout: nil
  @project = project
  @credentials = credentials
  @credentials = credentials
  @service = API::StorageService.new
  @service.client_options.application_name    = "google-cloud-storage"
  @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



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

def credentials
  @credentials
end

#mocked_serviceObject

Returns the value of attribute mocked_service.



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

def mocked_service
  @mocked_service
end

#projectObject



35
36
37
# File 'lib/google/cloud/storage/service.rb', line 35

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.



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

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.



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

def delete_bucket bucket_name
  execute { service.delete_bucket bucket_name }
end

#delete_bucket_acl(bucket_name, entity) ⇒ Object

Permanently deletes a bucket ACL.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



81
82
83
84
85
86
87
88
# File 'lib/google/cloud/storage/service.rb', line 81

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.



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

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.



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

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



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

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.



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

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



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

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

#list_bucket_acls(bucket_name) ⇒ Object

Retrieves a list of ACLs for the given bucket.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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



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

def service
  return mocked_service if mocked_service
  @service
end