Skip to content

Instantly share code, notes, and snippets.

@danielwestendorf
Last active October 2, 2016 21:54
Show Gist options
  • Save danielwestendorf/8fb4023785e605112c4123192964d846 to your computer and use it in GitHub Desktop.
Save danielwestendorf/8fb4023785e605112c4123192964d846 to your computer and use it in GitHub Desktop.
# 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