简体   繁体   中英

Creating Amazon S3 presigned url for public access from Ruby on Rails

I have exactly the same issue as this question but my code generates the following error when I test in Postman:

AccessDenied There were headers present in the request which were not signed.

在此处输入图片说明

This is the ruby code that creates the url:

require 'aws-sdk'
class Api::AmazonController < ApiController
  before_action :set_credentials

  def presign
    if params[:filename] # && params[:type]
      s3 = Aws::S3::Resource.new(region:'eu-west-2')
      bucket = Rails.application.secrets.s3_bucket_name.to_s
      obj = s3.bucket(bucket).object(params[:filename])
      url = obj.presigned_url(:put, acl:'public-read') #, content_type: params[:type], expires: 10*60) 
      render json: url
    else
      render json: { error: 'Invalid params' }
    end
  end

  private

    def set_credentials
      Aws.config[:credentials] = Aws::Credentials.new(Rails.application.secrets.aws_access_key_id, Rails.application.secrets.aws_secret_access_key)
      # Aws.config.update({region: 'eu-west-2'})
    end
end

If I remove the x-amz-acl header from the ruby code:

url = obj.presigned_url(:put)

Then it works and I can put a file. So what am I missing?

The solution is to NOT add the header in Postman. Leaving it in the ruby code and removing 'x-amz-acl' parameter from Postman fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM