【问题标题】:Error "SignatureDoesNotMatch". Google Cloud Storage Bucket PUT错误“SignatureDoesNotMatch”。谷歌云存储桶 PUT
【发布时间】:2018-07-22 03:55:27
【问题描述】:

我疯了。

我将 Shrine (https://github.com/janko-m/shrine) 与 Google Cloud Storage (https://github.com/renchap/shrine-google_cloud_storage) 一起使用,但是当我启动 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
image/jpeg
1518399402
/mybucket.appspot.com/7d5e4aad1e3a737fb8d2c59571fdb980.jpg
</StringToSign>
</Error>

我为 presign_endpoint 关注了此信息 (http://shrinerb.com/rdoc/classes/Shrine/Plugins/PresignEndpoint.html),但仍然没有:

class FileUploader < Shrine
  plugin :presign_endpoint, presign_options: -> (request) do
    filename     = request.params["filename"]
    extension    = File.extname(filename)
    content_type = Rack::Mime.mime_type(extension)

    {
      content_type: content_type
    }
  end
end

我试过有没有这个(每次都重新启动 Rails 服务器)。

我哪里错了?

我还尝试使用 Postman 对那个 URL 进行 PUT 并且没有任何 content-type。但还是什么都没有。

我在这里阅读:https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1976 和这里:https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1695

如何在没有 Rails 的情况下尝试?

是否有 REPL(或类似的)可以尝试使用我的凭据和文件?

【问题讨论】:

    标签: ruby file-upload google-cloud-storage pre-signed-url shrine


    【解决方案1】:

    the official docs here 所示,您可以通过多种方式将文件上传到 Google Cloud Storage。

    例如如果你想使用 Ruby 客户端库,你可以使用这个代码:

    # project_id        = "Your Google Cloud project ID"
    # your-bucket-name       = "Your Google Cloud Storage bucket name"
    # local_file_path   = "Path to local file to upload"
    # storage_file_path = "Path to store the file in Google Cloud Storage"
    
    require "google/cloud/storage"
    
    storage = Google::Cloud::Storage.new(project: "your-project_id")
    bucket  = storage.bucket "your-bucket-name"
    
    file = bucket.create_file "local_file_path", "storage_file_path"
    
    puts "Uploaded #{file.name}"
    

    您需要:

    将环境变量 GOOGLE_APPLICATION_CREDENTIALS 设置为包含您的服务帐号密钥的 JSON 文件的文件路径。

    正如您在Cloud Storage Client Libraries docs here 中看到的那样。

    但是,您使用的 github 存储库似乎使用了signed URLs。如果你需要创建一个签名的 URL,你有 the instructions here。它已经存在于official repo 中的the signed_urls method for Ruby

    无论如何,您遇到的错误是因为签名的网址有问题。确切地说,您在 7 天前引用了一个名为 Fix presigned uploads 的回购协议 (https://github.com/renchap/shrine-google_cloud_storage)。看起来这个提交应该修复“PUT”上传(在“GET”被签名之前,因此它不起作用)。不过我没有尝试过,所以我不知道它是否真的有效。

    【讨论】:

    • 那个修复是在我的问题之后。
    猜你喜欢
    • 2016-01-31
    • 2019-08-24
    • 2020-06-01
    • 1970-01-01
    • 2016-08-25
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    相关资源
    最近更新 更多