SignatureDoesNotMatch Ruby on Rails Google存储签名URL

时间:2018-04-17 10:10:53

标签: ruby-on-rails ruby google-cloud-storage pre-signed-url

我使用gem' google-cloud-storage'与Google存储建立了联系。回报是https://github.com/GoogleCloudPlatform/google-cloud-ruby/tree/master/google-cloud-storage

signed_url方法的示例我无法在repo中的一个示例中找到它,这里是第916行: https://github.com/GoogleCloudPlatform/google-cloud-ruby/blob/master/google-cloud-storage/lib/google/cloud/storage/bucket.rb

我设法为特定文件获取方法GET的签名URL,但是当我尝试在signed_url上为文件执行PUT时,它给出了一个响应体,如下所示:

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

application/x-www-form-urlencoded
1523909074
/bucket_name/logo1</StringToSign>
</Error>

PUT(不工作)的示例代码:

require 'google/cloud/storage'
storage = Google::Cloud::Storage.new(
    project_id: 'projectId',
    credentials: './config/keyfile.json'
)

bucket = storage.bucket 'bucket_name'
signed_url = bucket.signed_url 'logo1.png',
                   method: 'PUT',
                   content_type: 'image/png',
                   expires: 300 # 5 minutes from now

我尝试使用和不使用content_type。我也尝试了content_type:&#39; *&#39;或者&#39; **&#39;或者&#39; image / *&#39;或者&#39;&#39;。 我在Postman和使用primeng上传组件的Angular应用程序中尝试了PUT请求。 需要一点帮助。感谢。

1 个答案:

答案 0 :(得分:0)

一切都很好。我忘了在请求的标题中添加Content-Type。 我认为这是在签名的网址中隐含的,但没有。