Introduction
Amazon S3 provides a simple web services interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web. While uploading a file to S3 we need to check whether the file exists to avoid any data duplication.
Steps to check the presence of a file
Step#1
Add the below in your gem file,
[sourcecode]gem ‘aws-s3′[/sourcecode]
Then run the bundle,
[sourcecode]bundle install[/sourcecode]
Step#2
Modify your model as,
[sourcecode]require ‘aws/s3’
def is_file_exist?
AWS::S3::Base.establish_connection!( :access_key_id => ‘S3_KEY’, :secret_access_key => ‘S3_SECRET’)
return AWS::S3::S3Object.exists? attachment_id, “<YOUR_BUCKET_NAME>”
end[/sourcecode]
It is preferred to have the s3 credentials on the config folder and use it from the config file.
Step#3
Now modify your controller where you want to check the existence of the file,
[sourcecode]current_user.is_file_exist? #return true if exists[/sourcecode]
Easy method to avoid data redundancy in secured AWS S3.
Read Also: How to use Amazon S3 Bucket with Paperclip to store images in Rails3
I hope you find this useful. If you want to develop application in rails or want to deploy app in AWS cloud, then Andolasoft is the ideal and cost savvy option for you. Have something to add to this topic? Share it in the comments.