Module: FetchAndProcess::S3

Defined in:
lib/fetch_and_process/s3.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/fetch_and_process/s3.rb', line 7

def self.included(base)
  base.add_handler('s3', :s3_handler)
end

Instance Method Details

#s3_handlerObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fetch_and_process/s3.rb', line 11

def s3_handler
  # TODO: Get the bucket and key from the URI
  options = {
    bucket: uri.host,
    key: uri.path[1..],
  }
  options[:if_modified_since] = File.mtime(cache_location) if File.exist?(cache_location)
  s3.get_object(options, target: cache_location)
rescue Aws::S3::Errors::NotModified
  options.delete(:if_modified_since)
  s3.head_object(options)
rescue Aws::S3::Errors::AccessDenied => e
  raise FetchAndProcess::AccessControlError, "Could not fetch file from S3: #{e.message}"
rescue Aws::S3::Errors::NoSuchKey => e
  raise FetchAndProcess::FileNotFoundError, "Could not find file on S3: #{e.message}"
end