0

我正在失去理智。

我将 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>

我关注了这个信息(http://shrinerb.com/rdoc/classes/Shrine/Plugins/PresignEndpoint.htmlpresign_endpoint,但仍然没有:

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 服务器)。

我哪里错了?

我还尝试使用带有 PUT 到该 URL 并且没有任何content-type的 Postman 。但还是一无所获。

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

如果没有 Rails,我该如何尝试?

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

4

1 回答 1

0

如您在此处的官方文档中所见,您可以选择多种方式将文件上传到 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 文档中看到的那样。

但是,您使用的 github 存储库似乎使用了签名 URL。如果您需要创建签名 URL,请参阅此处的说明官方 repo中已经存在Rubysigned_urls方法

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

于 2018-02-20T14:20:58.160 回答