Class: S3

Inherits:
Object
  • Object
show all
Defined in:
lib/biosphere/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(bucket, main_key) ⇒ S3

Returns a new instance of S3.



4
5
6
7
8
9
10
11
12
# File 'lib/biosphere/s3.rb', line 4

def initialize(bucket, main_key)
    if !ENV['AWS_REGION'] || !ENV['AWS_SECRET_KEY'] || !ENV['AWS_ACCESS_KEY']
        puts "You need to specify AWS_REGION, AWS_ACCESS_KEY and AWS_SECRET_KEY"
        exit -1
    end
    @client = Aws::S3::Client.new
    @bucket_name = bucket
    @main_key = main_key
end

Instance Method Details

#delete_object(path_to_file) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/biosphere/s3.rb', line 54

def delete_object(path_to_file)
    filename = path_to_file.split('/')[-1]
    key = "#{@main_key}/#{filename}"
    puts "Fetching #{filename} from S3 from #{key}"
    begin
        resp = @client.delete_object({
            :bucket => @bucket_name,
            :key => key
        })
    rescue Aws::S3::Errors::NoSuchKey
        puts "Couldn't find remote file #{filename} from S3 at #{key}"
    rescue
        puts "\nError occurred while deleting file #{path_to_file}."
        puts "Error: #{$!}"
        exit 1
    end
end

#release_lockObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/biosphere/s3.rb', line 93

def release_lock()
    begin
        @client.delete_object({
            :bucket => @bucket_name,
            :key => "#{@main_key}/biosphere.lock"
        })
    rescue
        puts "\nCouldn't release the lock!"
        puts "Error: #{$!}"
        exit 1
    end
    puts "\nLock released successfully"
end

#retrieve(path_to_file) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/biosphere/s3.rb', line 33

def retrieve(path_to_file)
    filename = path_to_file.split('/')[-1]
    key = "#{@main_key}/#{filename}"
    puts "Fetching #{filename} from S3 from s3://#{@bucket_name}/#{key} (#{ENV['AWS_REGION']})"
    begin
        resp = @client.get_object({
            :bucket => @bucket_name,
            :key => key
        })
        File.open(path_to_file, 'w') do |f|
            f.puts(resp.body.read)
        end
    rescue Aws::S3::Errors::NoSuchKey
        puts "Couldn't find remote file #{filename} from S3 at #{key}"
    rescue
        puts "\nError occurred while fetching the remote state, can't continue."
        puts "Error: #{$!}"
        exit 1
    end
end

#save(path_to_file) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/biosphere/s3.rb', line 14

def save(path_to_file)
    filename = path_to_file.split('/')[-1]
    key = "#{@main_key}/#{filename}"
    puts "Saving #{path_to_file} to S3 #{key}"
    begin
        File.open(path_to_file, 'rb') do |file|
            @client.put_object({
                :bucket => @bucket_name,
                :key => key,
                :body => file
            })
        end
    rescue
        puts "\nError occurred while saving the remote state, can't continue"
        puts "Error: #{$!}"
        exit 1
    end
end

#set_lockObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/biosphere/s3.rb', line 72

def set_lock()
    begin
        resp = @client.get_object({
            :bucket => @bucket_name,
            :key => "#{@main_key}/biosphere.lock"
        })
        puts "\nThe remote state is locked since #{resp.last_modified}\nCan't continue."
        exit 1
    rescue Aws::S3::Errors::NoSuchKey
        puts "\nRemote state was not locked, adding the lockfile.\n"
        @client.put_object({
            :bucket => @bucket_name,
            :key => "#{@main_key}/biosphere.lock"
        })
    rescue
        puts "\There was an error while accessing the lock. Can't continue.'"
        puts "Error: #{$!}"
        exit 1
    end
end