aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_vault/site/gcs_site.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/active_vault/site/gcs_site.rb')
-rw-r--r--lib/active_vault/site/gcs_site.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/active_vault/site/gcs_site.rb b/lib/active_vault/site/gcs_site.rb
new file mode 100644
index 0000000000..e509ebbbd2
--- /dev/null
+++ b/lib/active_vault/site/gcs_site.rb
@@ -0,0 +1,47 @@
+require "google/cloud/storage"
+
+class ActiveVault::Site::GCSSite < ActiveVault::Site
+ attr_reader :client, :bucket
+
+ def initialize(project:, keyfile:, bucket:)
+ @client = Google::Cloud::Storage.new(project: project, keyfile: keyfile)
+ @bucket = @client.bucket(bucket)
+ end
+
+ def upload(key, io)
+ bucket.create_file(io, key)
+ end
+
+ def download(key)
+ io = file_for(key).download
+ io.rewind
+ io.read
+ end
+
+ def delete(key)
+ file_for(key).try(:delete)
+ end
+
+ def exist?(key)
+ file_for(key).present?
+ end
+
+
+ def byte_size(key)
+ file_for(key).size
+ end
+
+ def checksum(key)
+ convert_to_hex base64: file_for(key).md5
+ end
+
+
+ private
+ def file_for(key)
+ bucket.file(key)
+ end
+
+ def convert_to_hex(base64:)
+ base64.unpack("m0").first.unpack("H*").first
+ end
+end