Method: PorkyLib::FileService#read

Defined in:
lib/porky_lib/file_service.rb

#read(bucket_name, file_key, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/porky_lib/file_service.rb', line 44

def read(bucket_name, file_key, options = {})
  tempfile = Tempfile.new

  begin
    object = s3.bucket(bucket_name).object(file_key)
    raise FileSizeTooLargeError, "File size is larger than maximum allowed size of #{max_file_size}" if object.content_length > max_size

    object.download_file(tempfile.path, options)
  rescue Aws::Errors::ServiceError => e
    raise FileServiceError, "Attempt to download a file from S3 failed.\n#{e.message}"
  end

  decrypt_file_contents(tempfile)
end