Created
September 24, 2012 05:31
-
-
Save jhubert/3774422 to your computer and use it in GitHub Desktop.
Example of how to use the post-to-s3 ruby on rails gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Upload Your File</h1> | |
<%= s3_upload_form_for(@upload) do %> | |
<p><%= label_tag :file, 'Select Your File' %></p> | |
<p><%= file_field_tag :file %></p> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"disclaimer": "This is just an example and may not work for your S3 setup.", | |
"Sid": "Stmt1328506705529", | |
"Effect": "Allow", | |
"Principal": { | |
"AWS": "arn:aws:iam::395340211253:root" | |
}, | |
"Action": [ | |
"s3:PutObject" | |
], | |
"Resource": "arn:aws:s3:::mywebsite-media/uploads/*" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a configuration file in config/ | |
development: | |
bucket_name: <your bucket name> | |
access_key_id: <your access key> | |
secret_access_key: <your secret key> | |
test: | |
bucket_name: <your bucket name> | |
access_key_id: <your access key> | |
secret_access_key: <your secret key> | |
production: | |
bucket_name: <your bucket name> | |
access_key_id: <your access key> | |
secret_access_key: <your secret key> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is an initializer in config/initializers/, however, storing your S3 keys in a file that you check in might not be the best idea. This is mostly to show how to explicitly set the values. | |
# PostToS3 defaults to checking for AMAZON_ACCESS_KEY_ID and AMAZON_SECRET_ACCESS_KEY environment variables. If you set them that way, you can omit the keys from this configuration process. | |
APP_CONFIG = {} | |
APP_CONFIG[:s3] = YAML.load_file("#{Rails.root}/config/s3.yml")[Rails.env.to_s].symbolize_keys! | |
PostToS3.configure do |config| | |
config.access_key_id = APP_CONFIG[:s3][:access_key_id] | |
config.access_key_id = APP_CONFIG[:s3][:secret_access_key] | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UploadsController < ApplicationController | |
def new | |
# Define whatever path you want the upload to have here. | |
key = "uploads/#{Time.now.strftime('%Y%m%d%H%M%S')}" | |
@upload = PostToS3::FileUpload.new( | |
:bucket_url => "http://#{APP_CONFIG[:s3][:bucket_name]}.s3.amazonaws.com/", | |
:key => key, | |
:policy => { | |
'expiration' => 10.hours.from_now, # Set this to whatever is appropriate | |
'conditions' => [ | |
{ 'bucket' => APP_CONFIG[:s3][:bucket_name] }, | |
{ 'key' => key }, | |
{ 'acl' => 'private' }, # Change this if you want it to be public | |
{ 'success_action_redirect' => '/upload-finished.html' } # Change the URL that you want the user redirected to | |
] | |
} | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment