Last active
October 2, 2016 21:54
-
-
Save danielwestendorf/8fb4023785e605112c4123192964d846 to your computer and use it in GitHub Desktop.
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
# Handle file uploads to S3 | |
class UploadsController < ApplicationController | |
def new | |
@upload = Upload.new | |
end | |
# Step 1: POST to app to get the presignature URL | |
def create | |
@upload = Upload.create!(upload_params) | |
render json: signature # Step 2: Return the presigned URL after doing some validations | |
rescue | |
head :forbidden | |
end | |
private | |
def upload_params | |
params.permit(:size, :filename) | |
end | |
def signature | |
signature = @upload.bucket.presigned_post(signature_options) | |
{ | |
url: signature.url, | |
fields: signature.fields, | |
accessUrl: @upload.url | |
} | |
end | |
def signature_options | |
{ | |
key: @upload.key, | |
acl: "private", | |
content_length_range: 0..2.megabytes # Enforce a filesize limit | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment