If you are looking for a way to check if a file exists in Amazon S3 bucket, then probably this may help you.
One possible approach is to use Undesigned Amazon S3 library. Despite they don’t have a checkObject() function, you can use Rest API > Operation on Objects > HEAD object to return the head of an object or a Not Found error to know if the object exists or not.
Another possible approach may be using a kind of cache in your local server that keep track what is present on the Amazon S3 buckets. For example, you can use the approach followed by Slayerment Blog (Drupal CDN File Server Amazon S3), that mentioned the following:
If file exists
So this is one of the most important steps that I took to make this possible. Rather than actually checking if the file exists on S3 with one of the S3 class functions (slow) or via something like
file_get_contents(slow) I went with a database table on my own server that keeps track of what is on the S3 server (fast). This table keeps track of every filepath and when it was created and changed on S3. I am able to use the changed timestamp from my table to check it against the local file’s timestamp (
filemtime) to know when the file needs to be updated on S3.
So, instead of using the slower remote functions CURL of gile_get_contents, they just keep track of files in the local server and then ask locally if the file exists or not.