aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/active_storage/service/gcs_service.rb11
-rw-r--r--test/direct_uploads_controller_test.rb34
-rw-r--r--test/service/gcs_service_test.rb20
3 files changed, 63 insertions, 2 deletions
diff --git a/lib/active_storage/service/gcs_service.rb b/lib/active_storage/service/gcs_service.rb
index b0ad3e2fa4..7053a130c0 100644
--- a/lib/active_storage/service/gcs_service.rb
+++ b/lib/active_storage/service/gcs_service.rb
@@ -53,6 +53,17 @@ class ActiveStorage::Service::GCSService < ActiveStorage::Service
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
def file_for(key)
bucket.file(key)
diff --git a/test/direct_uploads_controller_test.rb b/test/direct_uploads_controller_test.rb
index bed985148e..f96a37f758 100644
--- a/test/direct_uploads_controller_test.rb
+++ b/test/direct_uploads_controller_test.rb
@@ -7,7 +7,7 @@ require "action_controller/test_case"
require "active_storage/direct_uploads_controller"
if SERVICE_CONFIGURATIONS[:s3]
- class ActiveStorage::DirectUploadsControllerTest < ActionController::TestCase
+ class ActiveStorage::S3DirectUploadsControllerTest < ActionController::TestCase
setup do
@blob = create_blob
@routes = Routes
@@ -32,5 +32,35 @@ if SERVICE_CONFIGURATIONS[:s3]
end
end
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
diff --git a/test/service/gcs_service_test.rb b/test/service/gcs_service_test.rb
index 7d4700498b..3d70080af4 100644
--- a/test/service/gcs_service_test.rb
+++ b/test/service/gcs_service_test.rb
@@ -1,4 +1,5 @@
require "service/shared_service_tests"
+require "httparty"
if SERVICE_CONFIGURATIONS[:gcs]
class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase
@@ -6,6 +7,25 @@ if SERVICE_CONFIGURATIONS[:gcs]
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
travel_to Time.now do
url = SERVICE.bucket.signed_url(FIXTURE_KEY, expires: 120) +