<?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>My Own Private Thermopylae &#187; S3</title>
	<atom:link href="http://thermopylae.net/blog/tag/s3/feed/" rel="self" type="application/rss+xml" />
	<link>http://thermopylae.net/blog</link>
	<description>Do I know what a rhetorical question is?</description>
	<lastBuildDate>Sat, 14 Mar 2009 18:53:37 +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>Playing with S3 in Python</title>
		<link>http://thermopylae.net/blog/2008/07/22/playing-with-s3-in-python/</link>
		<comments>http://thermopylae.net/blog/2008/07/22/playing-with-s3-in-python/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 22:31:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[S3]]></category>

		<guid isPermaLink="false">http://thermopylae.net/blog/?p=4</guid>
		<description><![CDATA[This is a brief guide to getting up and running against S3 in Python. You will need the following things: AWS Developer account (you can sign up for it on http://aws.amazon.com) Make sure you sign up for S3 (select Amazon Simple Storage Service in the sidebar on the left of the AWS page and then [...]]]></description>
			<content:encoded><![CDATA[<p>This is a brief guide to getting up and running against S3 in Python.</p>
<p>You will need the following things:</p>
<ol>
<li>AWS Developer account (you can sign up for it on <a href="http://aws.amazon.com">http://aws.amazon.com</a>)</li>
<li>Make sure you sign up for S3 (select Amazon Simple Storage Service in the sidebar on the left of the AWS page and then click &#8216;Sign Up For This Web Service&#8217; over on the right)</li>
<li>Python (I&#8217;m assuming you have it set up and working already)</li>
</ol>
<p>A great library to use for access to AWS is called boto and its homepage is here: <a href="http://code.google.com/p/boto">http://code.google.com/p/boto</a>. You can download the code directly and then run its setup.py to install it, or if you already have <a href="http://peak.telecommunity.com/DevCenter/EasyInstall">easy_install</a> you can make it do all of the work:</p>
<pre class="shell">C:boto-1.3a&gt;python setup.py install</pre>
<p>or</p>
<pre class="shell">C:&gt;python easy_install.py boto</pre>
<p>Either way, you can verify that boto is properly installed by trying to import it in python:</p>
<pre class="shell">C:&gt;python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import boto
&gt;&gt;&gt;</pre>
<p>Once boto is set up correctly get your AWS identifiers from the &#8220;Your Web Services Account&#8221; dropdown in the upper right of the AWS pages. You&#8217;ll need to use both the Access key and the Secret key when you make the connection.</p>
<p>At the highest level AWS stores data in &#8216;buckets&#8217;. These are unique across the entire service and each account is limited to a max of 100 of them. Within the bucket you create key/data pairs that look like filenames.</p>
<p>The code to do this is super simple. You&#8217;ll probably need to replace &#8216;examplebucket&#8217; with something else:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> boto.<span style="color: black;">s3</span>.<span style="color: black;">connection</span> <span style="color: #ff7700;font-weight:bold;">import</span> S3Connection
<span style="color: #ff7700;font-weight:bold;">from</span> boto.<span style="color: black;">s3</span>.<span style="color: black;">key</span> <span style="color: #ff7700;font-weight:bold;">import</span> Key
conn = S3Connection<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
bucket = conn.<span style="color: black;">create_bucket</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'examplebucket'</span><span style="color: black;">&#41;</span>
k = Key<span style="color: black;">&#40;</span>bucket<span style="color: black;">&#41;</span>
k.<span style="color: black;">key</span> = <span style="color: #483d8b;">'foo'</span>
k.<span style="color: black;">set_contents_from_filename</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'foo.png'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Doing this will upload the file foo.png to the bucket &#8216;examplebucket&#8217;. That would mean that the file is accessible at: http://s3.amazonaws.com/examplebucket/foo. If you attempt to go to the appropriate URL for that file you&#8217;ll get an access denied error back. By default new objects in the bucket aren&#8217;t publicly readable. Making the object readable takes advantage of a shortcut built into boto. In the boto.s3.acl module there is a list of &#8216;ready-to-use&#8217; modes:</p>
<pre class="shell">CannedACLStrings = ['private', 'public-read', 'public-read-write', 'authenticated-read']</pre>
<p>Now we just set the &#8216;public-read&#8217; ACL on our key and it will become accessible through the URL:</p>
<pre class="shell">k.set_acl('public-read')</pre>
<p>From here, check out the <a href="http://developer.amazonwebservices.com/connect/entry.jspa?externalID=123&#038;ref=latest-docs">official documentation</a> and tools such as <a href="https://addons.mozilla.org/en-US/firefox/addon/3247">S3Fox</a> that will make your S3 experience much easier.</p>
<p>You should also grab the bota documentation archive and save it locally to use as a reference.</p>
]]></content:encoded>
			<wfw:commentRss>http://thermopylae.net/blog/2008/07/22/playing-with-s3-in-python/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
