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

Add direct upload support to GCS service

This commit is contained in:
Michael Herold 2017-07-17 08:17:09 -05:00 committed by George Claghorn
parent 94a450acbe
commit be526d16fe
3 changed files with 63 additions and 2 deletions

View file

@ -53,6 +53,17 @@ class ActiveStorage::Service::GCSService < ActiveStorage::Service
end end
end end
def url_for_direct_upload(key, expires_in:, content_type:, content_length:)
instrument :url, key do |payload|
generated_url = bucket.signed_url key, method: "PUT", expires: expires_in,
content_type: content_type
payload[:url] = generated_url
generated_url
end
end
private private
def file_for(key) def file_for(key)
bucket.file(key) bucket.file(key)

View file

@ -7,7 +7,7 @@ require "action_controller/test_case"
require "active_storage/direct_uploads_controller" require "active_storage/direct_uploads_controller"
if SERVICE_CONFIGURATIONS[:s3] if SERVICE_CONFIGURATIONS[:s3]
class ActiveStorage::DirectUploadsControllerTest < ActionController::TestCase class ActiveStorage::S3DirectUploadsControllerTest < ActionController::TestCase
setup do setup do
@blob = create_blob @blob = create_blob
@routes = Routes @routes = Routes
@ -32,5 +32,35 @@ if SERVICE_CONFIGURATIONS[:s3]
end end
end end
else else
puts "Skipping Direct Upload tests because no S3 configuration was supplied" puts "Skipping S3 Direct Upload tests because no S3 configuration was supplied"
end
if SERVICE_CONFIGURATIONS[:gcs]
class ActiveStorage::GCSDirectUploadsControllerTest < ActionController::TestCase
setup do
@blob = create_blob
@routes = Routes
@controller = ActiveStorage::DirectUploadsController.new
@config = SERVICE_CONFIGURATIONS[:gcs]
@old_service = ActiveStorage::Blob.service
ActiveStorage::Blob.service = ActiveStorage::Service.configure(:gcs, SERVICE_CONFIGURATIONS)
end
teardown do
ActiveStorage::Blob.service = @old_service
end
test "creating new direct upload" do
post :create, params: { blob: {
filename: "hello.txt", byte_size: 6, checksum: Digest::MD5.base64digest("Hello"), content_type: "text/plain" } }
details = JSON.parse(@response.body)
assert_match %r{storage\.googleapis\.com/#{@config[:bucket]}}, details["url"]
assert_equal "hello.txt", GlobalID::Locator.locate_signed(details["sgid"]).filename.to_s
end
end
else
puts "Skipping GCS Direct Upload tests because no GCS configuration was supplied"
end end

View file

@ -1,4 +1,5 @@
require "service/shared_service_tests" require "service/shared_service_tests"
require "httparty"
if SERVICE_CONFIGURATIONS[:gcs] if SERVICE_CONFIGURATIONS[:gcs]
class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase
@ -6,6 +7,25 @@ if SERVICE_CONFIGURATIONS[:gcs]
include ActiveStorage::Service::SharedServiceTests include ActiveStorage::Service::SharedServiceTests
test "direct upload" do
begin
key = SecureRandom.base58(24)
data = "Something else entirely!"
direct_upload_url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size)
HTTParty.put(
direct_upload_url,
body: data,
headers: { "Content-Type" => "text/plain" },
debug_output: STDOUT
)
assert_equal data, @service.download(key)
ensure
@service.delete key
end
end
test "signed URL generation" do test "signed URL generation" do
travel_to Time.now do travel_to Time.now do
url = SERVICE.bucket.signed_url(FIXTURE_KEY, expires: 120) + url = SERVICE.bucket.signed_url(FIXTURE_KEY, expires: 120) +