Class: BgS3uploadable::SignedUrlsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/bg_s3uploadable/signed_urls_controller.rb

Instance Method Summary collapse

Instance Method Details

#showObject



3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/controllers/bg_s3uploadable/signed_urls_controller.rb', line 3

def show
  policy = s3_upload_policy_document

  render json: {
    policy: policy,
    signature: s3_upload_signature(policy),
    key: "uploads/#{SecureRandom.uuid}/#{params[:doc][:title]}",
    success_action_redirect: "/",
    upload_endpoint: "https://#{ENV['S3_BUCKET']}.s3.amazonaws.com",
    :AWSAccessKeyID => ENV['AWS_ACCESS_KEY_ID'],
  }
end

#uploaded_imageObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/bg_s3uploadable/signed_urls_controller.rb', line 16

def uploaded_image
  unless params[:key] =~ /\Auploads\//
    render json: { error: 'Access denied' }, status: 403
    return
  end

  s3 = AWS::S3.new
  bucket = s3.buckets[ENV['S3_BUCKET']]
  obj = bucket.objects[params[:key]]

  presign = AWS::S3::PresignV4.new obj

  redirect_to presign.presign(:get, expires: 5.minutes.from_now.to_i, secure: true).to_s
end