Sha256: 0c8c7066da397a8dd738b3ba959b7ad5483423b37a89b40dd1e300c0c3bf7737

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require "aws-sdk"

# Thin interface to all things Amazon S3
module Caseflow
  class S3Service
    def self.store_file(filename, content_or_filepath, type = :content)
      init!

      content = (type == :content) ? content_or_filepath : File.open(content_or_filepath, "rb")

      @bucket.put_object(acl: "private",
                         key: filename,
                         body: content,
                         server_side_encryption: "AES256")
    end

    def self.fetch_file(filename, dest_filepath)
      init!

      @client.get_object(
        response_target: dest_filepath,
        bucket: bucket_name,
        key: filename
      )
    end
    
    def self.fetch_content(filename)
      init!

      @client.get_object(
        bucket: bucket_name,
        key: filename
      ).body.read
    rescue Aws::S3::Errors::NoSuchKey
      nil
    end

    private 

    def self.init!
      return if @bucket

      Aws.config.update(region: "us-gov-west-1")

      @client = Aws::S3::Client.new
      @resource = Aws::S3::Resource.new(client: @client)
      @bucket = @resource.bucket(bucket_name)
    end

    def self.bucket_name
      Rails.application.config.s3_bucket_name
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
caseflow-0.1.5 app/services/caseflow/s3_service.rb