简体   繁体   English

使用ruby aws-sdk gem和预签名pos的SignatureDoesNotMatch

[英]SignatureDoesNotMatch using ruby aws-sdk gem and presigned pos

I need some help to get the new ruby AWS S3 SDK 2.0 working. 我需要一些帮助才能使新的ruby AWS S3 SDK 2.0正常工作。 I am using the region eu-central-1 so it is required to use the latest signature method v4 for all requests. 我使用的区域为eu-central-1因此要求对所有请求使用最新的签名方法v4

Actually I want to create a presigned post url to use it in combination with jquery-fileupload. 实际上,我想创建一个预签名的发布网址,以与jquery-fileupload结合使用。 I have setup S3 correctly with all access keys, bucket, CORS configuration, etc. But every time I generate a url with the following code 我已经使用所有访问密钥,存储桶,CORS配置等正确设置了S3。但是每次我使用以下代码生成url时

@signer = Aws::S3::Presigner.new
@url = @signer.presigned_url(:put_object, bucket: ENV['S3_BUCKET_NAME'], key: "documents/#{SecureRandom.uuid}/${filename}", acl: :public_read)

which creates the following URL 它创建以下URL

https://project-xxxxx-staging.s3.eu-central-1.amazonaws.com/documents/a253feb0-4c60-4735-8d95-4649c0d3dcb5/%24%7Bfilename%7D?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJVY57LY6XGIRIRHQ%2F20150205%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20150205T140425Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&x-amz-acl=public_read&X-Amz-Signature=ff2fbe233ed7380dc745aa7ba37421d7d8703db0d67208541e500367262a8c51

I get the following error 我收到以下错误

<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>

Does anybody know how to fix this or any hints on the underlying problem? 有人知道如何解决此问题或有关潜在问题的任何提示吗?

My environment I am using: 我正在使用的环境:

ruby '2.2.0' 
gem 'rails', '4.2.0' 
gem 'aws-sdk', '~> 2.0.21.pre'

A pre-signed URL contains a signature computed from headers and query parameters that would/should be sent. 一个预签名的URL包含一个根据标题和将/应该发送的查询参数计算出的签名。 What is likely happening is the jquery uploader is adding a header that Amazon S3 requires to be signed (such as Content-Type). 可能发生的情况是,jquery上载器正在添加Amazon S3要求签名的标头(例如Content-Type)。 You would need to pre-specify this content type when building the presigned url: 构建预签名的url时,您需要预先指定此内容类型:

signer = Aws::S3::Presigner.new signer.presigned_url(:put_object, bucket:'name', key:'key', acl:'public-read', content_type:'...')

Also, the correct canned ACL is "public-read", not :public_read . 另外,正确的罐头ACL是“ public-read”,而不是:public_read This could also possibly be causing an issue. 这也可能引起问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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