diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-05 13:06:29 +0200 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-05 13:06:29 +0200 |
commit | 571509ad12bf3bcb3190efd7494a38c4796302b8 (patch) | |
tree | 083bcf7f1a56e6856c486de7accef2d3778826e7 /lib/active_vault/site/gcs_site.rb | |
parent | e635dac88f0dfcc36a2313c10f860cb6e3a52cfa (diff) | |
download | rails-571509ad12bf3bcb3190efd7494a38c4796302b8.tar.gz rails-571509ad12bf3bcb3190efd7494a38c4796302b8.tar.bz2 rails-571509ad12bf3bcb3190efd7494a38c4796302b8.zip |
Rename from ActiveFile to ActiveVault since activefile gem name was taken
Diffstat (limited to 'lib/active_vault/site/gcs_site.rb')
-rw-r--r-- | lib/active_vault/site/gcs_site.rb | 47 |
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 |