Class: BucketStore::KeyStorage::KeyStreamer

Inherits:
Object
  • Object
show all
Defined in:
lib/bucket_store/key_storage.rb

Overview

Defines a streaming interface for download and upload operations.

Note that individual adapters may require additional configuration for the correct behavior of the streaming interface.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:, adapter_type:, bucket:, key:) ⇒ KeyStreamer

Returns a new instance of KeyStreamer.



25
26
27
28
29
30
# File 'lib/bucket_store/key_storage.rb', line 25

def initialize(adapter:, adapter_type:, bucket:, key:)
  @adapter = adapter
  @adapter_type = adapter_type
  @bucket = bucket
  @key = key
end

Instance Attribute Details

#adapter_typeObject (readonly)

Returns the value of attribute adapter_type.



23
24
25
# File 'lib/bucket_store/key_storage.rb', line 23

def adapter_type
  @adapter_type
end

#bucketObject (readonly)

Returns the value of attribute bucket.



23
24
25
# File 'lib/bucket_store/key_storage.rb', line 23

def bucket
  @bucket
end

#keyObject (readonly)

Returns the value of attribute key.



23
24
25
# File 'lib/bucket_store/key_storage.rb', line 23

def key
  @key
end

Instance Method Details

#download(file:) ⇒ Object

Streams the content of the reference key into a file-like object

Examples:

Download a key

buffer = StringIO.new
BucketStore.for("inmemory://bucket/file.xml").stream.download(file: buffer)
buffer.string == "Imagine I'm a 2GB file"

Parameters:

  • file (IO)

    a writeable IO instance, or a file-like object such as ‘StringIO`

Returns:

  • hash containing the bucket, the key and file like object passed in as input

See Also:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bucket_store/key_storage.rb', line 41

def download(file:)
  BucketStore.logger.info(event: "key_storage.download_started")

  start = BucketStore::Timing.monotonic_now
  adapter.download(
    bucket: bucket,
    key: key,
    file: file,
  )

  BucketStore.logger.info(event: "key_storage.download_finished",
                          duration: BucketStore::Timing.monotonic_now - start)

  {
    bucket: bucket,
    key: key,
    file: file,
  }
end

#upload!(file:) ⇒ Object

Performs a streaming upload to the backing object store

Examples:

Upload a key

buffer = StringIO.new("Imagine I'm a 2GB file")
BucketStore.for("inmemory://bucket/file.xml").stream.upload!(file: buffer)

Parameters:

  • file (IO)

    a readable IO instance, or a file-like object such as ‘StringIO`

Returns:

  • the generated key for the new object

Raises:

  • (ArgumentError)

See Also:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bucket_store/key_storage.rb', line 69

def upload!(file:)
  raise ArgumentError, "Key cannot be empty" if key.empty?

  BucketStore.logger.info(event: "key_storage.upload_started",
                          **log_context)

  start = BucketStore::Timing.monotonic_now
  adapter.upload!(
    bucket: bucket,
    key: key,
    file: file,
  )

  BucketStore.logger.info(event: "key_storage.upload_finished",
                          duration: BucketStore::Timing.monotonic_now - start,
                          **log_context)

  "#{adapter_type}://#{bucket}/#{key}"
end