Class: EY::BucketMinder

Inherits:
Object
  • Object
show all
Defined in:
lib/ey-flex/bucket_minder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_id, secret_key, bucket_name, region = 'us-east-1') ⇒ BucketMinder

Returns a new instance of BucketMinder.



5
6
7
8
9
10
11
# File 'lib/ey-flex/bucket_minder.rb', line 5

def initialize(secret_id, secret_key, bucket_name, region = 'us-east-1')
  @s3          = Fog::Storage.new(:provider => 'AWS',:aws_access_key_id => secret_id, :aws_secret_access_key => secret_key, :region => region)
  @region      = region
  @bucket_name = bucket_name || "ey-backup-#{Digest::SHA1.hexdigest(secret_id)[0..11]}"

  setup_bucket
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



13
14
15
# File 'lib/ey-flex/bucket_minder.rb', line 13

def bucket_name
  @bucket_name
end

Instance Method Details

#bucketObject



15
16
17
# File 'lib/ey-flex/bucket_minder.rb', line 15

def bucket
  @bucket ||= @s3.directories.get(@bucket_name)
end

#filesObject



19
20
21
# File 'lib/ey-flex/bucket_minder.rb', line 19

def files
  bucket.files
end

#list(prefix) ⇒ Object



46
47
48
49
# File 'lib/ey-flex/bucket_minder.rb', line 46

def list(prefix)
  listing = files.all(:prefix => prefix)
  s3merge(listing)
end

#put(filename, contents) ⇒ Object



76
77
78
# File 'lib/ey-flex/bucket_minder.rb', line 76

def put(filename, contents)
  files.create(:key => filename, :body => contents)
end

#remove_object(key) ⇒ Object



38
39
40
# File 'lib/ey-flex/bucket_minder.rb', line 38

def remove_object(key)
  @s3.delete_object(bucket.key, key)
end

#s3_params(params = {}) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ey-flex/bucket_minder.rb', line 29

def s3_params(params = {})
  return params if @region == 'us-east-1'
  if @region == 'eu-west-1'
    params.merge({:location => 'EU'})
  else
    params.merge({:location => @region})
  end
end

#s3merge(list) ⇒ Object

Merge s3 file listing to work with split files with naming of *.partdd



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ey-flex/bucket_minder.rb', line 52

def s3merge(list)
  return list if list.empty?
  distinct_files=Array.new()

  list.each do |item|
    fname = item.key.gsub(/.part\d+$/,'')
    match = false
    distinct_files.each_with_index do |b, i|
      if b[:name] == fname
        distinct_files[i][:keys] << item.key
        match = true
      end
    end

    if not match
      path = Array.new()
      path << item.key
      file = {:name => fname, :keys => path}
      distinct_files << file
    end
  end
  distinct_files
end

#setup_bucketObject



23
24
25
26
27
# File 'lib/ey-flex/bucket_minder.rb', line 23

def setup_bucket
  unless bucket
    @s3.directories.create(s3_params(:key => @bucket_name))
  end
end

#stream(key, &block) ⇒ Object



42
43
44
# File 'lib/ey-flex/bucket_minder.rb', line 42

def stream(key, &block)
  files.get(key, &block)
end