Class: Google::Cloud::Storage::Service
- Inherits:
-
Object
- Object
- Google::Cloud::Storage::Service
- 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
- #credentials ⇒ Object
-
#mocked_service ⇒ Object
Returns the value of attribute mocked_service.
- #project ⇒ Object
Instance Method Summary collapse
-
#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.
-
#delete_bucket(bucket_name) ⇒ Object
Permanently deletes an empty bucket.
-
#delete_bucket_acl(bucket_name, entity) ⇒ Object
Permanently deletes a bucket ACL.
-
#delete_default_acl(bucket_name, entity) ⇒ Object
Permanently deletes a default ACL.
-
#delete_file(bucket_name, file_path) ⇒ Object
Permanently deletes a file.
-
#delete_file_acl(bucket_name, file_name, entity, options = {}) ⇒ Object
Permanently deletes a file ACL.
-
#download_file(bucket_name, file_path, target_path, generation: nil, key: nil, key_sha256: nil) ⇒ Object
Download contents of a file.
-
#get_bucket(bucket_name) ⇒ Object
Retrieves bucket by name.
-
#get_file(bucket_name, file_path, generation: nil, key: nil, key_sha256: nil) ⇒ Object
Retrieves an object or its metadata.
-
#initialize(project, credentials, retries: nil, timeout: nil) ⇒ Service
constructor
Creates a new Service instance.
-
#insert_bucket(bucket_gapi, options = {}) ⇒ Object
Creates a new bucket.
-
#insert_bucket_acl(bucket_name, entity, role) ⇒ Object
Creates a new bucket ACL.
-
#insert_default_acl(bucket_name, entity, role) ⇒ Object
Creates a new default ACL.
-
#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.
-
#insert_file_acl(bucket_name, file_name, entity, role, options = {}) ⇒ Object
Creates a new file ACL.
- #inspect ⇒ Object
-
#list_bucket_acls(bucket_name) ⇒ Object
Retrieves a list of ACLs for the given bucket.
-
#list_buckets(prefix: nil, token: nil, max: nil) ⇒ Object
Retrieves a list of buckets for the given project.
-
#list_default_acls(bucket_name) ⇒ Object
Retrieves a list of default ACLs for the given bucket.
-
#list_file_acls(bucket_name, file_name) ⇒ Object
Retrieves a list of ACLs for the given file.
-
#list_files(bucket_name, options = {}) ⇒ Object
Retrieves a list of files matching the criteria.
-
#mime_type_for(path) ⇒ Object
Retrieves the mime-type for a file path.
-
#patch_bucket(bucket_name, bucket_gapi = nil, predefined_acl: nil, predefined_default_acl: nil) ⇒ Object
Updates a bucket, including its ACL metadata.
-
#patch_file(bucket_name, file_path, file_gapi = nil, predefined_acl: nil) ⇒ Object
Updates a file’s metadata.
- #service ⇒ Object
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..application_name = "google-cloud-storage" @service..application_version = \ Google::Cloud::Storage::VERSION @service..retries = retries || 3 @service..timeout_sec = timeout @service..open_timeout_sec = timeout @service. = @credentials.client end |
Instance Attribute Details
#credentials ⇒ Object
38 39 40 |
# File 'lib/google/cloud/storage/service.rb', line 38 def credentials @credentials end |
#mocked_service ⇒ Object
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 |
#project ⇒ Object
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, = {} execute do service.copy_object \ source_bucket_name, source_file_path, destination_bucket_name, destination_file_path, destination_predefined_acl: [:acl], source_generation: [:generation], options: (key: [:key], key_sha256: [: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, = {} execute do service.delete_object_access_control \ bucket_name, file_name, entity, generation: [: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: 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: 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, = {} execute do service.insert_bucket \ @project, bucket_gapi, predefined_acl: [:acl], predefined_default_object_acl: [: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: 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, = {} 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: [:generation] end end |
#inspect ⇒ Object
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, = {} execute do service.list_objects \ bucket_name, delimiter: [:delimiter], max_results: [:max], page_token: [:token], prefix: [:prefix], versions: [: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 |
#service ⇒ Object
56 57 58 59 |
# File 'lib/google/cloud/storage/service.rb', line 56 def service return mocked_service if mocked_service @service end |