How do I check if a file exists in AWS S3 bucket using Rails3
April 18, 2013
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
Add the below in your gem file,
Then run the bundle,
Modify your model as,
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>”
It is preferred to have the s3 credentials on the config folder and use it from the config file.
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.
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.