Class: Awshark::S3::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/awshark/s3/manager.rb

Instance Method Summary collapse

Instance Method Details

#list_bucketsObject



6
7
8
9
10
11
12
13
14
# File 'lib/awshark/s3/manager.rb', line 6

def list_buckets
  response = client.list_buckets
  response.buckets.map do |bucket|
    attributes = OpenStruct.new(bucket.to_hash)
    location = client.get_bucket_location(bucket: bucket.name)
    attributes.region = location.location_constraint
    Awshark::S3::Bucket.new(attributes)
  end
end

#list_objects(bucket:, prefix: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/awshark/s3/manager.rb', line 16

def list_objects(bucket:, prefix: nil)
  objects = []
  response = client.list_objects_v2(bucket: bucket, prefix: prefix)
  objects.concat(response.contents)

  while response.next_continuation_token
    response = client.list_objects_v2(
      bucket: bucket,
      prefix: prefix,
      continuation_token: response.next_continuation_token
    )
    objects.concat(response.contents)
  end

  objects.select { |o| o.size.positive? }
end

#update_object_metadata(bucket, key, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/awshark/s3/manager.rb', line 33

def (bucket, key, options = {})
  raise ArgumentError, 'meta=acl:STRING is missing' if options[:acl].blank?

  object = client.get_object(bucket: bucket, key: key)
   = object..merge(options[:metadata] || {})
  artifact = Artifact.new(key)

  # copy object in place to update metadata
  client.copy_object(
    acl: options[:acl] || 'private',
    bucket: bucket,
    copy_source: "/#{bucket}/#{key}",
    key: key,
    cache_control: options[:cache_control] || object.cache_control,
    content_type: artifact.content_type,
    metadata: .stringify_keys,
    metadata_directive: 'REPLACE',
    server_side_encryption: 'AES256'
  )
end