Class: Gcloud::Storage::Connection
- Inherits:
-
Object
- Object
- Gcloud::Storage::Connection
- Defined in:
- lib/gcloud/storage/connection.rb
Overview
as well as expose the API calls.
Constant Summary collapse
- API_VERSION =
"v1"
Instance Attribute Summary collapse
- #credentials ⇒ Object
-
#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) ⇒ 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, options = {}) ⇒ 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.
- #inspect ⇒ Object
-
#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, including its ACL metadata.
-
#patch_file(bucket_name, file_path, options = {}) ⇒ Object
Updates a file’s metadata.
-
#upload_file(resumable, bucket_name, file, path = nil, options = {}) ⇒ Object
Stores a new object and metadata.
Constructor Details
#initialize(project, credentials) ⇒ Connection
Creates a new Connection instance.
39 40 41 42 43 44 45 46 |
# File 'lib/gcloud/storage/connection.rb', line 39 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
35 36 37 |
# File 'lib/gcloud/storage/connection.rb', line 35 def credentials @credentials end |
#project ⇒ Object
Returns the value of attribute project.
32 33 34 |
# File 'lib/gcloud/storage/connection.rb', line 32 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.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/gcloud/storage/connection.rb', line 219 def copy_file source_bucket_name, source_file_path, destination_bucket_name, destination_file_path, = {} = { api_method: @storage.objects.copy, parameters: { sourceBucket: source_bucket_name, sourceObject: source_file_path, sourceGeneration: [:generation], destinationBucket: destination_bucket_name, destinationObject: destination_file_path, predefinedAcl: [:acl] }.delete_if { |_, v| v.nil? } } = add_headers , execute end |
#delete_bucket(bucket_name) ⇒ Object
Permanently deletes an empty bucket.
102 103 104 105 106 107 |
# File 'lib/gcloud/storage/connection.rb', line 102 def delete_bucket bucket_name execute( api_method: @storage.buckets.delete, parameters: { bucket: bucket_name } ) end |
#delete_bucket_acl(bucket_name, entity) ⇒ Object
Permanently deletes a bucket ACL.
130 131 132 133 134 135 |
# File 'lib/gcloud/storage/connection.rb', line 130 def delete_bucket_acl bucket_name, entity execute( api_method: @storage.bucket_access_controls.delete, parameters: { bucket: bucket_name, entity: entity } ) end |
#delete_default_acl(bucket_name, entity) ⇒ Object
Permanently deletes a default ACL.
158 159 160 161 162 163 |
# File 'lib/gcloud/storage/connection.rb', line 158 def delete_default_acl bucket_name, entity execute( api_method: @storage.default_object_access_controls.delete, parameters: { bucket: bucket_name, entity: entity } ) end |
#delete_file(bucket_name, file_path) ⇒ Object
Permanently deletes a file.
266 267 268 269 270 271 272 |
# File 'lib/gcloud/storage/connection.rb', line 266 def delete_file bucket_name, file_path execute( api_method: @storage.objects.delete, parameters: { bucket: bucket_name, object: file_path } ) end |
#delete_file_acl(bucket_name, file_name, entity, options = {}) ⇒ Object
Permanently deletes a file ACL.
298 299 300 301 302 303 304 305 306 |
# File 'lib/gcloud/storage/connection.rb', line 298 def delete_file_acl bucket_name, file_name, entity, = {} query = { bucket: bucket_name, object: file_name, entity: entity } query[:generation] = [:generation] if [:generation] execute( api_method: @storage.object_access_controls.delete, parameters: query ) end |
#download_file(bucket_name, file_path, options = {}) ⇒ Object
Download contents of a file.
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/gcloud/storage/connection.rb', line 238 def download_file bucket_name, file_path, = {} = { api_method: @storage.objects.get, parameters: { bucket: bucket_name, object: file_path, alt: :media } } = add_headers , execute end |
#get_bucket(bucket_name) ⇒ Object
Retrieves bucket by name.
64 65 66 67 68 69 |
# File 'lib/gcloud/storage/connection.rb', line 64 def get_bucket bucket_name 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.
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/gcloud/storage/connection.rb', line 204 def get_file bucket_name, file_path, = {} query = { bucket: bucket_name, object: file_path } query[:generation] = [:generation] if [:generation] = { api_method: @storage.objects.get, parameters: query } = add_headers , execute end |
#insert_bucket(bucket_name, options = {}) ⇒ Object
Creates a new bucket.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gcloud/storage/connection.rb', line 73 def insert_bucket bucket_name, = {} params = { project: @project, predefinedAcl: [:acl], predefinedDefaultObjectAcl: [:default_acl] }.delete_if { |_, v| v.nil? } execute( api_method: @storage.buckets.insert, parameters: params, body_object: insert_bucket_request(bucket_name, ) ) end |
#insert_bucket_acl(bucket_name, entity, role) ⇒ Object
Creates a new bucket ACL.
120 121 122 123 124 125 126 |
# File 'lib/gcloud/storage/connection.rb', line 120 def insert_bucket_acl bucket_name, entity, role 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.
148 149 150 151 152 153 154 |
# File 'lib/gcloud/storage/connection.rb', line 148 def insert_default_acl bucket_name, entity, role 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.
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/gcloud/storage/connection.rb', line 285 def insert_file_acl bucket_name, file_name, entity, role, = {} query = { bucket: bucket_name, object: file_name } query[:generation] = [:generation] if [:generation] execute( api_method: @storage.object_access_controls.insert, parameters: query, body_object: { entity: entity, role: role } ) end |
#inspect ⇒ Object
316 317 318 |
# File 'lib/gcloud/storage/connection.rb', line 316 def inspect "#{self.class}(#{@project})" end |
#list_bucket_acls(bucket_name) ⇒ Object
Retrieves a list of ACLs for the given bucket.
111 112 113 114 115 116 |
# File 'lib/gcloud/storage/connection.rb', line 111 def list_bucket_acls bucket_name 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.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gcloud/storage/connection.rb', line 50 def list_buckets = {} params = { project: @project } params["prefix"] = [:prefix] if [:prefix] params["pageToken"] = [:token] if [:token] params["maxResults"] = [:max] if [:max] 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.
139 140 141 142 143 144 |
# File 'lib/gcloud/storage/connection.rb', line 139 def list_default_acls bucket_name 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.
276 277 278 279 280 281 |
# File 'lib/gcloud/storage/connection.rb', line 276 def list_file_acls bucket_name, file_name 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.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/gcloud/storage/connection.rb', line 167 def list_files bucket_name, = {} params = { bucket: bucket_name, prefix: [:prefix], delimiter: [:delimiter], pageToken: [:token], maxResults: [:max], versions: [:versions] }.delete_if { |_, v| v.nil? } 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.
311 312 313 |
# File 'lib/gcloud/storage/connection.rb', line 311 def mime_type_for path MIME::Types.of(path).first.to_s end |
#patch_bucket(bucket_name, options = {}) ⇒ Object
Updates a bucket, including its ACL metadata.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/gcloud/storage/connection.rb', line 87 def patch_bucket bucket_name, = {} params = { bucket: bucket_name, predefinedAcl: [:predefined_acl], predefinedDefaultObjectAcl: [:predefined_default_acl] }.delete_if { |_, v| v.nil? } execute( api_method: @storage.buckets.patch, parameters: params, body_object: patch_bucket_request() ) end |
#patch_file(bucket_name, file_path, options = {}) ⇒ Object
Updates a file’s metadata.
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/gcloud/storage/connection.rb', line 251 def patch_file bucket_name, file_path, = {} params = { bucket: bucket_name, object: file_path, predefinedAcl: [:predefined_acl] }.delete_if { |_, v| v.nil? } execute( api_method: @storage.objects.patch, parameters: params, body_object: patch_file_request() ) end |
#upload_file(resumable, bucket_name, file, path = nil, options = {}) ⇒ Object
Stores a new object and metadata. If resumable is true, a resumable upload, otherwise uses a multipart form post.
UploadIO comes from Faraday, which gets it from multipart-post The initializer signature is: filename_or_io, content_type, filename = nil, opts = {}
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/gcloud/storage/connection.rb', line 190 def upload_file resumable, bucket_name, file, path = nil, = {} local_path = Pathname(file).to_path [:content_type] ||= mime_type_for(local_path) media = file_media local_path, , resumable upload_path = Pathname(path || local_path).to_path result = insert_file resumable, bucket_name, upload_path, media, return result unless resumable upload = result.resumable_upload result = execute upload while upload.resumable? result end |