Check if a file exists in Amazon S3 bucket

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 cURL or 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.


Did you like it?

4 Responses to “Check if a file exists in Amazon S3 bucket”

  1. anon
    May 13, 2011 at 12:21 pm #

    A local cache sounds like a very good idea. Thanks. :)

  2. August 15, 2011 at 11:24 am #

    I would suggest, store that API function in a variable and play around it.
    Its very easy

  3. November 11, 2011 at 7:10 am #

    cUrl works fine and is fast.
    The local cache would be great but cannot be used with dynamic files that change.

  4. June 1, 2012 at 1:18 am #

    so no code for this? whats the point of this post

Leave a Comment