提交 be526d16 编写于 作者: M Michael Herold 提交者: George Claghorn

Add direct upload support to GCS service

上级 94a450ac
......@@ -53,6 +53,17 @@ def url(key, expires_in:, disposition:, filename:)
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)
......
......@@ -7,7 +7,7 @@
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 @@ class ActiveStorage::DirectUploadsControllerTest < ActionController::TestCase
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
require "service/shared_service_tests"
require "httparty"
if SERVICE_CONFIGURATIONS[:gcs]
class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase
......@@ -6,6 +7,25 @@ class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase
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) +
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册