Class: ExportMongoS3::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/export_mongo_s3/storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Storage

Returns a new instance of Storage.



4
5
6
7
8
# File 'lib/export_mongo_s3/storage.rb', line 4

def initialize(options)
  @s3           = AWS::S3.new({access_key_id: options[:access_key_id], secret_access_key: options[:secret_access_key]})
  @bucket       = get_bucket(options[:bucket])
  @store_prefix = options[:store_prefix] || ''
end

Instance Method Details

#get_uploaded_files(prefix = '', limit = 100) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/export_mongo_s3/storage.rb', line 36

def get_uploaded_files(prefix = '', limit = 100)
  result = []

  prefix = get_full_store_path(prefix)

  @bucket.objects.with_prefix(prefix).each(:limit => limit) do |object|
    unless object.key.end_with?('/')
      result << object
    end
  end

  result
end

#upload(store_path, file_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/export_mongo_s3/storage.rb', line 13

def upload(store_path, file_name)
  key = File.join(get_full_store_path(store_path), File.basename(file_name))

  checksum = get_signature(file_name)

  begin

    file = File.open(file_name, 'rb')

    obj = @bucket.objects[key]

    obj.write(:content_length => file.size, metadata: {checksum: checksum}) do |buffer, bytes|
      buffer.write(file.read(bytes))
    end

    file.close

  rescue Exception => error
    raise "Error upload file <#{file_name}> to s3 <#{key}>: #{error.message}"
  end

end