Class: Gcloud::Storage::Connection
- Inherits:
-
Object
- Object
- Gcloud::Storage::Connection
- Defined in:
- lib/gcloud/storage/connection.rb
Overview
Represents the connection to Storage, as well as expose the API calls.
Constant Summary collapse
- API_VERSION =
:nodoc:
"v1"
Instance Attribute Summary collapse
-
#credentials ⇒ Object
:nodoc:.
-
#project ⇒ Object
Returns the value of attribute project.
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, opts = {}) ⇒ Object
Permenently deletes an empty bucket.
-
#delete_bucket_acl(bucket_name, entity) ⇒ Object
Permenently deletes a bucket ACL.
-
#delete_default_acl(bucket_name, entity) ⇒ Object
Permenently deletes a default ACL.
-
#delete_file(bucket_name, file_path) ⇒ Object
Permenently deletes a file.
-
#delete_file_acl(bucket_name, file_name, entity, options = {}) ⇒ Object
Permenently deletes a file ACL.
-
#download_file(bucket_name, file_path) ⇒ Object
Download contents of a file.
-
#get_bucket(bucket_name) ⇒ Object
Retrieves bucket by name.
-
#get_file(bucket_name, file_path, options = {}) ⇒ Object
Retrieves an object or its metadata.
-
#initialize(project, credentials) ⇒ Connection
constructor
Creates a new Connection instance.
-
#insert_bucket(bucket_name, 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_acl(bucket_name, file_name, entity, role, options = {}) ⇒ Object
Creates a new file ACL.
-
#insert_file_multipart(bucket_name, file, path = nil, options = {}) ⇒ Object
Stores a new object and metadata.
-
#insert_file_resumable(bucket_name, file, path = nil, chunk_size = nil, options = {}) ⇒ Object
Stores a new object and metadata.
-
#list_bucket_acls(bucket_name) ⇒ Object
Retrieves a list of ACLs for the given bucket.
-
#list_buckets(options = {}) ⇒ 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, options = {}) ⇒ Object
Updates a bucket’s metadata.
-
#patch_file(bucket_name, file_path, options = {}) ⇒ Object
Updates a file’s metadata.
Constructor Details
#initialize(project, credentials) ⇒ Connection
Creates a new Connection instance.
35 36 37 38 39 40 41 42 |
# File 'lib/gcloud/storage/connection.rb', line 35 def initialize project, credentials @project = project @credentials = credentials @client = Google::APIClient.new application_name: "gcloud-ruby", application_version: Gcloud::VERSION @client. = @credentials.client @storage = @client.discovered_api "storage", API_VERSION end |
Instance Attribute Details
#credentials ⇒ Object
:nodoc:
31 32 33 |
# File 'lib/gcloud/storage/connection.rb', line 31 def credentials @credentials end |
#project ⇒ Object
Returns the value of attribute project.
30 31 32 |
# File 'lib/gcloud/storage/connection.rb', line 30 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.
261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/gcloud/storage/connection.rb', line 261 def copy_file source_bucket_name, source_file_path, destination_bucket_name, destination_file_path, = {} @client.execute( api_method: @storage.objects.copy, parameters: { sourceBucket: source_bucket_name, sourceObject: source_file_path, destinationBucket: destination_bucket_name, destinationObject: destination_file_path, predefinedAcl: [:acl] }.delete_if { |_, v| v.nil? }) end |
#delete_bucket(bucket_name, opts = {}) ⇒ Object
Permenently deletes an empty bucket.
99 100 101 102 103 104 105 106 |
# File 'lib/gcloud/storage/connection.rb', line 99 def delete_bucket bucket_name, opts = {} incremental_backoff opts do @client.execute( api_method: @storage.buckets.delete, parameters: { bucket: bucket_name } ) end end |
#delete_bucket_acl(bucket_name, entity) ⇒ Object
Permenently deletes a bucket ACL.
129 130 131 132 133 134 |
# File 'lib/gcloud/storage/connection.rb', line 129 def delete_bucket_acl bucket_name, entity @client.execute( api_method: @storage.bucket_access_controls.delete, parameters: { bucket: bucket_name, entity: entity } ) end |
#delete_default_acl(bucket_name, entity) ⇒ Object
Permenently deletes a default ACL.
157 158 159 160 161 162 |
# File 'lib/gcloud/storage/connection.rb', line 157 def delete_default_acl bucket_name, entity @client.execute( api_method: @storage.default_object_access_controls.delete, parameters: { bucket: bucket_name, entity: entity } ) end |
#delete_file(bucket_name, file_path) ⇒ Object
Permenently deletes a file.
301 302 303 304 305 306 307 |
# File 'lib/gcloud/storage/connection.rb', line 301 def delete_file bucket_name, file_path @client.execute( api_method: @storage.objects.delete, parameters: { bucket: bucket_name, object: file_path } ) end |
#delete_file_acl(bucket_name, file_name, entity, options = {}) ⇒ Object
Permenently deletes a file ACL.
333 334 335 336 337 338 339 340 341 |
# File 'lib/gcloud/storage/connection.rb', line 333 def delete_file_acl bucket_name, file_name, entity, = {} query = { bucket: bucket_name, object: file_name, entity: entity } query[:generation] = [:generation] if [:generation] @client.execute( api_method: @storage.object_access_controls.delete, parameters: query ) end |
#download_file(bucket_name, file_path) ⇒ Object
Download contents of a file.
276 277 278 279 280 281 282 283 |
# File 'lib/gcloud/storage/connection.rb', line 276 def download_file bucket_name, file_path @client.execute( api_method: @storage.objects.get, parameters: { bucket: bucket_name, object: file_path, alt: :media } ) end |
#get_bucket(bucket_name) ⇒ Object
Retrieves bucket by name.
60 61 62 63 64 65 |
# File 'lib/gcloud/storage/connection.rb', line 60 def get_bucket bucket_name @client.execute( api_method: @storage.buckets.get, parameters: { bucket: bucket_name } ) end |
#get_file(bucket_name, file_path, options = {}) ⇒ Object
Retrieves an object or its metadata.
249 250 251 252 253 254 255 256 257 |
# File 'lib/gcloud/storage/connection.rb', line 249 def get_file bucket_name, file_path, = {} query = { bucket: bucket_name, object: file_path } query[:generation] = [:generation] if [:generation] @client.execute( api_method: @storage.objects.get, parameters: query ) end |
#insert_bucket(bucket_name, options = {}) ⇒ Object
Creates a new bucket.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/gcloud/storage/connection.rb', line 69 def insert_bucket bucket_name, = {} params = { project: @project, predefinedAcl: [:acl], predefinedDefaultObjectAcl: [:default_acl] }.delete_if { |_, v| v.nil? } incremental_backoff do @client.execute( api_method: @storage.buckets.insert, parameters: params, body_object: { name: bucket_name } ) end end |
#insert_bucket_acl(bucket_name, entity, role) ⇒ Object
Creates a new bucket ACL.
119 120 121 122 123 124 125 |
# File 'lib/gcloud/storage/connection.rb', line 119 def insert_bucket_acl bucket_name, entity, role @client.execute( api_method: @storage.bucket_access_controls.insert, parameters: { bucket: bucket_name }, body_object: { entity: entity, role: role } ) end |
#insert_default_acl(bucket_name, entity, role) ⇒ Object
Creates a new default ACL.
147 148 149 150 151 152 153 |
# File 'lib/gcloud/storage/connection.rb', line 147 def insert_default_acl bucket_name, entity, role @client.execute( api_method: @storage.default_object_access_controls.insert, parameters: { bucket: bucket_name }, body_object: { entity: entity, role: role } ) end |
#insert_file_acl(bucket_name, file_name, entity, role, options = {}) ⇒ Object
Creates a new file ACL.
320 321 322 323 324 325 326 327 328 329 |
# File 'lib/gcloud/storage/connection.rb', line 320 def insert_file_acl bucket_name, file_name, entity, role, = {} query = { bucket: bucket_name, object: file_name } query[:generation] = [:generation] if [:generation] @client.execute( api_method: @storage.object_access_controls.insert, parameters: query, body_object: { entity: entity, role: role } ) end |
#insert_file_multipart(bucket_name, file, path = nil, options = {}) ⇒ Object
Stores a new object and metadata. Uses a multipart form post.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/gcloud/storage/connection.rb', line 188 def insert_file_multipart bucket_name, file, path = nil, = {} local_path = Pathname(file).to_path upload_path = Pathname(path || local_path).to_path mime_type = mime_type_for local_path media = Google::APIClient::UploadIO.new local_path, mime_type params = { uploadType: "multipart", bucket: bucket_name, name: upload_path, predefinedAcl: [:acl] }.delete_if { |_, v| v.nil? } @client.execute( api_method: @storage.objects.insert, media: media, parameters: params, body_object: { contentType: mime_type } ) end |
#insert_file_resumable(bucket_name, file, path = nil, chunk_size = nil, options = {}) ⇒ Object
Stores a new object and metadata. Uses a resumable upload.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/gcloud/storage/connection.rb', line 213 def insert_file_resumable bucket_name, file, path = nil, chunk_size = nil, = {} local_path = Pathname(file).to_path upload_path = Pathname(path || local_path).to_path # mime_type = options[:mime_type] || mime_type_for local_path mime_type = mime_type_for local_path # This comes from Faraday, which gets it from multipart-post # The signature is: # filename_or_io, content_type, filename = nil, opts = {} media = Google::APIClient::UploadIO.new local_path, mime_type media.chunk_size = chunk_size params = { uploadType: "resumable", bucket: bucket_name, name: upload_path, predefinedAcl: [:acl] }.delete_if { |_, v| v.nil? } result = @client.execute( api_method: @storage.objects.insert, media: media, parameters: params, body_object: { contentType: mime_type } ) upload = result.resumable_upload result = @client.execute upload while upload.resumable? result end |
#list_bucket_acls(bucket_name) ⇒ Object
Retrieves a list of ACLs for the given bucket.
110 111 112 113 114 115 |
# File 'lib/gcloud/storage/connection.rb', line 110 def list_bucket_acls bucket_name @client.execute( api_method: @storage.bucket_access_controls.list, parameters: { bucket: bucket_name } ) end |
#list_buckets(options = {}) ⇒ Object
Retrieves a list of buckets for the given project.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gcloud/storage/connection.rb', line 46 def list_buckets = {} params = { project: @project } params["prefix"] = [:prefix] if [:prefix] params["pageToken"] = [:token] if [:token] params["maxResults"] = [:max] if [:max] @client.execute( api_method: @storage.buckets.list, parameters: params ) end |
#list_default_acls(bucket_name) ⇒ Object
Retrieves a list of default ACLs for the given bucket.
138 139 140 141 142 143 |
# File 'lib/gcloud/storage/connection.rb', line 138 def list_default_acls bucket_name @client.execute( api_method: @storage.default_object_access_controls.list, parameters: { bucket: bucket_name } ) end |
#list_file_acls(bucket_name, file_name) ⇒ Object
Retrieves a list of ACLs for the given file.
311 312 313 314 315 316 |
# File 'lib/gcloud/storage/connection.rb', line 311 def list_file_acls bucket_name, file_name @client.execute( api_method: @storage.object_access_controls.list, parameters: { bucket: bucket_name, object: file_name } ) end |
#list_files(bucket_name, options = {}) ⇒ Object
Retrieves a list of files matching the criteria.
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/gcloud/storage/connection.rb', line 166 def list_files bucket_name, = {} params = { bucket: bucket_name, prefix: [:prefix], pageToken: [:token], maxResults: [:max], versions: [:versions] }.delete_if { |_, v| v.nil? } @client.execute( api_method: @storage.objects.list, parameters: params ) 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.
346 347 348 |
# File 'lib/gcloud/storage/connection.rb', line 346 def mime_type_for path MIME::Types.of(path).first.to_s end |
#patch_bucket(bucket_name, options = {}) ⇒ Object
Updates a bucket’s metadata.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/gcloud/storage/connection.rb', line 85 def patch_bucket bucket_name, = {} params = { bucket: bucket_name, predefinedAcl: [:acl], predefinedDefaultObjectAcl: .delete(:default_acl) }.delete_if { |_, v| v.nil? } @client.execute( api_method: @storage.buckets.patch, parameters: params ) end |
#patch_file(bucket_name, file_path, options = {}) ⇒ Object
Updates a file’s metadata.
287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/gcloud/storage/connection.rb', line 287 def patch_file bucket_name, file_path, = {} params = { bucket: bucket_name, object: file_path, predefinedAcl: .delete(:acl) }.delete_if { |_, v| v.nil? } @client.execute( api_method: @storage.objects.patch, parameters: params ) end |