Search code examples
javascriptamazon-web-servicesamazon-s3amazon-javascript-sdk

Check if File Exists on AWS S3 Using Browser JavaScript SDK?


I recently changed the naming convention for a file in my job folders. Since I need to support both the new naming convention and the old naming convention when a user tries to download the specific file, I need to check if the new naming standard URL exists and if not, download from the old naming standard URL.

Is there a way to 'ping' a S3 URL to see if a valid file is stored at the URL? Standard AJAX calls doesn't work because of cross-domain issues.

The files I am checking for are binary files.


Solution

  • You can have a look at the headObject() method in the AWS JavaScript SDK, but if the file is publicly accessible, a simple HEAD request (using ajax) will also do.

    You can work out the cross domain issues by specifying a CORS policy on your bucket.