diff --git a/lib/active_storage/service/cellar_service.rb b/lib/active_storage/service/cellar_service.rb index 7cb2fb185..de8f569a6 100644 --- a/lib/active_storage/service/cellar_service.rb +++ b/lib/active_storage/service/cellar_service.rb @@ -11,9 +11,22 @@ module ActiveStorage @bucket = bucket end + def download(key) + instrument :download, key: key do + http_start do |http| + request = Net::HTTP::Get.new(URI::join(@endpoint, "/#{key}")) + sign(request, key) + response = http.request(request) + if response.is_a?(Net::HTTPSuccess) + response.body + end + end + end + end + def delete(key) instrument :delete, key: key do - Net::HTTP.start(@endpoint.host, @endpoint.port, use_ssl: true) do |http| + http_start do |http| request = Net::HTTP::Delete.new(URI::join(@endpoint, "/#{key}")) sign(request, key) http.request(request) @@ -55,6 +68,10 @@ module ActiveStorage private + def http_start(&block) + Net::HTTP.start(@endpoint.host, @endpoint.port, use_ssl: true, &block) + end + def sign(request, key, checksum: '') date = Time.now.httpdate sig = signature(method: request.method, key: key, date: date, checksum: checksum)