Class: S3

Inherits:
Store show all
Defined in:
lib/cryo/store/s3.rb

Instance Attribute Summary collapse

Attributes inherited from Store

#logger

Instance Method Summary collapse

Methods inherited from Store

#archive_and_purge, create

Methods included from Utils

#delete_file, #get_age_from_key_name, #get_tempfile, #get_timstamped_key_name, #get_utc_time, #get_utc_time_from_key_name, #get_utc_timestamp, #gzip_file, #need_to_archive?, #safe_run, #ungzip_file, #verify_system_dependency

Constructor Details

#initialize(opts = {}) ⇒ S3

Returns a new instance of S3.



6
7
8
9
10
11
12
13
# File 'lib/cryo/store/s3.rb', line 6

def initialize(opts={})
  super(opts)
  AWS.config(:access_key_id => opts[:aws_access_key], 
             :secret_access_key => opts[:aws_secret_key])
  @s3 = AWS::S3.new
  @snapshot_bucket = @s3.buckets[opts[:snapshot_bucket]]
  @archive_bucket = @s3.buckets[opts[:archive_bucket]]
end

Instance Attribute Details

#archive_bucketObject

Returns the value of attribute archive_bucket.



4
5
6
# File 'lib/cryo/store/s3.rb', line 4

def archive_bucket
  @archive_bucket
end

#prefixObject

Returns the value of attribute prefix.



4
5
6
# File 'lib/cryo/store/s3.rb', line 4

def prefix
  @prefix
end

#snapshot_bucketObject

Returns the value of attribute snapshot_bucket.



4
5
6
# File 'lib/cryo/store/s3.rb', line 4

def snapshot_bucket
  @snapshot_bucket
end

Instance Method Details

#etag(opts = {}) ⇒ Object



38
39
40
41
42
# File 'lib/cryo/store/s3.rb', line 38

def etag(opts={})
  bucket = opts[:bucket]
  key = opts[:key]
  @s3.buckets[bucket].objects[key].etag
end

#get(opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cryo/store/s3.rb', line 15

def get(opts={})
  bucket = opts[:bucket]
  key = opts[:key]
  file_path = opts[:file] || opts[:path]
  if file_path
    File.open(file_path,'w') do |file|
      @s3.buckets[bucket].objects[key].read {|chunk| file.write chunk}
      return true
    end
  else
    return @s3.buckets[bucket].objects[key].read
  end
end

#get_archive_listObject

retun an array listing the objects in our archive bucket



52
53
54
# File 'lib/cryo/store/s3.rb', line 52

def get_archive_list
  get_bucket_listing(bucket: archive_bucket, prefix: @prefix)
end

#get_bucket_listing(opts = {}) ⇒ Object

return an array listing of objects in a bucket



57
58
59
60
61
62
63
64
65
# File 'lib/cryo/store/s3.rb', line 57

def get_bucket_listing(opts={})
  bucket = opts[:bucket]
  prefix = opts[:prefix]
  list = []
  @s3.buckets[bucket].objects.with_prefix(prefix).each do |object|
    list << object
  end
  list
end

#get_snapshot_listObject

retun an array listing the objects in our snapshot bucket



46
47
48
# File 'lib/cryo/store/s3.rb', line 46

def get_snapshot_list
  get_bucket_listing(bucket: @snapshot_bucket, prefix: @prefix)
end

#put(opts = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/cryo/store/s3.rb', line 30

def put(opts={})
  bucket = opts[:bucket]
  key = opts[:key]
  content = opts[:content]
  @s3.buckets[bucket].objects[key].write(content) # TODO: verify that bucket exists?
end