Setting up Amazon S3 as a CDN

CDN functionality provides the ability to store and serve content managed files (e.g. CSS, images, etc) from somewhere other than the application server itself.

Setting up Amazon as a CDN is relatively straightforward. Before you get started you will need:

  • an S3 Bucket and the relevant AWS credentials to read and write to it
  • a FarCry 6.3.x or 7+ installation

Configure the S3 Bucket

Add to your _serverSpecificVarsAfterInit.cfm a config for your S3 bucket.

<!--- 
 // Amazon S3 CDN 
----------------------------------------------------------------->
<cfset application.aws.accesskeyid = "TOPSECRET" />
<cfset application.aws.secretkey = "u2b3e4r5t9o0p6s7e8c9r9e9t4" />
 
<cfset application.aws.imgPrefix = "//s3.amazonaws.com/" & "io.daemon.com.au/live" />
 
<cfset application.fc.lib.cdn.setLocation(
    name="images_old",
    cdn="local",
    locationinfo=application.fc.lib.cdn.getLocation("images")) />

<cfset application.fc.lib.cdn.setLocation(
    name="images",
    cdn="s3",
    accessKeyId=application.aws.accesskeyid,
    awsSecretKey=application.aws.secretkey,
    bucket="io.daemon.com.au",
    region="",
    security="public",
    pathPrefix="/live",
    admins=["aws-daemon@daemon.com.au"]) />
    
<cfset application.fc.lib.cdn.setLocation(
    name="archive_old",
    cdn="local",
    locationinfo=application.fc.lib.cdn.getLocation("archive")) />
    
<cfset application.fc.lib.cdn.setLocation(
    name="archive",
    cdn="s3",
    accessKeyId=application.aws.accesskeyid,
    awsSecretKey=application.aws.secretkey,
    bucket="io.daemon.com.au",
    region="",
    security="private",
    urlExpiry=1800,
    pathPrefix="/live/mediaArchive",
    admins=["aws-daemon@daemon.com.au"]) />
    
<cfset application.fc.lib.cdn.setLocation(
    name="publicfiles_old",
    cdn="local",
    locationinfo=application.fc.lib.cdn.getLocation("publicfiles")) />
    
<cfset application.fc.lib.cdn.setLocation(
    name="publicfiles",
    cdn="s3",
    accessKeyId=application.aws.accesskeyid,
    awsSecretKey=application.aws.secretkey,
    bucket="io.daemon.com.au",
    region="",
    security="public",
    pathPrefix="/live/files",
    admins=["aws-daemon@daemon.com.au"]) />

<cfset application.fc.lib.cdn.setLocation(
    name="privatefiles_old",
    cdn="local",
    locationinfo=application.fc.lib.cdn.getLocation("privatefiles")) />
    
<cfset application.fc.lib.cdn.setLocation(
    name="privatefiles",
    cdn="s3",
    accessKeyId=application.aws.accesskeyid,
    awsSecretKey=application.aws.secretkey,
    bucket="io.daemon.com.au",
    region="",
    security="private",
    urlExpiry=1800,
    pathPrefix="/live/securefiles",
    admins=["aws-daemon@daemon.com.au"]) />

Updating your Webskins

Once you have a CDN config in place you will need to change the way you reference files and images in your views (aka webskins).

files

All files should be linked to via /download.cfm, where FarCry will take care of file security and location. The format for these links is:
/download.cfm?downloadfile=[content objectid]&typename=[content typename]&fieldname=[file field]

images

Images in code should reference their src attribute using the types.getFileLocation() function.

For example, referencing a teaser image in a News article:

<cfif isValid("UUID",stObj.teaserImage)>
	<cfset stImage = application.fapi.getContentObject(stObj.teaserImage)>
	<a href="#link#" class="thumbnail">
		<cfif len(stImage.thumbnailImage)><img class="thumb" title="#stImage.label#" src="#getFileLocation(stObject=stImage,fieldname="thumbnailImage").path#" /></cfif>
	</a>
</cfif>

Alternatively, you can prefix the relative file path with the result of either application.fapi.getImageWebRoot() or application.fc.lib.cdn.ioGetFileLocation().path

More info

For more details on implementing CDNs in FarCry 6.3.x and 7.+ be sure to check out Working With CDNs on the WIKI.