public
Description: Simple rails plugin that makes it easy to store uploaded files on Amazon S3
Clone URL: git://github.com/standout/s3storage.git
standout (author)
Tue Apr 22 13:30:20 -0700 2008
commit  808bd158b6ac3d06db051eec7e0a6f13cb39f848
tree    4c8a290d0389b211cbd83c367bb9855ec1cf17b8
parent  44091c27c1f993af21a2cd2c1c4af6b38d19aee0
name age message
file MIT-LICENSE Tue Apr 22 00:40:03 -0700 2008 First commit [standout]
file README Tue Apr 22 13:30:20 -0700 2008 Removed <code>-blocks from readme. [standout]
file Rakefile Tue Apr 22 00:40:03 -0700 2008 First commit [standout]
file init.rb Tue Apr 22 00:40:03 -0700 2008 First commit [standout]
file install.rb Tue Apr 22 00:40:03 -0700 2008 First commit [standout]
directory lib/ Tue Apr 22 13:26:59 -0700 2008 Updated readme with some examples of usage. [standout]
directory tasks/ Tue Apr 22 00:40:03 -0700 2008 First commit [standout]
directory test/ Tue Apr 22 00:40:03 -0700 2008 First commit [standout]
file uninstall.rb Tue Apr 22 00:40:03 -0700 2008 First commit [standout]
README
S3Storage
=========

This is an alpha-release. Use at your own risk.

This plugin is created for the Ruby on Rails framework and makes it really simple to 
use Amazon S3 for storing your uploaded files. 

You will need an account with Amazon Web Services http://aws.amazon.com/ 

Installation
============
Install the Amazon gem first

  sudo gem install aws-s3

Then you can install the plugin itself:

  ruby ./script/install plugin git://github.com/standout/s3storage.git

Your model needs to have the following fields for this to work.

  - original_filename
  - content_type


Usage
=======

The plugin will create your bucket automatically if it does not already exist. 
Remember that bucket names must be unique across all of S3. I usually get around this
by setting the bucket name to a domain name.

I'll show some example code for a model called Document.

Scaffold (command line):
ruby ./script/generate scaffold Document title:string, original_filename:string, content_type:string
rake db:migrate

Model (app/models/document.rb):
class Document < ActiveRecord::Base
  s3_storage  :bucket => 'YOUR_BUCKET_NAME',
              :access_key => 'YOUR_ACCESS_KEY',
              :secret => 'YOUR_SECRET_KEY'
end

New document (app/views/document/new.html.erb):
In your view, you'll need to have a form for uploading.
<% form_tag ({:controller => 'documents', :action => 'create'}, {:multipart => true}) do %>
  <p>
    <label for="document_title">Title</label><br />
    <%= text_field :document, :title %>
  </p>
  <p>
    <label for="document_file">Pick a file to upload</label><br />
    <%= file_field :document, :file %> <!-- this is the important part -->
  </p>
  <%= submit_tag "Upload file" %>
<% end %>

List of documents (app/views/document/index.html.erb):
<ul>
<% for document in @documents %
  <li>
    <%= link_to document.title, document.s3_path %>
  </li>
<% end %>
</ul>

That's it. Your uploaded files will now be stored in your Amazon S3 bucket.
Files will be deleted from Amazon S3 when you delete the object as usual in rails.

-------
Copyright (c) 2008 David Svensson http://www.standout.se/, released under the MIT license