I am trying to check whether a particular pdf file exists on AWS S3 using aws-sdk gem (version 2) inside ruby on rails application.
I have the AWS connection established and currently using exists?
method:
puts @bucket.objects(prefix:"path/sample_100.pdf").exists?
on running the above statement, I get the below no method error:
undefined method 'exists?' for Aws::Resources::Collection
Checked few documents but of not much help. Is there any other way to achieve the same?
Thanks in advance
I'm not a Ruby developer myself, but I might be able to suggest something.
The usual way to check whether an object exists in Amazon S3 is using the HEAD Object operation. Basically, it returns the metadata (but no content) of an object if it exists, or a 404 error if it doesn't. It's like GET Object, but without the contents of the object.
I just looked up in the AWS SDK for Ruby API Reference and found this method:
http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html#head_object-instance_method
Take a look at that, it's probably what you are looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With