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

后端 未结 2 462
广开言路
广开言路 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:05

    This will work using the aws-sdk-s3 gem

    aws_client = 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
    )
    
    
    s3 = Aws::S3::Resource.new(client: aws_client)
    bucket = s3.bucket('bucket-name')
    obj = bucket.object("${filename}-#{SecureRandom.uuid}")
    
    url = obj.presigned_url(:put)
    

    additional http verbs:

    obj.presigned_url(:put)                              
    obj.presigned_url(:head)                    
    obj.presigned_url(:delete)
    
    0 讨论(0)
  • 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}"
    )
    
    0 讨论(0)
提交回复
热议问题