Implement GET for Cellar

This commit is contained in:
Frederic Merizen 2018-02-14 15:40:41 +01:00
parent 76c72bf4e0
commit 0e74c55d48

View file

@ -11,9 +11,22 @@ module ActiveStorage
@bucket = bucket @bucket = bucket
end 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) def delete(key)
instrument :delete, key: key do 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}")) request = Net::HTTP::Delete.new(URI::join(@endpoint, "/#{key}"))
sign(request, key) sign(request, key)
http.request(request) http.request(request)
@ -55,6 +68,10 @@ module ActiveStorage
private private
def http_start(&block)
Net::HTTP.start(@endpoint.host, @endpoint.port, use_ssl: true, &block)
end
def sign(request, key, checksum: '') def sign(request, key, checksum: '')
date = Time.now.httpdate date = Time.now.httpdate
sig = signature(method: request.method, key: key, date: date, checksum: checksum) sig = signature(method: request.method, key: key, date: date, checksum: checksum)