class ServerBackups::S3

Constants

PROVIDER

Attributes

config[R]
logger[R]

Public Class Methods

new(config) click to toggle source
# File lib/server_backups/s3.rb, line 9
def initialize(config)
    @config = config
    @logger = config.logger
end

Public Instance Methods

bucket() click to toggle source
# File lib/server_backups/s3.rb, line 23
def bucket
    @bucket ||= Aws::S3::Bucket.new(config.bucket, client: client)
end
client() click to toggle source
# File lib/server_backups/s3.rb, line 14
def client
    @client ||= begin
        Aws.config[:credentials] = Aws::Credentials.new(
            config.access_key_id, config.secret_access_key
        )
        Aws::S3::Client.new region: config.region
    end
end
delete_files_not_newer_than(key, age) click to toggle source
# File lib/server_backups/s3.rb, line 31
def delete_files_not_newer_than(key, age)
    bucket.objects(prefix: key).each do |file|
        destroy key, true unless file.last_modified.to_datetime > age
    end
end
destroy(key, existence_known = false) click to toggle source
# File lib/server_backups/s3.rb, line 43
def destroy(key, existence_known = false)
    return unless existence_known || exists?(key)
    client.delete_object bucket: config.bucket, key: key
end
exists?(path) click to toggle source
# File lib/server_backups/s3.rb, line 37
def exists?(path)
    logger.debug "Exists? #{config.bucket}  #{path}"
    !bucket.objects(prefix: path).to_a.empty?
    # !!client.head_object(bucket: config.bucket, key: path)
end
get_ordered_collection(prefix) click to toggle source
# File lib/server_backups/s3.rb, line 27
def get_ordered_collection(prefix)
    OrderedBackupFileCollection.new bucket.objects(prefix: prefix)
end
save(local_file_name, s3_key) click to toggle source
# File lib/server_backups/s3.rb, line 48
def save(local_file_name, s3_key)
    full_path = if s3_key[-1] == '/'
                    File.join(s3_key, File.basename(local_file_name))
                else
                    s3_key
                end

    return if exists?(full_path)
    file = Aws::S3::Object.new(config.bucket, full_path, client: client)
    file.put(
        acl: 'private',
        body: File.open(local_file_name, 'rb'),
        content_md5: md5of(local_file_name),
        storage_class: 'STANDARD_IA'
    )
end

Private Instance Methods

md5of(local_file_name) click to toggle source
# File lib/server_backups/s3.rb, line 67
def md5of(local_file_name)
    Digest::MD5.base64digest(File.read(local_file_name))
end