Class: AionS3::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/aion_s3/uploader.rb

Overview

An Uploader is a utility for uploading data to s3. The Uploader supports compliance locking, where files are locked for update/deletion for a configured amount of time.

The utility is a wrapper around Aws::S3::Client.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, lock_days: 0, s3_options: {}) ⇒ Uploader

Returns a new instance of Uploader.

Parameters:

  • bucket (String)
  • lock_days (Integer) (defaults to: 0)
  • s3_options (Hash) (defaults to: {})


17
18
19
20
21
# File 'lib/aion_s3/uploader.rb', line 17

def initialize(bucket, lock_days: 0, s3_options: {})
  @client = Aws::S3::Client.new(s3_options)
  @bucket = bucket
  @lock_seconds = lock_days.to_i * 24 * 60 * 60
end

Instance Attribute Details

#clientAws::S3::Client (readonly)

Returns:

  • (Aws::S3::Client)


12
13
14
# File 'lib/aion_s3/uploader.rb', line 12

def client
  @client
end

Instance Method Details

#get(key, target = nil) ⇒ Aws::S3::Types::GetObjectOutput

Gets metadata from an object in s3.

If a target is provided, the object data will be downloaded as well. target must be a string with file path or an IO object.

Parameters:

  • key (String)
  • target (String, IO) (defaults to: nil)

Returns:

  • (Aws::S3::Types::GetObjectOutput)


31
32
33
34
35
36
37
# File 'lib/aion_s3/uploader.rb', line 31

def get(key, target = nil)
  @client.get_object(
      bucket: @bucket,
      key: key,
      response_target: target
  )
end

#put(key, body) ⇒ Aws::S3::Types::PutObjectOutput

Puts an object to s3.

If Uploader was configured with lock_days > 0 a compliance lock will be put on the object.

Parameters:

  • key (String)

    an urlsafe string

  • body (String, IO)

Returns:

  • (Aws::S3::Types::PutObjectOutput)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aion_s3/uploader.rb', line 46

def put(key, body)
  options = {
      bucket: @bucket,
      key: key,
      body: body,
  }

  if @lock_seconds > 0
    options.reverse_merge!(
        object_lock_mode: 'COMPLIANCE',
        object_lock_retain_until_date: Time.now + @lock_seconds
    )
  end

  @client.put_object(options)
end