You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Easily generate a form that allows you to upload directly to Amazon S3.
6
-
Multi file uploading supported by jquery-fileupload.
5
+
This is a fork of [s3_direct_upload](https://github.com/waynehoover/s3_direct_upload).
7
6
8
-
Code extracted from Ryan Bates' [gallery-jquery-fileupload](https://github.com/railscasts/383-uploading-to-amazon-s3/tree/master/gallery-jquery-fileupload).
7
+
It's also has code from [refile](https://github.com/elabs/refile), mainly
8
+
related with the frontend.
9
+
10
+
The general idea of this gem is to be backend agnostic. I'd liked how
11
+
s3_direct_upload interact with S3 and I'd liked how refile interact with the
12
+
form and the backend, so I made a hybrid.
13
+
14
+
For now it's only works with [paperclip](https://github.com/thoughtbot/paperclip).
9
15
10
16
## Installation
11
17
Add this line to your application's Gemfile:
@@ -40,47 +46,57 @@ Make sure your AWS S3 CORS settings for your bucket look something like this:
40
46
```
41
47
In production the AllowedOrigin key should be your domain.
42
48
43
-
Add the following js and css to your asset pipeline:
49
+
Add the following js to your asset pipeline:
44
50
45
51
**application.js.coffee**
46
52
```coffeescript
47
53
#= require s3-upnow
48
54
```
49
55
50
-
**application.css**
51
-
```css
52
-
//= require s3-upnow_progress_bars
53
-
```
54
-
55
56
## Usage
56
-
Create a new view that uses the form helper `s3_uploader_form`:
57
+
Create a new view that uses the form helper `s3_upnow_field`:
57
58
```ruby
58
-
<%= s3_uploader_form callback_url: model_url, callback_param: "model[image_url]", id: "s3-uploader" do %>
59
-
<%= file_field_tag :file, multiple: true %>
60
-
<% end %>
59
+
= simple_form_for @modeldo|f|
60
+
= f.s3_upnow_field :avatar
61
+
= f.button :submit
61
62
```
62
63
63
-
*It is required that the file_field_tag is named 'file'.
64
-
*A unique :id should be added to file_field_tag if there is many 's3_uploader_form' in the page
64
+
It will create a file field as well a hidden field with `s3_key` suffix. The
I just have one field, and I want to submit it right after I receive the
89
+
upload:success event. I don't know how this will be used in frontends, so I
90
+
want to have minimal policies for now. So, be warned and write your own code :)
91
+
92
+
And that's it! All your models that have `has_attached_file :avatar` will copy
93
+
the file specified by s3_key to the path expected by paperclip.
82
94
83
95
## Options for form helper
96
+
97
+
All these options are still used in code, but the callbacks options doesn't have
98
+
any effect. I will polish it when I have some tests written.
99
+
84
100
*`callback_url:` No default. The url that is POST'd to after file is uploaded to S3. If you don't specify this option, no callback to the server will be made after the file has uploaded to S3.
85
101
*`callback_method:` Defaults to `POST`. Use PUT and remove the multiple option from your file field to update a model.
86
102
*`callback_param:` Defaults to `file`. Parameter key for the POST to `callback_url` the value will be the full s3 url of the file. If for example this is set to "model[image_url]" then the data posted would be `model[image_url] : http://bucketname.s3.amazonws.com/filename.ext`
@@ -93,158 +109,10 @@ Optionally, you can also place this template in the same view for the progress b
93
109
*`data:` Optional html data attribute hash.
94
110
*`bucket:` Optional (defaults to bucket used in config).
### Example to persist the S3 url in your rails app
113
-
It is recommended that you persist the url that is sent via the POST request (to the url given to the `callback_url` option and as the key given in the `callback_param` option).
114
-
115
-
One way to do this is to make sure you have `resources model` in your routes file, and add a `s3_url` (or something similar) attribute to your model. Then make sure you have the create action in your controller for that model that saves the url from the callback_param.
116
-
117
-
You could then have your create action render a javascript file like this:
118
-
**create.js.erb**
119
-
```ruby
120
-
<% if @model.new_record? %>
121
-
alert("Failed to upload model: <%= j @model.errors.full_messages.join(', ').html_safe %>");
So that javascript code would be executed after the model instance is created, without a page refresh. See [@rbates's gallery-jquery-fileupload](https://github.com/railscasts/383-uploading-to-amazon-s3/tree/master/gallery-jquery-fileupload)) for an example of that method.
127
-
128
-
Note: the POST request to the rails app also includes the following parameters `filesize`, `filetype`, `filename` and `filepath`.
129
-
130
-
### Advanced Customizations
131
-
Feel free to override the styling for the progress bars in s3-upnow_progress_bars.css, look at the source for inspiration.
132
-
133
-
Also feel free to write your own js to interface with jquery-file-upload. You might want to do this to do custom validations on the files before it is sent to S3 for example.
134
-
To do this remove `s3-upnow` from your application.js and include the necessary jquery-file-upload scripts in your asset pipeline (they are included in this gem automatically):
135
-
```cofeescript
136
-
#= require jquery-fileupload/basic
137
-
#= require jquery-fileupload/vendor/tmpl
138
-
```
139
-
Use the javascript in `s3-upnow` as a guide.
140
-
141
-
142
-
## Options for S3Upload jQuery Plugin
143
-
144
-
* `path:` manual path for the files on your s3 bucket. Example: `path/to/my/files/on/s3`
145
-
Note: Your path MUST start with the option you put in your form builder for `key_starts_with`, or else you will get S3 permission errors. The file path in your s3 bucket will be `path + key`.
146
-
* `additional_data:` You can send additional data to your rails app in the persistence POST request. This would be accessible in your params hash as `params[:key][:value]`
147
-
Example: `{key: value}`
148
-
* `remove_completed_progress_bar:` By default, the progress bar will be removed once the file has been successfully uploaded. You can set this to `false` if you want to keep the progress bar.
149
-
* `remove_failed_progress_bar:` By default, the progress bar will not be removed when uploads fail. You can set this to `true` if you want to remove the progress bar.
150
-
* `before_add:` Callback function that executes before a file is added to the queue. It is passed file object and expects `true` or `false` to be returned. This could be useful if you would like to validate the filenames of files to be uploaded for example. If true is returned file will be uploaded as normal, false will cancel the upload.
151
-
* `progress_bar_target:` The jQuery selector for the element where you want the progress bars to be appended to. Default is the form element.
152
-
* `click_submit_target:` The jQuery selector for the element you wish to add a click handler to do the submitting instead of submiting on file open.
153
-
154
-
### Example with all options
155
-
```coffeescript
156
-
jQuery ->
157
-
$("#myS3Uploader").S3Uploader
158
-
path:'path/to/my/files/on/s3'
159
-
additional_data: {key:'value'}
160
-
remove_completed_progress_bar:false
161
-
before_add: myCallBackFunction # must return true or false if set
162
-
progress_bar_target: $('.js-progress-bars')
163
-
click_submit_target: $('.submit-target')
164
-
```
165
-
### Example with single file upload bar without script template
166
-
167
-
This demonstrates how to use progress_bar_target and allow_multiple_files (only works with false option - single file) to show only one progress bar without script template.
168
-
169
-
```coffeescript
170
-
jQuery->
171
-
$("#myS3Uploader").S3Uploader
172
-
progress_bar_target:$('.js-progress-bars')
173
-
allow_multiple_files:false
174
-
```
175
-
176
-
Target for progress bar
177
-
178
-
```html
179
-
<divclass="upload js-progress-bars">
180
-
<divclass="progress">
181
-
<divclass="bar"> </div>
182
-
</div>
183
-
</div>
184
-
```
185
-
186
-
187
-
188
-
189
-
### Public methods
190
-
You can change the settings on your form later on by accessing the jQuery instance:
191
-
192
-
```coffeescript
193
-
jQuery->
194
-
v=$("#myS3Uploader").S3Uploader()
195
-
...
196
-
v.path("new/path/") #only works when the key_starts_with option is blank. Not recommended.
197
-
v.additional_data("newdata")
198
-
```
199
-
200
-
### Javascript Events Hooks
201
-
202
-
#### First upload started
203
-
`s3_uploads_start` is fired once when any batch of uploads is starting.
204
-
```coffeescript
205
-
$('#myS3Uploader').bind's3_uploads_start', (e) ->
206
-
alert("Uploads have started")
207
-
```
208
-
209
-
#### Successfull upload
210
-
When a file has been successfully uploaded to S3, the `s3_upload_complete` is triggered on the form. A `content` object is passed along with the following attributes :
211
-
212
-
*`url` The full URL to the uploaded file on S3.
213
-
*`filename` The original name of the uploaded file.
214
-
*`filepath` The path to the file (without the filename or domain)
215
-
*`filesize` The size of the uploaded file.
216
-
*`filetype` The type of the uploaded file.
217
-
218
-
This hook could be used for example to fill a form hidden field with the returned S3 url :
When an error occured during the transferm the `s3_upload_failed` is triggered on the form with the same `content` object is passed for the successful upload with the addition of the `error_thrown` attribute. The most basic way to handle this error would be to display an alert message to the user in case the upload fails :
IE file uploads are working but with a couple caveats.
280
-
281
-
* The before_add callback doesn't work.
282
-
* The progress bar doesn't work on IE.
283
-
284
-
But IE should still upload your files fine.
285
-
144
+
Alternately, if you'd prefer for S3 to delete your old uploads automatically,
145
+
you can do so by setting your bucket's [Lifecycle Configuration](http://docs.aws.amazon.com/AmazonS3/latest/UG/LifecycleConfiguration.html).
286
146
287
147
## Contributing / TODO
288
-
This is just a simple gem that only really provides some javascript and a form helper.
289
-
This gem could go all sorts of ways based on what people want and how people contribute.
148
+
This is just a simple gem that only really provides some javascript and a form
149
+
helper. This gem could go all sorts of ways based on what people want and how
150
+
people contribute.
151
+
290
152
Ideas:
291
153
* More specs!
292
154
* More options to control file types, ability to batch upload.
293
-
* More convention over configuration on rails side
294
-
* Create generators.
295
-
* Model methods.
296
-
* Model method to delete files from s3
155
+
* More convention over configuration on frontend side
297
156
298
157
299
158
## Credit
300
-
This gem is basically a small wrapper around code that [Ryan Bates](http://github.com/rbates) wrote for [Railscast#383](http://railscasts.com/episodes/383-uploading-to-amazon-s3). Most of the code in this gem was extracted from [gallery-jquery-fileupload](https://github.com/railscasts/383-uploading-to-amazon-s3/tree/master/gallery-jquery-fileupload).
301
-
302
-
Thank you Ryan Bates!
303
-
304
-
This code also uses the excellecnt [jQuery-File-Upload](https://github.com/blueimp/jQuery-File-Upload), which is included in this gem by its rails counterpart [jquery-fileupload-rails](https://github.com/tors/jquery-fileupload-rails)
159
+
This gem is a hybrid of [s3_direct_upload](https://github.com/waynehoover/s3_direct_upload)
160
+
and [refile](https://github.com/elabs/refile). Maybe it have more personality in
0 commit comments