Class: PatientHttp::PayloadStore::S3Store

Inherits:
Base
  • Object
show all
Defined in:
lib/patient_http/payload_store/s3_store.rb

Overview

S3-based payload store for production deployments.

Stores payloads as JSON objects in S3. This store is recommended for production environments where payloads need durable storage and can be shared across multiple processes/instances.

Thread-safe: S3 clients handle their own thread safety.

Examples:

Configuration with S3 bucket

s3 = Aws::S3::Resource.new
bucket = s3.bucket("my-payloads-bucket")
config.register_payload_store(:s3, adapter: :s3, bucket: bucket)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

create, #generate_key, lookup, register, registered_adapters, #store

Constructor Details

#initialize(bucket:, key_prefix: nil) ⇒ S3Store

Initialize a new S3 store.

Parameters:

  • bucket (Aws::S3::Bucket)

    S3 Bucket object. Required.

  • key_prefix (String) (defaults to: nil)

    Prefix for all S3 object keys. Defaults to “patient_http/payloads/”

Raises:

  • (ArgumentError)

    If bucket is not provided



35
36
37
38
39
40
# File 'lib/patient_http/payload_store/s3_store.rb', line 35

def initialize(bucket:, key_prefix: nil)
  raise ArgumentError, "S3 bucket is required" unless bucket

  @bucket = bucket
  @key_prefix = key_prefix || "patient_http/payloads/"
end

Instance Attribute Details

#key_prefixString (readonly)

Returns The key prefix used for all stored payloads.

Returns:

  • (String)

    The key prefix used for all stored payloads



27
28
29
# File 'lib/patient_http/payload_store/s3_store.rb', line 27

def key_prefix
  @key_prefix
end

Instance Method Details

#delete(key) ⇒ Boolean

Delete a payload from S3.

Idempotent - does not raise if object doesn’t exist.

Parameters:

  • key (String)

    The key to delete

Returns:

  • (Boolean)

    true



72
73
74
75
76
# File 'lib/patient_http/payload_store/s3_store.rb', line 72

def delete(key)
  full_key = key_with_prefix(key)
  @bucket.object(full_key).delete
  true
end

#exists?(key) ⇒ Boolean

Check if a payload exists.

Parameters:

  • key (String)

    The key to check

Returns:

  • (Boolean)

    true if the payload exists



82
83
84
85
# File 'lib/patient_http/payload_store/s3_store.rb', line 82

def exists?(key)
  full_key = key_with_prefix(key)
  @bucket.object(full_key).exists?
end

#fetch(key) ⇒ Hash?

Fetch data from S3.

Parameters:

  • key (String)

    The key to fetch

Returns:

  • (Hash, nil)

    The stored data or nil if not found



57
58
59
60
61
62
63
64
# File 'lib/patient_http/payload_store/s3_store.rb', line 57

def fetch(key)
  full_key = key_with_prefix(key)
  response = @bucket.object(full_key).get

  JSON.parse(response.body.read)
rescue Aws::S3::Errors::NoSuchKey
  nil
end

#store_json(key, json) ⇒ String

Store pre-serialized JSON string directly in S3.

Parameters:

  • key (String)

    Unique key (appended to key_prefix)

  • json (String)

    Pre-serialized JSON string

Returns:

  • (String)

    The key



47
48
49
50
51
# File 'lib/patient_http/payload_store/s3_store.rb', line 47

def store_json(key, json)
  full_key = key_with_prefix(key)
  @bucket.object(full_key).put(body: json, content_type: "application/json")
  key
end