Class: ExportMongoS3::Storage
- Inherits:
-
Object
- Object
- ExportMongoS3::Storage
- Defined in:
- lib/export_mongo_s3/storage.rb
Instance Method Summary collapse
- #get_uploaded_files(prefix = '', limit = 100) ⇒ Object
-
#initialize(options) ⇒ Storage
constructor
A new instance of Storage.
- #upload(store_path, file_name) ⇒ Object
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() @s3 = AWS::S3.new({access_key_id: [:access_key_id], secret_access_key: [:secret_access_key]}) @bucket = get_bucket([:bucket]) @store_prefix = [: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.}" end end |