<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spiral_code &#187; paperclip</title>
	<atom:link href="http://blog.trydionel.com/tag/paperclip/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.trydionel.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Aug 2010 00:35:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using Paperclip with Amazon S3</title>
		<link>http://blog.trydionel.com/2009/11/08/using-paperclip-with-amazon-s3/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=using-paperclip-with-amazon-s3</link>
		<comments>http://blog.trydionel.com/2009/11/08/using-paperclip-with-amazon-s3/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 23:08:32 +0000</pubDate>
		<dc:creator>Jeff Tucker</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[imagemagick]]></category>
		<category><![CDATA[paperclip]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[s3]]></category>

		<guid isPermaLink="false">http://trydionel.wordpress.com/?p=21</guid>
		<description><![CDATA[I recently launched the website for my wedding on Heroku. I opted to use Thoughtbot&#8217;s popular Paperclip gem to manage photo attachments for the site. It&#8217;s a snap to use with filesystem storage (the default), but Heroku doesn&#8217;t allow applications &#8230; <a href="http://blog.trydionel.com/2009/11/08/using-paperclip-with-amazon-s3/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently launched the website for my wedding on Heroku. I opted to use Thoughtbot&#8217;s popular Paperclip gem to manage photo attachments for the site. It&#8217;s a snap to use with filesystem storage (the default), but Heroku doesn&#8217;t allow applications to have access to the filesystem. Plan B then is to use Paperclip&#8217;s ability to manage files over Amazon&#8217;s Simple Storage Service (aka Amazon S3).  S3 is cloud storage for ground people. It offers super cheap storage and huge bandwidth, allowing small websites to use it as a simple CDN. Initial setup is simple, albeit a bit unusual.</p>
<h3>Basic S3 Setup</h3>
<p>After registering for the service at Amazon&#8217;s site, you&#8217;ll need to collect your access credentials to allow Rails to connect to your account.  You can find this in the <a href="https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&amp;action=access-key">Security Credentials</a> link under the Your Account menu.  Create a simple yaml file in your <code>config</code> directory with the following details:</p>
<pre class="brush: ruby;">
# config/s3.yml
access_key_id: &amp;lt;your access key here&amp;gt;
secret_code: &amp;lt;your secret code here&amp;gt;
</pre>
<p>Note that this file can be configured to support multiple environments as well:</p>
<pre class="brush: ruby;">
# config/s3.yml
development:
  access_key_id: &amp;lt;your development access key here&amp;gt;
  secret_code: &amp;lt;your development secret code here&amp;gt;
production:
  access_key_id: &amp;lt;your production access key here&amp;gt;
  secret_code: &amp;lt;your production secret code here&amp;gt;
</pre>
<h3>S3 Buckets</h3>
<p>I found that the tricky part in setting up Paperclip was dealing with S3 buckets.  <a href="http://docs.amazonwebservices.com/AmazonS3/latest/index.html?UsingBucket.html">Buckets</a> are something like file folders in the cloud, with the unusual stipulation that each bucket name must be unique across the entire service.  Paperclip requires that you specify an S3 bucket to store your files in; unfortunately it doesn&#8217;t handle the creation of the buckets for you.  That&#8217;s where the <a href="http://amazon.rubyforge.org/">aws-s3 gem</a> comes into play.  Install it with <code>gem install aws-s3</code>, drop into the Rails console and execute the following:</p>
<pre class="brush: ruby;">
# load the aws-s3 gem
&amp;gt;&amp;gt; require 'aws/s3'
=&amp;gt; []
# connect to your account
&amp;gt;&amp;gt; AWS::S3::Base.establish_connection!(YAML.load_file('config/s3.yml').symbolize_keys!)
=&amp;gt; &amp;lt;AWS::S3::Connection:0x2236d90 @secret_access_key=&amp;lt;your secret key&amp;gt;, @http=#&amp;lt;Net::HTTP s3.amazonaws.com:80 open=false&amp;gt;, @access_key_id=&amp;lt;your access id&amp;gt;, @options={:access_key_id=&amp;gt;&amp;lt;your access id&amp;gt;, :secret_access_key=&amp;gt;&amp;lt;your secret key&amp;gt;, :server=&amp;gt;&amp;quot;s3.amazonaws.com&amp;quot;, :port=&amp;gt;80}&amp;gt;
# create your unique bucket
&amp;gt;&amp;gt; AWS::S3::Bucket.create('your_unique_s3_bucket')
=&amp;gt; true
</pre>
<h3>Paperclip with S3</h3>
<p>Once you&#8217;ve got your configuration file set and bucket created, it&#8217;s time to set up your model. <a href="http://dev.thoughtbot.com/paperclip/">Paperclip&#8217;s RDOC</a> is really great at describing all the various options for their model and is well worth the time to read through. On top of that, there are several <a href="http://patshaughnessy.net/2009/4/30/paperclip-sample-app">great tutorials</a> on getting Paperclip running. The basics though, with the important S3 bits added, look like this:</p>
<pre class="brush: ruby;">
# models/photo.rb
has_attached_file :image,
  :styles =&amp;gt; { :thumbnail =&amp;gt; &amp;quot;100x100&amp;gt;&amp;quot; },
  :storage =&amp;gt; :s3,
  :s3_credentials =&amp;gt; &amp;quot;#{Rails.root}/config/s3.yml&amp;quot;,
  :bucket =&amp;gt; &amp;quot;your_unique_s3_bucket&amp;quot;;
</pre>
<p>Note that you can specify the <code>:bucket</code> option in your <code>config/s3.yml</code> file instead.</p>
<h3>Using ImageMagick to Auto-Orient Images</h3>
<p>With the above, you&#8217;ll have a functional S3-backed Paperclip-powered model. I quickly ran into an annoying problem though. Modern digital cameras add special metadata to their photos that indicates the orientation of the camera when a photo is taken.  Unfortunately, most web browsers aren&#8217;t smart enough yet to handle that extra info, so whereas the photo appears as intended on the desktop, it appears flopped onto its side on the web. <a href="http://john.2nout.com/articles/2009/01/11/auto_rotate-post-upload-processor-for-paperclip-rails-plugin">Some developers</a> have taken to using RMagick to properly orient their photo uploads, but I&#8217;ve found that there&#8217;s a simpler way. Paperclip exposes a hook to send custom conversion flags straight to ImageMagick when a photo is uploaded. Even better, ImageMagick has a simple flag <code>-auto-orient</code> to handle automatically orient images according to their metadata. Simply change the model above to:</p>
<pre class="brush: ruby;">
# models/photo.rb
has_attached_file :image,
  :styles =&amp;gt; { :thumbnail =&amp;gt; &amp;quot;100x100&amp;gt;&amp;quot; },
  :storage =&amp;gt; :s3,
  :s3_credentials =&amp;gt; &amp;quot;#{Rails.root}/config/s3.yml&amp;quot;,
  :bucket =&amp;gt; &amp;quot;your_unique_s3_bucket&amp;quot;,
  :convert_options =&amp;gt; { :all =&amp;gt; &amp;quot;-auto-orient&amp;quot; }
</pre>
<p>You&#8217;ll now have a modern-camera-friendly, cloud-hosted image attachment system for your app.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.trydionel.com/2009/11/08/using-paperclip-with-amazon-s3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
