Recommended way to generate a presigned url to S3 Bucket in Ruby

后端 未结 2 468
广开言路
广开言路 2021-02-13 18:44

I am trying to generate a pre-signed url on my Rails server to send to the browser, so that the browser can upload to S3.

It seems like aws-sdk-s3 is the ge

2条回答
  •  花落未央
    2021-02-13 19:14

    So, thanks to the tips by @strognjz above, here is what worked for me using `aws-sdk-s3'.

    require 'aws-sdk-s3'
    
    #credentials below for the IAM user I am using
    s3 = Aws::S3::Client.new(
      region:               'us-west-2', #or any other region
      access_key_id:        AWS_ACCESS_KEY_ID,
      secret_access_key:    AWS_SECRET_ACCESS_KEY
    )
    
    signer = Aws::S3::Presigner.new(client: s3)
    url = signer.presigned_url(
      :put_object,
      bucket: S3_BUCKET_NAME,
      key: "${filename}-#{SecureRandom.uuid}"
    )
    

提交回复
热议问题