Class: TpCommon::FileStorage::Downloaders::Private

Inherits:
Base
  • Object
show all
Defined in:
lib/tp_common/file_storage/downloaders/private.rb

Overview

Pull and read a files from storage service. Included retry mechanism.

Constant Summary

Constants inherited from Base

Base::MAX_RETRIES

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from TpCommon::FileStorage::Base

Instance Method Details

#download(file_key) ⇒ Object

Pull a file with provided key. Return a Fog file object www.rubydoc.info/github/fog/fog-aws/Fog/Storage/AWS/File


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tp_common/file_storage/downloaders/private.rb', line 11

def download(file_key)
  retried_count = 0
  begin
    directory.files.get(mask_key(file_key)).tap do |file|
      raise FileStorage::Errors::FileNotFound.new("Could not find file: #{file_key}") unless file
    end
  rescue ::Fog::Errors::Error => error
    retried_count += 1
    retry if retried_count < MAX_RETRIES
    raise FileStorage::Errors::FailedToDownload.new("Failed to download file via fog: #{error.message}")
  end
end

#read(file_key) ⇒ Object

Same as #download but return file content


25
26
27
# File 'lib/tp_common/file_storage/downloaders/private.rb', line 25

def read(file_key)
  download(mask_key(file_key)).body
end