aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_file/sites/gcs_site.rb
diff options
context:
space:
mode:
authorGeorge Claghorn <george.claghorn@gmail.com>2017-07-03 17:54:57 -0400
committerGitHub <noreply@github.com>2017-07-03 17:54:57 -0400
commit52171ac2788183816cd5e8b145ab4408a7310978 (patch)
treed445fba118828d461070494de396493ded6d0f08 /lib/active_file/sites/gcs_site.rb
parenta91a30260b0d474fd1704b4cde7ee7c3bd1d9a41 (diff)
downloadrails-52171ac2788183816cd5e8b145ab4408a7310978.tar.gz
rails-52171ac2788183816cd5e8b145ab4408a7310978.tar.bz2
rails-52171ac2788183816cd5e8b145ab4408a7310978.zip
Add a Google Cloud Storage site
Diffstat (limited to 'lib/active_file/sites/gcs_site.rb')
-rw-r--r--lib/active_file/sites/gcs_site.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/active_file/sites/gcs_site.rb b/lib/active_file/sites/gcs_site.rb
new file mode 100644
index 0000000000..d9164621c2
--- /dev/null
+++ b/lib/active_file/sites/gcs_site.rb
@@ -0,0 +1,43 @@
+require "google/cloud/storage"
+
+class ActiveFile::Sites::GCSSite < ActiveFile::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, data)
+ bucket.create_file(data, 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)
+ file_for(key).md5.unpack("m0").first.unpack("H*").first
+ end
+
+
+ private
+ def file_for(key)
+ bucket.file(key)
+ end
+end