Class: S3TarBackup::Backend::S3Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_tar_backup/backend/s3_backend.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_key, region, dest_prefix) ⇒ S3Backend

Returns a new instance of S3Backend.



12
13
14
15
16
# 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

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/s3_tar_backup/backend/s3_backend.rb', line 8

def prefix
  @prefix
end

Instance Method Details

#download_item(relative_path, local_path) ⇒ Object



32
33
34
35
36
37
38
39
40
# 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) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# 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 = '') ⇒ Object



50
51
52
53
54
55
# 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

#nameObject



18
19
20
# File 'lib/s3_tar_backup/backend/s3_backend.rb', line 18

def name
  "S3"
end

#remove_item(relative_path) ⇒ Object



22
23
24
25
# 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) ⇒ Object



42
43
44
45
46
47
48
# 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