1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add a Google Cloud Storage site

This commit is contained in:
George Claghorn 2017-07-03 17:54:57 -04:00 committed by GitHub
parent a91a30260b
commit 52171ac278
5 changed files with 256 additions and 1 deletions

View file

@ -7,3 +7,4 @@ gem 'byebug'
gem 'sqlite3'
gem 'aws-sdk'
gem 'google-cloud'

View file

@ -23,6 +23,8 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.1)
public_suffix (~> 2.0, >= 2.0.2)
arel (8.0.0)
aws-sdk (2.10.7)
aws-sdk-resources (= 2.10.7)
@ -34,16 +36,159 @@ GEM
aws-sigv4 (1.0.0)
byebug (9.0.6)
concurrent-ruby (1.0.5)
declarative (0.0.9)
declarative-option (0.1.0)
digest-crc (0.4.1)
faraday (0.12.1)
multipart-post (>= 1.2, < 3)
globalid (0.4.0)
activesupport (>= 4.2.0)
google-api-client (0.13.0)
addressable (~> 2.5, >= 2.5.1)
googleauth (~> 0.5)
httpclient (>= 2.8.1, < 3.0)
mime-types (~> 3.0)
representable (~> 3.0)
retriable (>= 2.0, < 4.0)
google-cloud (0.34.0)
google-cloud-bigquery (~> 0.27.0)
google-cloud-datastore (~> 1.0)
google-cloud-dns (~> 0.25.0)
google-cloud-error_reporting (~> 0.25.0)
google-cloud-language (~> 0.26.0)
google-cloud-logging (~> 1.0)
google-cloud-monitoring (~> 0.24.0)
google-cloud-pubsub (~> 0.25.0)
google-cloud-resource_manager (~> 0.26.0)
google-cloud-spanner (~> 0.21.0)
google-cloud-speech (~> 0.24.0)
google-cloud-storage (~> 1.2)
google-cloud-trace (~> 0.25.0)
google-cloud-translate (~> 1.0)
google-cloud-video_intelligence (~> 0.20.0)
google-cloud-vision (~> 0.24.0)
google-cloud-bigquery (0.27.0)
google-api-client (~> 0.13.0)
google-cloud-core (~> 1.0)
google-cloud-core (1.0.0)
google-cloud-env (~> 1.0)
googleauth (~> 0.5.1)
google-cloud-datastore (1.0.1)
google-cloud-core (~> 1.0)
google-gax (~> 0.8.0)
google-protobuf (~> 3.2.0)
google-cloud-dns (0.25.0)
google-api-client (~> 0.13.0)
google-cloud-core (~> 1.0)
zonefile (~> 1.04)
google-cloud-env (1.0.0)
faraday (~> 0.11)
google-cloud-error_reporting (0.25.0)
google-cloud-core (~> 1.0)
google-gax (~> 0.8.0)
stackdriver-core (~> 1.1)
google-cloud-language (0.26.2)
google-cloud-core (~> 1.0)
google-gax (~> 0.8.2)
google-cloud-logging (1.1.0)
google-cloud-core (~> 1.0)
google-gax (~> 0.8.0)
stackdriver-core (~> 1.1)
google-cloud-monitoring (0.24.0)
google-gax (~> 0.8.0)
google-cloud-pubsub (0.25.0)
google-cloud-core (~> 1.0)
google-gax (~> 0.8.0)
grpc-google-iam-v1 (~> 0.6.8)
google-cloud-resource_manager (0.26.0)
google-api-client (~> 0.13.0)
google-cloud-core (~> 1.0)
google-cloud-spanner (0.21.0)
concurrent-ruby (~> 1.0)
google-cloud-core (~> 1.0)
google-gax (~> 0.8.1)
grpc (~> 1.1)
grpc-google-iam-v1 (~> 0.6.8)
google-cloud-speech (0.24.0)
google-cloud-core (~> 1.0)
google-gax (~> 0.8.2)
google-cloud-storage (1.2.0)
digest-crc (~> 0.4)
google-api-client (~> 0.13.0)
google-cloud-core (~> 1.0)
google-cloud-trace (0.25.0)
google-cloud-core (~> 1.0)
google-gax (~> 0.8.0)
stackdriver-core (~> 1.1)
google-cloud-translate (1.0.0)
google-cloud-core (~> 1.0)
googleauth (~> 0.5.1)
google-cloud-video_intelligence (0.20.0)
google-gax (~> 0.8.0)
google-cloud-vision (0.24.0)
google-cloud-core (~> 1.0)
google-gax (~> 0.8.0)
google-gax (0.8.4)
google-protobuf (~> 3.2)
googleapis-common-protos (~> 1.3.5)
googleauth (~> 0.5.1)
grpc (~> 1.0)
rly (~> 0.2.3)
google-protobuf (3.2.0.2)
googleapis-common-protos (1.3.5)
google-protobuf (~> 3.2)
grpc (~> 1.0)
googleauth (0.5.1)
faraday (~> 0.9)
jwt (~> 1.4)
logging (~> 2.0)
memoist (~> 0.12)
multi_json (~> 1.11)
os (~> 0.9)
signet (~> 0.7)
grpc (1.4.1)
google-protobuf (~> 3.1)
googleauth (~> 0.5.1)
grpc-google-iam-v1 (0.6.8)
googleapis-common-protos (~> 1.3.1)
googleauth (~> 0.5.1)
grpc (~> 1.0)
httpclient (2.8.3)
i18n (0.8.4)
jmespath (1.3.1)
jwt (1.5.6)
little-plugger (1.1.4)
logging (2.2.2)
little-plugger (~> 1.1)
multi_json (~> 1.10)
memoist (0.16.0)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
minitest (5.10.2)
multi_json (1.12.1)
multipart-post (2.0.0)
os (0.9.6)
public_suffix (2.0.5)
rake (12.0.0)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
uber (< 0.2.0)
retriable (3.0.2)
rly (0.2.3)
signet (0.7.3)
addressable (~> 2.3)
faraday (~> 0.9)
jwt (~> 1.5)
multi_json (~> 1.10)
sqlite3 (1.3.13)
stackdriver-core (1.1.0)
thread_safe (0.3.6)
tzinfo (1.2.3)
thread_safe (~> 0.1)
uber (0.1.0)
zonefile (1.06)
PLATFORMS
ruby
@ -53,6 +198,7 @@ DEPENDENCIES
aws-sdk
bundler (~> 1.15)
byebug
google-cloud
rake
sqlite3

View file

@ -41,4 +41,5 @@ module ActiveFile::Sites
end
require "active_file/sites/disk_site"
require "active_file/sites/gcs_site"
require "active_file/sites/s3_site"

View file

@ -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

64
test/gcs_site_test.rb Normal file
View file

@ -0,0 +1,64 @@
require "test_helper"
require "fileutils"
require "tmpdir"
require "active_support/core_ext/securerandom"
require "active_file/site"
if ENV["GCS_PROJECT"] && ENV["GCS_KEYFILE"] && ENV["GCS_BUCKET"]
class ActiveFile::GCSSiteTest < ActiveSupport::TestCase
FIXTURE_KEY = SecureRandom.base58(24).to_s
FIXTURE_FILE = StringIO.new("Hello world!")
setup do
@site = ActiveFile::Sites::GCSSite.new(
project: ENV["GCS_PROJECT"],
keyfile: ENV["GCS_KEYFILE"],
bucket: ENV["GCS_BUCKET"]
)
@site.upload FIXTURE_KEY, FIXTURE_FILE
FIXTURE_FILE.rewind
end
teardown do
@site.delete FIXTURE_KEY
FIXTURE_FILE.rewind
end
test "uploading" do
begin
key = SecureRandom.base58(24)
data = "Something else entirely!"
@site.upload(key, StringIO.new(data))
assert_equal data, @site.download(key)
ensure
@site.delete key
end
end
test "downloading" do
assert_equal FIXTURE_FILE.read, @site.download(FIXTURE_KEY)
end
test "existing" do
assert @site.exist?(FIXTURE_KEY)
assert_not @site.exist?(FIXTURE_KEY + "nonsense")
end
test "deleting" do
@site.delete FIXTURE_KEY
assert_not @site.exist?(FIXTURE_KEY)
end
test "sizing" do
assert_equal FIXTURE_FILE.size, @site.byte_size(FIXTURE_KEY)
end
test "checksumming" do
assert_equal Digest::MD5.hexdigest(FIXTURE_FILE.read), @site.checksum(FIXTURE_KEY)
end
end
else
puts "Skipping GCS Site tests because ENV variables are missing"
end