Sha256: c2e8a10ff035ab6ba63713487594b269a0cdc77168741034db30b48444318ece

Contents?: true

Size: 810 Bytes

Versions: 10

Compression:

Stored size: 810 Bytes

Contents

require 'aws-sdk'

module Backupsss
  # A class for listing and sorting files in an s3 bucket
  class BackupBucket
    attr_reader :dir, :region

    def initialize(opts = {})
      @dir    = opts[:dir]
      @region = opts[:region]
    end

    def ls
      list_objects.map(&:key)
    end

    def ls_t
      list_objects.sort_by(&:last_modified).map(&:key)
    end

    def ls_rt
      ls_t.reverse
    end

    def rm(file)
      s3_client.delete_object(bucket: bucket, key: file)
      file
    end

    private

    def list_objects
      s3_client.list_objects(bucket: bucket, prefix: prefix).contents
    end

    def s3_client
      Aws::S3::Client.new(region: region)
    end

    def bucket
      dir.split('/').first
    end

    def prefix
      dir.split('/').drop(1).join('/')
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
backupsss-0.5.0 lib/backupsss/backup_bucket.rb
backupsss-0.4.1 lib/backupsss/backup_bucket.rb
backupsss-0.4.0 lib/backupsss/backup_bucket.rb
backupsss-0.3.2 lib/backupsss/backup_bucket.rb
backupsss-0.3.1 lib/backupsss/backup_bucket.rb
backupsss-0.3.0 lib/backupsss/backup_bucket.rb
backupsss-0.2.0 lib/backupsss/backup_bucket.rb
backupsss-0.1.3 lib/backupsss/backup_bucket.rb
backupsss-0.1.1 lib/backupsss/backup_bucket.rb
backupsss-0.1.0 lib/backupsss/backup_bucket.rb