lib/gcloud/storage/connection.rb in gcloud-0.10.0 vs lib/gcloud/storage/connection.rb in gcloud-0.11.0
- old
+ new
@@ -17,10 +17,11 @@
require "gcloud/version"
require "gcloud/backoff"
require "gcloud/upload"
require "google/api_client"
require "mime/types"
+require "digest/sha2"
module Gcloud
module Storage
##
# @private Represents the connection to Storage,
@@ -50,20 +51,20 @@
params = { project: @project }
params["prefix"] = options[:prefix] if options[:prefix]
params["pageToken"] = options[:token] if options[:token]
params["maxResults"] = options[:max] if options[:max]
- @client.execute(
+ execute(
api_method: @storage.buckets.list,
parameters: params
)
end
##
# Retrieves bucket by name.
def get_bucket bucket_name
- @client.execute(
+ execute(
api_method: @storage.buckets.get,
parameters: { bucket: bucket_name }
)
end
@@ -72,96 +73,92 @@
def insert_bucket bucket_name, options = {}
params = { project: @project, predefinedAcl: options[:acl],
predefinedDefaultObjectAcl: options[:default_acl]
}.delete_if { |_, v| v.nil? }
- incremental_backoff options do
- @client.execute(
- api_method: @storage.buckets.insert,
- parameters: params,
- body_object: insert_bucket_request(bucket_name, options)
- )
- end
+ execute(
+ api_method: @storage.buckets.insert,
+ parameters: params,
+ body_object: insert_bucket_request(bucket_name, options)
+ )
end
##
# Updates a bucket, including its ACL metadata.
def patch_bucket bucket_name, options = {}
params = { bucket: bucket_name,
predefinedAcl: options[:predefined_acl],
predefinedDefaultObjectAcl: options[:predefined_default_acl]
}.delete_if { |_, v| v.nil? }
- @client.execute(
+ execute(
api_method: @storage.buckets.patch,
parameters: params,
body_object: patch_bucket_request(options)
)
end
##
# Permanently deletes an empty bucket.
- def delete_bucket bucket_name, opts = {}
- incremental_backoff opts do
- @client.execute(
- api_method: @storage.buckets.delete,
- parameters: { bucket: bucket_name }
- )
- end
+ def delete_bucket bucket_name
+ execute(
+ api_method: @storage.buckets.delete,
+ parameters: { bucket: bucket_name }
+ )
end
##
# Retrieves a list of ACLs for the given bucket.
def list_bucket_acls bucket_name
- @client.execute(
+ execute(
api_method: @storage.bucket_access_controls.list,
parameters: { bucket: bucket_name }
)
end
##
# Creates a new bucket ACL.
def insert_bucket_acl bucket_name, entity, role
- @client.execute(
+ execute(
api_method: @storage.bucket_access_controls.insert,
parameters: { bucket: bucket_name },
body_object: { entity: entity, role: role }
)
end
##
# Permanently deletes a bucket ACL.
def delete_bucket_acl bucket_name, entity
- @client.execute(
+ execute(
api_method: @storage.bucket_access_controls.delete,
parameters: { bucket: bucket_name, entity: entity }
)
end
##
# Retrieves a list of default ACLs for the given bucket.
def list_default_acls bucket_name
- @client.execute(
+ execute(
api_method: @storage.default_object_access_controls.list,
parameters: { bucket: bucket_name }
)
end
##
# Creates a new default ACL.
def insert_default_acl bucket_name, entity, role
- @client.execute(
+ execute(
api_method: @storage.default_object_access_controls.insert,
parameters: { bucket: bucket_name },
body_object: { entity: entity, role: role }
)
end
##
# Permanently deletes a default ACL.
def delete_default_acl bucket_name, entity
- @client.execute(
+ execute(
api_method: @storage.default_object_access_controls.delete,
parameters: { bucket: bucket_name, entity: entity }
)
end
@@ -175,11 +172,11 @@
pageToken: options[:token],
maxResults: options[:max],
versions: options[:versions]
}.delete_if { |_, v| v.nil? }
- @client.execute(
+ execute(
api_method: @storage.objects.list,
parameters: params
)
end
@@ -196,81 +193,90 @@
media = file_media local_path, options, resumable
upload_path = Pathname(path || local_path).to_path
result = insert_file resumable, bucket_name, upload_path, media, options
return result unless resumable
upload = result.resumable_upload
- result = @client.execute upload while upload.resumable?
+ result = execute upload while upload.resumable?
result
end
##
# Retrieves an object or its metadata.
def get_file bucket_name, file_path, options = {}
query = { bucket: bucket_name, object: file_path }
query[:generation] = options[:generation] if options[:generation]
- @client.execute(
+ request_options = {
api_method: @storage.objects.get,
parameters: query
- )
+ }
+
+ request_options = add_headers request_options, options
+ execute request_options
end
## Copy a file from source bucket/object to a
# destination bucket/object.
def copy_file source_bucket_name, source_file_path,
destination_bucket_name, destination_file_path, options = {}
- @client.execute(
+ request_options = {
api_method: @storage.objects.copy,
parameters: { sourceBucket: source_bucket_name,
sourceObject: source_file_path,
sourceGeneration: options[:generation],
destinationBucket: destination_bucket_name,
destinationObject: destination_file_path,
predefinedAcl: options[:acl]
- }.delete_if { |_, v| v.nil? })
+ }.delete_if { |_, v| v.nil? }
+ }
+ request_options = add_headers request_options, options
+ execute request_options
end
##
# Download contents of a file.
- def download_file bucket_name, file_path
- @client.execute(
+
+ def download_file bucket_name, file_path, options = {}
+ request_options = {
api_method: @storage.objects.get,
parameters: { bucket: bucket_name,
object: file_path,
alt: :media }
- )
+ }
+ request_options = add_headers request_options, options
+ execute request_options
end
##
# Updates a file's metadata.
def patch_file bucket_name, file_path, options = {}
params = { bucket: bucket_name,
object: file_path,
predefinedAcl: options[:predefined_acl]
}.delete_if { |_, v| v.nil? }
- @client.execute(
+ execute(
api_method: @storage.objects.patch,
parameters: params,
body_object: patch_file_request(options)
)
end
##
# Permanently deletes a file.
def delete_file bucket_name, file_path
- @client.execute(
+ execute(
api_method: @storage.objects.delete,
parameters: { bucket: bucket_name,
object: file_path }
)
end
##
# Retrieves a list of ACLs for the given file.
def list_file_acls bucket_name, file_name
- @client.execute(
+ execute(
api_method: @storage.object_access_controls.list,
parameters: { bucket: bucket_name, object: file_name }
)
end
@@ -278,11 +284,11 @@
# Creates a new file ACL.
def insert_file_acl bucket_name, file_name, entity, role, options = {}
query = { bucket: bucket_name, object: file_name }
query[:generation] = options[:generation] if options[:generation]
- @client.execute(
+ execute(
api_method: @storage.object_access_controls.insert,
parameters: query,
body_object: { entity: entity, role: role }
)
end
@@ -291,11 +297,11 @@
# Permanently deletes a file ACL.
def delete_file_acl bucket_name, file_name, entity, options = {}
query = { bucket: bucket_name, object: file_name, entity: entity }
query[:generation] = options[:generation] if options[:generation]
- @client.execute(
+ execute(
api_method: @storage.object_access_controls.delete,
parameters: query
)
end
@@ -372,14 +378,18 @@
bucket: bucket_name,
name: path,
predefinedAcl: options[:acl]
}.delete_if { |_, v| v.nil? }
- @client.execute api_method: @storage.objects.insert,
- media: media,
- parameters: params,
- body_object: insert_file_request(options)
+ request_options = {
+ api_method: @storage.objects.insert,
+ media: media,
+ parameters: params,
+ body_object: insert_file_request(options)
+ }
+ request_options = add_headers request_options, options
+ execute request_options
end
def file_media local_path, options, resumable
media = Google::APIClient::UploadIO.new local_path,
options[:content_type]
@@ -408,12 +418,26 @@
"metadata" => options[:metadata],
"acl" => options[:acl]
}.delete_if { |_, v| v.nil? }
end
- def incremental_backoff options = {}
- Gcloud::Backoff.new(options).execute_gapi do
- yield
+ def add_headers request_options, options
+ headers = (request_options[:headers] ||= {})
+ if options[:encryption_key] || options[:encryption_key_sha256]
+ headers["x-goog-encryption-algorithm"] = "AES256"
+ end
+ if (key = options.delete(:encryption_key))
+ headers["x-goog-encryption-key"] = Base64.encode64 key
+ end
+ if (key_sha256 = options.delete(:encryption_key_sha256))
+ headers["x-goog-encryption-key-sha256"] = Base64.encode64 key_sha256
+ end
+ request_options
+ end
+
+ def execute options
+ Gcloud::Backoff.new.execute_gapi do
+ @client.execute options
end
end
end
end
end