<?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>func09 &#187; s3</title>
	<atom:link href="http://www.func09.com/wordpress/archives/tag/s3/feed" rel="self" type="application/rss+xml" />
	<link>http://www.func09.com/wordpress</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 06 Feb 2012 04:25:55 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>[Rails] PaperClipを使って本番の時だけAmazon S3を使う</title>
		<link>http://www.func09.com/wordpress/archives/674</link>
		<comments>http://www.func09.com/wordpress/archives/674#comments</comments>
		<pubDate>Wed, 09 Sep 2009 04:23:30 +0000</pubDate>
		<dc:creator>haga</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[s3]]></category>

		<guid isPermaLink="false">http://www.func09.com/wordpress/?p=674</guid>
		<description><![CDATA[PaperClipはとってもシンプルなActiveRecordで添付ファイルを管理するプラグインです。 http://github.com/thoughtbot/paperclip/tree/master 便利すぎて鼻血 [...]]]></description>
			<content:encoded><![CDATA[<p>PaperClipはとってもシンプルなActiveRecordで添付ファイルを管理するプラグインです。</p>

<p><a href="http://github.com/thoughtbot/paperclip/tree/master">http://github.com/thoughtbot/paperclip/tree/master</a></p>

<p>便利すぎて鼻血がでそうなプラグインです</p>

<h2>使い方のおさらい</h2>

<p>カラムを追加して</p>

<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; class AddAvatarColumnsToUser &lt; ActiveRecord::Migration<br />
&nbsp; &nbsp; def self.up<br />
&nbsp; &nbsp; &nbsp; add_column :users, :avatar_file_name, &nbsp; &nbsp;:string<br />
&nbsp; &nbsp; &nbsp; add_column :users, :avatar_content_type, :string<br />
&nbsp; &nbsp; &nbsp; add_column :users, :avatar_file_size, &nbsp; &nbsp;:integer<br />
&nbsp; &nbsp; &nbsp; add_column :users, :avatar_updated_at, &nbsp; :datetime<br />
&nbsp; &nbsp; end<br />
<br />
&nbsp; &nbsp; def self.down<br />
&nbsp; &nbsp; &nbsp; remove_column :users, :avatar_file_name<br />
&nbsp; &nbsp; &nbsp; remove_column :users, :avatar_content_type<br />
&nbsp; &nbsp; &nbsp; remove_column :users, :avatar_file_size<br />
&nbsp; &nbsp; &nbsp; remove_column :users, :avatar_updated_at<br />
&nbsp; &nbsp; end<br />
&nbsp; end</div></div>

<p>モデルクラスで定義</p>

<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">class User &lt; ActiveRecord::Base<br />
&nbsp; &nbsp; has_attached_file :avatar<br />
end</div></div>

<p>とするだけですね。
詳細は<a href="http://rdoc.info/projects/thoughtbot/paperclip">RDoc</a>をみてください</p>

<h2>Amazon S3を使う</h2>

<p>PaperClipはストレージに<a href="http://rdoc.info/rdoc/thoughtbot/paperclip/blob/d6ebd321c801e88a173dd5f929db36db05373893/Paperclip/Storage/S3.html">S3を使うためのオプション</a>があります</p>

<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">has_attached_file :avatar,<br />
&nbsp; &nbsp; :storage =&gt; :s3,<br />
&nbsp; &nbsp; :s3_credentials =&gt; &quot;#{RAILS_ROOT}/config/s3.yml&quot;,<br />
&nbsp; &nbsp; :path =&gt; &quot;:attachment/:id/:style.:extension&quot;,<br />
&nbsp; &nbsp; :bucket =&gt; 'mybucket'</div></div>

<p>config/s3.ymlには</p>

<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">access_key_id: 456...<br />
secret_access_key: 456...</div></div>

<p>こんな感じのアクセスキーとシークレットキーを置いておきます</p>

<h2>productionでだけS3を使いたい</h2>

<p>開発中はローカルで完結したい場合は</p>

<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; has_attached_file :avatar, Proc.new {<br />
&nbsp; &nbsp; if RAILS_ENV == 'production'<br />
&nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; :storage =&gt; :s3,<br />
&nbsp; &nbsp; &nbsp; &nbsp; :s3_credentials =&gt; &quot;#{RAILS_ROOT}/config/s3.yml&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; :path =&gt; &quot;:attachment/:id/:style.:extension&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; :bucket =&gt; 'mybucket'<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; {}<br />
&nbsp; &nbsp; end<br />
&nbsp; }.call</div></div>

<p>こんな感じでいいかな</p>
]]></content:encoded>
			<wfw:commentRss>http://www.func09.com/wordpress/archives/674/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

