Saturday, January 7, 2012

Validate Amazon URL in ColdFusion

All my images are on Amazon S3. Some of my records don't have images there -- or they do, but they don't have thumbnails. In listings, I tried the following things, with the following results.

1. Verify file exists. Doesn't work unless you do relative URL it appears.

2. Use HTTP 'trace'. That didn't work either. Got a 'forbidden' response to everything.

3. Use "IsImage" with the full URL. That didn't work either, it believed everything to be ok.

4. Use HTTP 'head'. Then check for 200 OK response code. That worked!

<cfset myfileref="full-path-to-image-filename-goes-here">
<cfhttp method="head" url="#variables.myfileref#">
<cfif cfhttp.statusCode is not '200 OK'>
<cfset myfileref="#variables.path-to-file#/icon-spacer.gif">

So when variables.myfileref is called it will either be the valid image or the spacer replacement image I have, if needed.


PJ

No comments: