class S3TarBackup::Backend::S3Backend

Attributes

prefix[R]

Public Class Methods

new(access_key, secret_key, region, dest_prefix) click to toggle source
# File lib/s3_tar_backup/backend/s3_backend.rb, line 12
def initialize(access_key, secret_key, region, dest_prefix)
  warn "No AWS region specified (config key settings.s3_region). Assuming eu-west-1" unless region
  @s3 = AWS::S3.new(access_key_id: access_key, secret_access_key: secret_key, region: region || 'eu-west-1')
  @prefix = dest_prefix
end

Public Instance Methods

download_item(relative_path, local_path) click to toggle source
# File lib/s3_tar_backup/backend/s3_backend.rb, line 32
def download_item(relative_path, local_path)
  bucket, path = parse_bucket_object("#{@prefix}/#{relative_path}")
  object = @s3.buckets[bucket].objects[path]
  open(local_path, 'wb') do |f|
    object.read do |chunk|
      f.write(chunk)
    end
  end
end
item_exists?(relative_path) click to toggle source
# File lib/s3_tar_backup/backend/s3_backend.rb, line 27
def item_exists?(relative_path)
  bucket, path = parse_bucket_object("#{@prefix}/#{relative_path}")
  @s3.buckets[bucket].objects[path].exists?
end
list_items(relative_path='') click to toggle source
# File lib/s3_tar_backup/backend/s3_backend.rb, line 50
def list_items(relative_path='')
  bucket, path = parse_bucket_object("#{@prefix}/#{relative_path}")
  @s3.buckets[bucket].objects.with_prefix(path).map do |x|
    BackendObject.new(x.key, x.content_length)
  end
end
name() click to toggle source
# File lib/s3_tar_backup/backend/s3_backend.rb, line 18
def name
  "S3"
end
remove_item(relative_path) click to toggle source
# File lib/s3_tar_backup/backend/s3_backend.rb, line 22
def remove_item(relative_path)
  bucket, path = parse_bucket_object("#{@prefix}/#{relative_path}")
  @s3.buckets[bucket].objects[path].delete
end
upload_item(relative_path, local_path, remove_original) click to toggle source
# File lib/s3_tar_backup/backend/s3_backend.rb, line 42
def upload_item(relative_path, local_path, remove_original)
  bucket, path = parse_bucket_object("#{@prefix}/#{relative_path}")
  @s3.buckets[bucket].objects.create(path, Pathname.new(local_path))
  File.delete(local_path) if remove_original
rescue Errno::ECONNRESET => e
  raise UploadItemFailedError.new, e.message
end

Private Instance Methods

parse_bucket_object(path) click to toggle source
# File lib/s3_tar_backup/backend/s3_backend.rb, line 58
def parse_bucket_object(path)
  path.split('/', 2)
end