Skip to content

Commit

Permalink
readme and url s3_uploader_url helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ncri committed Jun 26, 2015
1 parent 9aef36d commit a98dc01
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://travis-ci.org/waynehoover/s3_direct_upload.png)](https://travis-ci.org/waynehoover/s3_direct_upload)

Easily generate a form that allows you to upload directly to Amazon S3.
Easily upload files directly to Amazon S3.
Multi file uploading supported by jquery-fileupload.

Code extracted from Ryan Bates' [gallery-jquery-fileupload](https://github.com/railscasts/383-uploading-to-amazon-s3/tree/master/gallery-jquery-fileupload).
Expand Down Expand Up @@ -53,10 +53,10 @@ Add the following js and css to your asset pipeline:
```

## Usage
Create a new view that uses the form helper `s3_uploader_form`:
Use the `s3_uploader` helper to add an s3 upload file field to your view:
```ruby
<%= s3_uploader_form callback_url: model_url, callback_param: "model[image_url]", id: "s3-uploader" do %>
<%= file_field_tag :file, multiple: true %>
<%= s3_uploader callback_url: model_url, callback_param: "model[image_url]", id: "s3-uploader" do %>
<%= file_field_tag :file, multiple: true, data: { url: s3_uploader_url } %>
<% end %>
```

Expand Down Expand Up @@ -105,7 +105,7 @@ Optionally, you can also place this template in the same view for the progress b
id: "s3-uploader",
class: "upload-form",
data: {:key => :val} do %>
<%= file_field_tag :file, multiple: true %>
<%= file_field_tag :file, multiple: true, data: { url: s3_uploader_url } %>
<% end %>
```
Expand Down
10 changes: 4 additions & 6 deletions lib/s3_direct_upload/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ def s3_uploader_form(options = {}, &block)

alias_method :s3_uploader, :s3_uploader_form

def s3_uploader_url ssl = true
S3DirectUpload.config.url || "http#{ssl ? 's' : ''}://#{S3DirectUpload.config.region || "s3"}.amazonaws.com/#{S3DirectUpload.config.bucket}/"
end

class S3Uploader
def initialize(options)
@key_starts_with = options[:key_starts_with] || "uploads/"
@options = options.reverse_merge(
aws_access_key_id: S3DirectUpload.config.access_key_id,
aws_secret_access_key: S3DirectUpload.config.secret_access_key,
bucket: options[:bucket] || S3DirectUpload.config.bucket,
region: S3DirectUpload.config.region || "s3",
url: S3DirectUpload.config.url,
ssl: true,
acl: "public-read",
expiration: 10.hours.from_now.utc.iso8601,
Expand Down Expand Up @@ -59,10 +61,6 @@ def key
@key ||= "#{@key_starts_with}{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}"
end

def url
@options[:url] || "http#{@options[:ssl] ? 's' : ''}://#{@options[:region]}.amazonaws.com/#{@options[:bucket]}/"
end

def policy
Base64.encode64(policy_data.to_json).gsub("\n", "")
end
Expand Down

0 comments on commit a98dc01

Please sign in to comment.