0

I'm loosing my mind.

I'm using Shrine (https://github.com/janko-m/shrine) with Google Cloud Storage (https://github.com/renchap/shrine-google_cloud_storage), but when I start the PUT call I get this:

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

I followed this info (http://shrinerb.com/rdoc/classes/Shrine/Plugins/PresignEndpoint.html) for presign_endpoint, but still nothing:

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

I tried with and without this (restarting the Rails server everytime).

Where am I wrong?

I also tried with Postman with a PUT to that URL and withtout any content-type. But still nothing.

I read here: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1976 and here: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1695

How can I try without Rails?

Is there a REPL (or similar) to try with my credentials and with a file?

1 Answers1

0

You have several options for just uploading a file to Google Cloud Storage as you can see in the official docs here.

For example if you want to use Ruby Client Library, you can use this code:

# 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}"

You will need to:

Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key.

as you can see in the Cloud Storage Client Libraries docs here.

However, it looks like the github repo you are using makes use of signed URLs. If you need to create a signed URL you have the instructions here. It exists already the signed_urls method for Ruby in the official repo.

Anyway, the error you were getting is because there was something wrong being made with the signed urls. And precisely there was a commit to the repo you were referencing (https://github.com/renchap/shrine-google_cloud_storage) 7 days ago with the name Fix presigned uploads. It looks like this commit should fix the "PUT" upload (before a "GET" was being signed, therefore it wouldn't work). Still I didn't try it so I don't know if it is actually working.

VictorGGl
  • 1,848
  • 10
  • 15