class Stax::Cmd::S3

Public Instance Methods

buckets() click to toggle source
# File lib/stax/mixin/s3.rb, line 43
def buckets
  puts stack_s3_bucket_names
end
clear() click to toggle source
# File lib/stax/mixin/s3.rb, line 115
def clear
  debug("Clearing buckets for #{my.stack_name}")
  (options[:names] || stack_s3_bucket_names).each do |b|
    if yes?("Clear contents of bucket #{b}?", :yellow)
      ::Aws::S3::Bucket.new(b).clear!
    end
  end
end
delete() click to toggle source
# File lib/stax/mixin/s3.rb, line 126
def delete
  debug("Deleting buckets for #{my.stack_name}")
  (options[:names] || stack_s3_bucket_names).each do |b|
    if yes?("Delete bucket and contents #{b}?", :yellow)
      ::Aws::S3::Bucket.new(b).delete!
    end
  end
end
expire(days = 1) click to toggle source
# File lib/stax/mixin/s3.rb, line 90
def expire(days = 1)
  debug("Expiring objects in buckets tagged by #{my.stack_name}")
  stack_tagged_buckets.each do |bucket|
    if yes?("Expire all objects for #{bucket.name} in #{days}d?", :yellow)
      Aws::S3.put_lifecycle(
        bucket.name,
        rules: [
          {
            prefix: '',   # required, all objects
            status: :Enabled,
            expiration: {
              days: days,
            },
            noncurrent_version_expiration: {
              noncurrent_days: days,
            },
          }
        ]
      )
    end
  end
end
lifecycle() click to toggle source
# File lib/stax/mixin/s3.rb, line 80
def lifecycle
  debug("Lifecycle for buckets tagged by #{my.stack_name}")
  stack_tagged_buckets.each do |bucket|
    Aws::S3.get_lifecycle(bucket.name).each do |l|
      puts YAML.dump(stringify_keys(l.to_hash))
    end
  end
end
ls() click to toggle source
# File lib/stax/mixin/s3.rb, line 35
def ls
  debug("Buckets for #{my.stack_name}")
  print_table stack_s3_bucket_names.map { |b|
    [ b, Aws::S3.location(b) ]
  }
end
reap() click to toggle source
# File lib/stax/mixin/s3.rb, line 70
def reap
  debug("Deleting buckets tagged by #{my.stack_name}")
  stack_tagged_buckets.each do |b|
    if yes?("Delete bucket and contents #{b.name}?", :yellow)
      ::Aws::S3::Bucket.new(b.name).delete!
    end
  end
end
stack_s3_bucket_names() click to toggle source
# File lib/stax/mixin/s3.rb, line 20
def stack_s3_bucket_names
  stack_s3_buckets.map(&:physical_resource_id).compact
end
stack_s3_buckets() click to toggle source
# File lib/stax/mixin/s3.rb, line 16
def stack_s3_buckets
  Aws::Cfn.resources_by_type(my.stack_name, 'AWS::S3::Bucket')
end
stack_tagged_buckets() click to toggle source
# File lib/stax/mixin/s3.rb, line 24
def stack_tagged_buckets
  Aws::S3.list_buckets.select do |bucket|
    region = Aws::S3.bucket_region(bucket.name)
    next unless region.empty? || region == ENV['AWS_REGION']
    tags = Aws::S3.bucket_tags(bucket.name)
    tags.any? { |t| t.key == 'aws:cloudformation:stack-name' && t.value == my.stack_name }
  end
end
tagged() click to toggle source
# File lib/stax/mixin/s3.rb, line 62
def tagged
  debug("Buckets tagged by stack #{my.stack_name}")
  print_table stack_tagged_buckets.map { |b|
    [b.name, b.creation_date]
  }
end
website() click to toggle source
# File lib/stax/mixin/s3.rb, line 48
def website
  stack_s3_bucket_names.each do |b|
    debug("Website endpoint for #{b}")
    begin
      Aws::S3.client.get_bucket_website(bucket: b)
      region = Aws::S3.location(b)
      puts "#{b}.s3-website-#{region}.amazonaws.com"
    rescue ::Aws::S3::Errors::NoSuchWebsiteConfiguration => e
      puts e.message      # handle no website config
    end
  end
end