Sha256: 822dc3860597a153ff7e2236f3323778d0e7e85aba431d422de678c77ecad01b

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

require 'aws-sdk-s3'
require 'clarion/stores/base'
module Clarion
  module Stores
    class S3 < Base
      def initialize(region:, bucket:, prefix: nil, retry_interval: 0.1, retry_max: 10)
        @region = region
        @bucket = bucket
        @prefix = prefix

        @retry_interval = retry_interval
        @retry_max = retry_max
      end

      def store_authn(authn)
        s3.put_object(
          bucket: @bucket,
          key: authn_s3_key(authn.id),
          body: "#{authn.to_json(:all)}\n",
          content_type: 'application/json',
        )
        self
      end

      def find_authn(id)
        retry_count = 0
        json = begin
          s3.get_object(
            bucket: @bucket,
            key: authn_s3_key(id),
          ).body.read
        rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::AccessDenied
          if retry_count < retry_max
            sleep @retry_interval
            retry_count += 1
            retry
          else
            return nil
          end
        end

        Authn.new(**JSON.parse(json, symbolize_names: true))
      end

      def authn_s3_key(authn_id)
        "#{@prefix}authn/#{authn_id}"
      end

      def s3
        @s3 ||= Aws::S3::Client.new(region: @region)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
clarion-1.1.0 lib/clarion/stores/s3.rb
clarion-1.0.0 lib/clarion/stores/s3.rb
clarion-0.3.0 lib/clarion/stores/s3.rb
clarion-0.2.1 lib/clarion/stores/s3.rb
clarion-0.2.0 lib/clarion/stores/s3.rb
clarion-0.1.0 lib/clarion/stores/s3.rb