Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file exists on AWS S3 path using aws-sdk gem

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

like image 630
Ekta Verma Avatar asked Oct 29 '22 03:10

Ekta Verma


1 Answers

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.

like image 184
Bruno Reis Avatar answered Nov 13 '22 05:11

Bruno Reis