Using Amazon’s CloudFront with Rails & Paperclip

Posted by – September 8, 2009

It took me a bit of experimentation, and I never found an example in a single place that showed how to set it up exactly how I wanted, so here is my code in my model for storing images used by the ArtCat calendar on Amazon S3. I am using Paperclip version 2.3.1.

First you will need to set up the distribution in Amazon for your given bucket, so that you have a URL to use for the :s3_host_alist value. I also set up a CNAME so that I can use a nice url like

Note that I don't want to store any images other than my resized ones, so my :default_style is set to :original. Some of these values are actually constants in my config files, but I've replaced those here to make it more clear.

    has_attached_file :image,
      :storage => 's3',
      :s3_credentials => "#{RAILS_ROOT}/config/s3_credentials.yml",
      :bucket => 'artcal-production',
      :s3_host_alias => '',
      :url => ':s3_alias_url',
      :path => "images/:class/:id_:timestamp.:style.:extension",
      :styles => { :thumb  => '60x60#', :medium => '270x200#', :original  => '600x600>' },
      :default_style => :original,
      :default_url => '',
      :s3_headers => { 'Expires' => 1.year.from_now.httpdate },
      :convert_options => { :all => '-strip -trim' }

Note that you do NOT have to set the ActionController::Base.asset_host to your CNAME for images. Paperclip just handles it as expected for these images.

You'll notice an interpolation in the :path that is not standard. Thanks to this Intridea Company Blog post I learned that I will need to change my image names when they are updated. CloudFront will not update my image due to that Expires header I set above for a whole year, which is not what we want to happen. I solved this by including the timestamp based on the updated_at value for the image. Based on that Intridea post, this is the code I added to config/initalizers/paperclib.rb.

Paperclip.interpolates(:timestamp) do |attachment, style|

At first I was storing the images on the file system and serving them via Apache. Moving them to CloudFront improved my page load times by at least 50%, and means that I don't have to run as powerful as server to handle a lot of traffic on this image-heavy site as I might otherwise need.

Don’t buy a Samsung monitor & Tekserve rocks

Posted by – April 10, 2009

The perils of buying a Samsung monitor are now evident.

  • Feburary 6: bought a Samsung 2043BWX monitor from Tekserve
  • Last week of February: monitor dies -- no image whatsoever
  • March 10: trying to meet deadlines, finally have time to submit an exchange request to Samsung, giving a credit card so they can mail a replacement
  • Next 4 weeks: status on Samsung site does change. I call weekly, am told that it will ship out after 15 business days, and I should be patient.
  • April 3: Told no monitor is available for an exchange, and I will be called back with options such as other monitors they could send.
  • April 6: I call back after getting no call from Samsung service, am told the person who could authorize shipping me a different model has been out and they will call me the next day.
  • April 8: I call again, and I'm told that shipping a replacement is impossible. I will need to ship them my monitor and wait 5+ business days for it to be repaired.

So, basically 4+ weeks of phone calls, and empty promises to call me back, to learn they’re not going to replace a monitor that I could have shipped in had I been told to do so on March 10. Since I bill hourly for my consulting work, I would have been better off just throwing away the monitor based on the time this has taken.

Update: I sent an email to customer service at Tekserve with a link to this blog post asking for help, and one of the owners(!) wrote back to say they’ll replace it right away.

Notes on mysql replication in Ubuntu hardy

Posted by – September 8, 2008

I'm moving some sites currently on shared hosting to Slicehost: and setting up MySQL replication so I can run backups from the slave and push them to Amazon S3 storage on a regular basis using s3sync.

This ONLamp article gives a handy step-by-step guide. One thing that got me though, was that I had set up iptables to firewall my servers, and I needed to get my master and slave able to talk to each other without opening up MySQL to the whole world. My solution was to email Slicehost and request private IP addresses for both servers.

I then changed this line in /etc/mysql/my.cnf from

bq. bind-address          =


bq. bind-address          = my private IP address

on each server. Then I added this to my iptables rules on both:

-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT

I reloaded the iptables rules with iptables-restore, and then checked with telnet from another server outside of those 2 to make sure I couldn't reach port 3306.

The next problem I encountered was an error like this when I restarted the slave after copying over the data in the master to get started:

error: 'Access denied for user 'debian-sys-maint'@'localhost' (using password: YES)'

A search of the Ubuntu forums gave me this thread which explained that debian uses a file called /etc/mysql/debian.cnf for that user password, and when I moved things over from the master DB, I changed the password for that user. The solution is to change to password in that file on the slave to the same password found on the master.

Culture Pundits official launch

Posted by – September 8, 2008


We first set up the Culture Pundits website in August 2007, but now that we have reached a critical mass of websites, traffic, and advertisers, we have announced the official launch.

Check out the press release.

Custom Drupal Modules – creating filters for Views

Posted by – July 3, 2008

Continuing my exploration of custom modules and views, here is my code for allowing view builders to filter entries based on a status field, say "special offers", that is represented in our module's database table by an integer with 1 for true or 0 for false.

Add this key to your array entry for a table, at the same level as the "join" key. See my earlier post for how the rest of the array works.

In this case, the database column is called "is_special_offer". The keys you should supply are discussed here in the Views handbook.

‘filters’ => array(
  ‘is_special_offer’ => array(
    ‘name’ => ‘Product: Is Special Offer’,
    ‘operator’ => ‘views_handler_operator_eqneq’,
    ‘list’ => ‘views_handler_operator_yesno’,
    ‘list-type’ => ‘select’,
    ‘help’ => t(‘Show only Special Offers’),

Blueprint CSS framework

Posted by – June 28, 2008

All hail the power of Blueprint. All of my new projects use it as the base for setting up the design. We used it as the basis for the redesign of ArtCal done in cooperation with Subsidiary Design.

It's the easiest grid layout CSS I have ever used, and much more convenient than YUI Grids. You also get the advantage of attractively designed fonts and sizes for all HTML elements including headers.

In anticipation of a big press release distribution announcing the official launch of Culture Pundits, I redesigned my Tristan Media LLC homepage using Blueprint, adding blocks for my three main projects. I'm using PHP with a JSON feed to rotate the featured ArtCat artist on every page load. I'm also using the Google AJAX Feed API to display recent items from RSS feeds.

Subsidiary is also working on new artist and gallery templates for ArtCat, using Blueprint.

Be careful when buying a domain on Dotster

Posted by – June 27, 2008

Yesterday, I used Dotster to buy a domain name for a new art gallery client. Their name is a Polynesian name shared with a certain beach in the Pacific, and has no hint of a porn connection, as far as I know. This is a screen grab of what Dotster put up as a placeholder page. I recommend pairNIC in the future.


Custom Drupal Modules and Views

Posted by – June 26, 2008

I'm working with a client on a Drupal site where another programmer has implemented several custom content types via a module, not CCK. I'm researching how to integrate this with the Views module, and found this useful information so far.

The hook of note seems to be hook_views_tables to get the view editing interface to know about the columns in our custom table.

Here is the start of a function to return the information to the Views module. In this example we use product_id as the primary key of our table, which is the same as the node's nid. This only exposes the name of the product to the view, but more columns and tables will be added later.

// hook to tell views about our tables
function mymodule_views_tables() {
  $tables['mymodule_product'] = array(
    'name' => 'mymodule_product',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'nid'
      'right' => array(
        'field' => 'product_id'
    'fields' => array(
      'name' => array('name' => t('Product: Name'),
      'sortable' => TRUE),
  return $tables;