class ExportMongoS3::Storage

Public Class Methods

new(options) click to toggle source
# 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

Public Instance Methods

get_uploaded_files(prefix = '', limit = 100) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

get_bucket(bucket_name) click to toggle source
# File lib/export_mongo_s3/storage.rb, line 53
def get_bucket(bucket_name)
  bucket = @s3.buckets[bucket_name]

  unless bucket.exists?
    raise "Bucket #{bucket_name} doesn't not exists. Please create it"
  end

  bucket
end
get_full_store_path(path) click to toggle source
# File lib/export_mongo_s3/storage.rb, line 67
def get_full_store_path(path)
  if @store_prefix.empty?
    path
  else
    File.join(@store_prefix, path)
  end
end
get_signature(file_name) click to toggle source
# File lib/export_mongo_s3/storage.rb, line 63
def get_signature(file_name)
  Digest::MD5.hexdigest(File.size(file_name).to_s)
end