<?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>threehv &#187; filesystems</title>
	<atom:link href="http://www.3hv.co.uk/blog/tag/filesystems/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3hv.co.uk/blog</link>
	<description>precision engineering for your website</description>
	<lastBuildDate>Fri, 13 Jan 2012 16:18:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Moving attachment_fu files from the local filesystem to Amazon S3</title>
		<link>http://www.3hv.co.uk/blog/2009/11/24/moving-attachment_fu-files-from-the-local-filesystem-to-amazon-s3/</link>
		<comments>http://www.3hv.co.uk/blog/2009/11/24/moving-attachment_fu-files-from-the-local-filesystem-to-amazon-s3/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 20:26:08 +0000</pubDate>
		<dc:creator>Rahoul Baruah</dc:creator>
				<category><![CDATA[Ruby on Rails and Software Development]]></category>
		<category><![CDATA[Amazon S3]]></category>
		<category><![CDATA[attachment_fu]]></category>
		<category><![CDATA[filesystems]]></category>

		<guid isPermaLink="false">http://www.3hv.co.uk/blog/?p=611</guid>
		<description><![CDATA[attachment_fu may not be the in-thing any more but there are still a lot of sites out there using it. And every now and then you realise, far too late, that you should have used S3 instead of the local filesystem. Switching is easy &#8211; just change the :storage parameter to :s3. But before you [...]]]></description>
			<content:encoded><![CDATA[<p>attachment_fu may not be the in-thing any more but there are still a lot of sites out there using it.  And every now and then you realise, far too late, that you should have used S3 instead of the local filesystem.  </p>
<p>Switching is easy &#8211; just change the :storage parameter to :s3.  But before you can do that, how do you get the existing files onto S3 in the first place?  </p>
<p>Well, here&#8217;s your answer.  </p>
<p><strong>Before</strong> you change the storage parameter, create you S3 account and a bucket to store your files.  </p>
<p>Then add a new rake task to your application, looking something like this: </p>
<pre><code>
  desc "Upload files to S3"
  task :upload_files_to_s3 => :environment do

    AWS::S3::Base.establish_connection!(
      :access_key_id => 'MYACCESSKEY',
      :secret_access_key => 'MYSECRETKEY')

    Model.find(:all).each do | user |
      filename = "/home/user/app/current/public#{model.public_filename}"
      puts "Migrating model #{model.id} - #{filename}"
      if File.exists?(filename)
        AWS::S3::S3Object.store("/models/#{model.id}/#{model.filename}",
          open(filename),
          'my_bucket',
          :access => :public_read)
        puts "...migrated"
      else
        puts "...not found"
      end
    end
  end
</code></pre>
<p>In other words; </p>
<ul>
<li>connect to S3</li>
<li>for each &#8220;model&#8221; instance, find the attachment on the local filesystem</li>
<li>if found, then push it to S3, using /models/id/filename as the key (where models is your table name)</li>
</ul>
<p>As soon as you&#8217;ve done that, switch the :storage to :s3 and redeploy your app (not forgetting your config/amazon_s3.yml file).  And, with a bit of luck, you should see your files being served from Amazon&#8217;s servers, instead of your own.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.3hv.co.uk/blog/2009/11/24/moving-attachment_fu-files-from-the-local-filesystem-to-amazon-s3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.181 seconds -->

