class BucketClient::GCPClient
Public Class Methods
new(project_id, secret)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 8 def initialize(project_id, secret) @client = Google::Cloud::Storage.new(project_id: project_id, keyfile: secret) end
Public Instance Methods
create_bucket(key)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 30 def create_bucket(key) begin @client.create_bucket key bucket = get_bucket! key OperationResult.new(true, "OK", bucket, 200) rescue StandardError => e OperationResult.new(false, e.message, nil, 400) end end
delete_blob(uri)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 140 def delete_blob(uri) data = break_uri uri begin bucket = @client.bucket data[:bucket] bucket.file(data[:blob], skip_lookup: true).delete OperationResult.new(true, "Deleted Blob", nil, 204) rescue StandardError => e OperationResult.new(false, e.message, nil, 400) end end
delete_blob_if_exist(uri)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 131 def delete_blob_if_exist(uri) exist = exist_blob uri if exist delete_blob uri else OperationResult.new(true, "Blob already deleted", nil, 204) end end
delete_bucket(key)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 40 def delete_bucket(key) exist = exist_bucket key if exist bucket = @client.bucket(key) success = bucket.delete message = success ? "Deleted" : "Failed to delete" code = success ? 204 : 400 OperationResult.new(success, message, nil, code) else OperationResult.new(false, "Bucket does not exist", nil, 404) end end
delete_bucket_if_exist(key)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 53 def delete_bucket_if_exist(key) exist = exist_bucket key if exist delete_bucket key else OperationResult.new(true, "Bucket already deleted", nil, 200) end end
exist_blob(uri)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 100 def exist_blob(uri) data = break_uri uri bucket = @client.bucket data[:bucket] bucket.file(data[:blob], skip_lookup: true).exists? end
exist_bucket(key)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 12 def exist_bucket(key) !@client.bucket(key).nil? end
get_blob(uri)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 88 def get_blob(uri) data = break_uri uri begin bucket = @client.bucket data[:bucket] blob = bucket.file data[:blob] bin = blob.download.read OperationResult.new(true, "OK", bin, 200) rescue StandardError => e OperationResult.new(false, e.message, nil, 400) end end
get_bucket!(key)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 16 def get_bucket!(key) GCPBucket.new(self, key) end
put_blob(payload, uri)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 106 def put_blob(payload, uri) mime = MimeMagic.by_magic payload data = break_uri uri begin # @type [Google::Cloud::Storage::Bucket] bucket = @client.bucket data[:bucket] f = StringIO.new f << payload type = mime&.type || "text/plain" file = bucket.create_file f, data[:blob], cache_control: "no-cache", content_type: type OperationResult.new(true, "OK", file.public_url, 200) rescue StandardError => e OperationResult.new(false, e.message, nil, 400) end end
put_bucket(key)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 20 def put_bucket(key) exist = exist_bucket key if exist bucket = get_bucket! key OperationResult.new(true, "OK", bucket, 200) else create_bucket key end end
set_get_cors(key, cors)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 76 def set_get_cors(key, cors) begin bucket = @client.bucket key bucket.cors(&:clear) bucket.cors {|rules| rules.add_rule cors, "GET"} OperationResult.new(true, "OK", nil, 200) rescue StandardError => e OperationResult.new(false, e.message, nil, 400) end end
set_read_policy(key, access)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 62 def set_read_policy(key, access) raise ArgumentError.new("Read Policy not accepted") if access != :public && access != :private begin bucket = @client.bucket key modify_acl bucket, access bucket.files.each do |file| modify_acl file, access end OperationResult.new(true, "OK", nil, 200) rescue StandardError => e OperationResult.new(false, e.message, nil, 400) end end
update_blob(payload, uri)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 122 def update_blob(payload, uri) exist = exist_blob uri if exist put_blob payload, uri else OperationResult.new(false, "Blob does not exist", nil, 404) end end
Private Instance Methods
break_uri(uri)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 153 def break_uri(uri) url = Addressable::URI.parse uri fragments = url.path.split("/").select {|x| !x.nil? && !x.empty?} bucket = fragments[0] blob = fragments.drop(1).join "/" {bucket: bucket, blob: blob} end
modify_acl(principal, access)
click to toggle source
# File lib/bucket_client/gcp/gcp_client.rb, line 161 def modify_acl(principal, access) if access == :private principal.acl.private! principal.default_acl.private! else principal.acl.public! principal.default_acl.public! end end