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

#fileObject



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

def file
  bucket
end

#filesObject



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

def files
  bucket.files
end

#list(prefix) ⇒ Object



50
51
52
53
# File 'lib/ey-flex/bucket_minder.rb', line 50

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

#put(filename, contents) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/ey-flex/bucket_minder.rb', line 80

def put(filename, contents)
  files.create(
    :key => filename, 
    :body => contents,
    :public => false,
    :multipart_chunk_size => 100*1024*1024, # 100MB
    'x-amz-server-side-encryption' => 'AES256'
  )
end

#remove_object(key) ⇒ Object



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

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

#s3_params(params = {}) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ey-flex/bucket_minder.rb', line 33

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



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

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



27
28
29
30
31
# File 'lib/ey-flex/bucket_minder.rb', line 27

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

#stream(key, &block) ⇒ Object



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

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