Sha256: fe4b167815b6ad76ecd1f1e546035ae6cea29f63de760d577bd2220e5812b030

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require "kvmultiplex/provider"

require "json"
require "aws-sdk-s3"

module KVMultiplex
  module Providers
    class S3 < KVMultiplex::Provider
      def initialize(bucket_name:, key_prefix:, resource_client: nil)
        raise "bucket_name must be a String." unless bucket_name.is_a?(String)
        raise "key_prefix must be a String." unless key_prefix.is_a?(String)

        resource_client ||= Aws::S3::Resource.new

        raise "resource_client must be an Aws::S3::Resource." unless client.is_a?(Aws::S3::Resource)

        @bucket_name = bucket_name
        @bucket = resource_client.bucket(@bucket_name)
        raise "bucket '#{bucket_name}' doesn't exist." unless @bucket.exists?

        @key_prefix = key_prefix
        @resource_client = resource_client
      end

      def get(subkey, _full_key)
        object = locate_object(subkey)
        object.exists? ? JSON.parse(object.get.body.read) : nil
      end

      def set(subkey, _full_key, value)
        locate_object(subkey).put(body: JSON.generate(value))
      end

      private

      def locate_object(subkey)
        @bucket.object(subkey.join("/"))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kvmultiplex-s3-0.1.3 lib/kvmultiplex/providers/s3.rb