Class: Siba::Destination::AwsS3::Cloud

Inherits:
Object
  • Object
show all
Includes:
FilePlug, LoggerPlug
Defined in:
lib/siba-destination-aws-s3/cloud.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, access_key_id, secret_key) ⇒ Cloud

Returns a new instance of Cloud.



13
14
15
16
17
18
19
20
21
# File 'lib/siba-destination-aws-s3/cloud.rb', line 13

def initialize(bucket, access_key_id, secret_key)
  splitted_name = bucket.split("/")
  @bucket = splitted_name.shift
  @sub_dir = splitted_name.join("/")
  @access_key_id = access_key_id
  @secret_key = secret_key

  check_connection
end

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id.



11
12
13
# File 'lib/siba-destination-aws-s3/cloud.rb', line 11

def access_key_id
  @access_key_id
end

#bucketObject

Returns the value of attribute bucket.



11
12
13
# File 'lib/siba-destination-aws-s3/cloud.rb', line 11

def bucket
  @bucket
end

#secret_keyObject

Returns the value of attribute secret_key.



11
12
13
# File 'lib/siba-destination-aws-s3/cloud.rb', line 11

def secret_key
  @secret_key
end

#sub_dirObject

Returns the value of attribute sub_dir.



11
12
13
# File 'lib/siba-destination-aws-s3/cloud.rb', line 11

def sub_dir
  @sub_dir
end

Instance Method Details

#bucket_exists?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/siba-destination-aws-s3/cloud.rb', line 61

def bucket_exists?
  access_and_close do
    AWS::S3::Service.buckets.any?{|a| a.name == bucket}
  end
end

#check_connectionObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/siba-destination-aws-s3/cloud.rb', line 95

def check_connection
  siba_file.run_this do
    raise Siba::Error, "Bucket '#{bucket}' does not exist." unless bucket_exists?
    exists? "some_file"
    logger.debug "Access to Amazon S3 is verified"
  end
rescue Exception
  logger.error "Can not connect to Amazon S3. Bucket: #{bucket_with_path}"
  raise
end

#delete(file_name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/siba-destination-aws-s3/cloud.rb', line 84

def delete(file_name)
  access_and_close do
    begin
      AWS::S3::S3Object.delete path(file_name)
    rescue
      logger.error "Failed to delete file #{file_name} from Amazon S3"
      raise
    end
  end
end

#exists?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/siba-destination-aws-s3/cloud.rb', line 55

def exists?(file_name)
  access_and_close do
    AWS::S3::S3Object.exists? path(file_name)
  end
end

#find_objects(object_prefix) ⇒ Object



67
68
69
70
71
# File 'lib/siba-destination-aws-s3/cloud.rb', line 67

def find_objects(object_prefix)
  access_and_close do
    AWS::S3::Bucket.objects(prefix: path(object_prefix))
  end
end

#get_backups_list(backup_prefix) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/siba-destination-aws-s3/cloud.rb', line 37

def get_backups_list(backup_prefix)
  backups = find_objects backup_prefix    
  siba_file.run_this do
    backups.map do |obj|
      [File.basename(obj.key), obj.last_modified]
    end
  end
end

#get_file(file_name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/siba-destination-aws-s3/cloud.rb', line 73

def get_file(file_name)
  access_and_close do
    begin
      AWS::S3::S3Object.value path(file_name)
    rescue
      logger.error "Failed to get file #{file_name} from Amazon S3"
      raise
    end
  end
end

#restore_backup_to_dir(backup_name, dir) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/siba-destination-aws-s3/cloud.rb', line 46

def restore_backup_to_dir(backup_name, dir)
  access_and_close do
    path_to_file = File.join dir, backup_name
    open(path_to_file, 'wb') do |file|
      file.write(get_file backup_name)
    end
  end
end

#upload(src_file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/siba-destination-aws-s3/cloud.rb', line 23

def upload(src_file)        
  file_name = File.basename src_file
  logger.info "Uploading backup file to Amazon S3: #{file_name}, bucket: #{bucket_with_path}"
  access_and_close do
    unless siba_file.file_file? src_file
      raise Siba::Error, "Can not find backup file for uploading: #{src_file}"
    end

    File.open(src_file, "rb") do |file|
      AWS::S3::S3Object.store path(file_name), file
    end
  end
end