Class: StellarCoreBackup::S3

Inherits:
Object
  • Object
show all
Includes:
Contracts
Defined in:
lib/stellar-core-backup/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ S3

Returns a new instance of S3.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/stellar-core-backup/s3.rb', line 8

def initialize(config)
  @config       = config
  @working_dir  = StellarCoreBackup::Utils.create_working_dir(@config.get('working_dir'))
  @s3_region    = @config.get('s3_region')
  @s3_bucket    = @config.get('s3_bucket')
  @s3_path      = @config.get('s3_path')
  begin
    @s3_client    = Aws::S3::Client.new(region: @s3_region)
    @s3_resource  = Aws::S3::Resource.new(client: @s3_client)
  rescue Aws::S3::Errors::ServiceError => e
    puts "info: error connecting to s3"
    puts e
  end
end

Instance Method Details

#get(file) ⇒ Object

fetches a backup tar file from s3, places in working dir



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stellar-core-backup/s3.rb', line 38

def get(file)
  local_copy = "#{@working_dir}/#{File.basename(file)}"
  begin
    download = @s3_resource.bucket(@s3_bucket).object(file)
    if download.download_file(local_copy) then
      puts "info: fetched #{file} from s3 (#{@s3_bucket})"
      return local_copy
    else
      puts "error: download from s3 failed"
    end
  rescue Aws::S3::Errors::ServiceError => e
    puts "info: error downloading #{file} from s3 (#{@s3_bucket})"
    puts e
  end
end

#latest(listlen) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/stellar-core-backup/s3.rb', line 54

def latest(listlen)
  begin
    @s3_client.list_objects_v2({bucket: @s3_bucket, prefix: @s3_path+'/core-backup-'}).contents.map{|o| o.key}.sort{|a,b| a.gsub(/(\d+)/,'\1') <=> b.gsub(/(\d+)/,'\1')}.last(listlen)
  rescue Aws::S3::Errors::ServiceError => e
    puts "info: error listing s3 (#{@s3_bucket})"
    puts e
  end
end

#push(file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/stellar-core-backup/s3.rb', line 23

def push(file)
  begin
    upload = @s3_resource.bucket(@s3_bucket).object("#{@s3_path}/#{File.basename(file)}")
    if upload.upload_file(file) then
      puts "info: pushed #{file} to s3 (#{@s3_bucket})"
    else
      puts "error: upload to s3 failed"
    end
  rescue Aws::S3::Errors::ServiceError => e
    puts "info: error pushing #{file} to s3 (#{@s3_bucket})"
    puts e
  end
end