]> Use Amazon S3 Cloudfront to store images | MC Net Media

Use Amazon S3 Cloudfront to store images

mcadmin's picture

My goal is for Drupal Cloudfront module, integrated with Imagecache module, to serve images from Amazon CloudFront CDN.  The first thing I do is look to README.txt in Drupal module folders.

See Inmagecache at admin/build/imagecache (or look for it at Site Bulding).

reset Name Storage Actions
event_logo Normal Edit    Delete    Flush    Export
preview Normal Edit    Delete    Flush    Export
product_full Override Edit    Revert    Flush    Export
promotion_logo Normal Edit    Delete    Flush    Export
thumbnail Normal Edit    Delete    Flush    Export
cart Default View    Flush
product Default View    Flush
product_list Default View    Flush
uc_thumbnail Default View    Flush

Get setup with AWS.  Here is the best AWS information you'll find! Deck: AWS Slideshare by Jinesh Varia  Blog: AWS Blog by Jinesh Varia  Thank you Jinesh!  Here is the place to start: AWS Management Console.  Register, and in lieu of the Inet Console -- Download Latest Release of Bucket Explorer for Amazon CloudFront (Amazon's CDN)  Amazon S3 stores data as objects within buckets. An object is a file and metadata. Images are stored on S3 with path <bucket>/preset_id_<#>/<imagefile>.  It is safe to point multiple presets at the same bucket.

Access to AWS cloud is secure with three types of credentials offered.  I'm using Drupal, I add the "Access Keys" to /default/settings.php:
$conf = array(
   'cloudfront_access_key_id' => 'AKIxxxxxxxxxxCJLJJIA',
   'cloudfront_secret_access_key' => 'mfm8ubhdwxxxxxxxxxxNBTixxGcZNFeEl9A1EMk',
);

Enable Drupal CloudFront from the Administer->Modules page.  That's it with Drupal, for a while. 

Create a S3 bucket and use AWS Cloudfront to create a "Distrubution URL".   Keep it simple for getting started, "mybucket".  On the "Distribution", leave CNAME blank, and don't require https.   Once the distribution is entered, AWS has assigned a domain name for accessing the S3 bucket.  Something like d12e0leytdskmq.cloudfront.net.  If you don't do anything more then access results in a 403 Forbidden message.  I manually uploaded an image to my bucket and entered it as the default object at the Distribution, then the URL displays that image instead of a 403 Forbidden message.

Override theme_imagecache: You can override the theme_imagecache function by placing a function called _imagecache in your theme.php file. Here's an example:

function garland_imagecache($namespace, $path, $alt = '', $title = '', $attributes = null)
   if (module_exists('cloudfront'))
    {
       return cloudview_theme_imagecache($namespace, $path, $alt, $title, $attributes);
    }
    else
    {
    return theme_imagecache($namespace, $path, $alt = '', $title = '', $attributes = null);
    }
}

If you have already overridden theme_imagecache then take a look at _cloudview_theme_imagecache in cloudview.module to see how to integrate cloudview into your function.

  • Images will not be sent to s3 (and not be served through cloudfront) until this /cloudfront/processqueue url is accessed.
  • Set up a cron job to hit <base_url>/cloudfront/processqueue often.
  • It is safe to hit this as often as you want. If it is still processing a batch of files it will not start a second instance.