Class: Lara::S3Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lara/s3_client.rb

Instance Method Summary collapse

Instance Method Details

#download(url:) ⇒ Object

Raises:



45
46
47
48
49
50
51
# File 'lib/lara/s3_client.rb', line 45

def download(url:)
  conn = Faraday.new(url: url) { |f| f.adapter Faraday.default_adapter }
  response = conn.get
  raise Lara::LaraError, "S3 download failed: HTTP #{response.status}" unless response.success?

  response.body
end

#upload(url:, fields:, io:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lara/s3_client.rb', line 8

def upload(url:, fields:, io:)
  payload_io = io.is_a?(String) ? File.open(io, "rb") : io

  begin
    filename = if io.is_a?(String)
                 File.basename(io)
               else
                 io.respond_to?(:path) ? File.basename(io.path) : "upload.bin"
               end

    conn = Faraday.new(url: url) do |f|
      f.request(:multipart)
      f.request(:url_encoded)
      f.adapter(Faraday.default_adapter)
    end

    file_part = Faraday::Multipart::FilePart.new(payload_io, "application/octet-stream",
                                                 filename)

    response = conn.post(nil) do |req|
      req.body = fields.transform_values(&:to_s).merge("file" => file_part)
    end

    raise Lara::LaraError, "S3 upload failed: HTTP #{response.status}" unless response.success?

    nil
  ensure
    payload_io.close if io.is_a?(String) && payload_io && !payload_io.closed?
  end
rescue ArgumentError => e
  warn "[S3Client] upload ArgumentError: #{e.message}"
  raise
rescue Faraday::Error => e
  warn "[S3Client] upload Faraday error: #{e.class} #{e.message}"
  raise Lara::LaraError, "S3 upload failed: #{e.message}"
end