我在通過簽名的url上傳到谷歌云存盤時遇到了問題。
為了找到問題所在,我用ruby寫了一個小測驗腳本。
#
#
# Script to see how upload ror url generation works.
#
#
require "google/cloud/storage"
require 'digest' >。
project_id = "你的project_id"/span>
bucket_name = "你的bucket"。
# commandline...
unless ARGV.length == 1 or ARGV.length == 2
把"用法:gcs_upload_file.rb file_to_upload content_type (default image/jpeg)
"
退出
end
@Content_type = "image/jpeg"/span>
file_path = ARGV[0]
if !ARGV[1].nil?
@Content_type = ARGV[1] 。
end
#檔案檢查 #檔案檢查
if !File.exist? file_path
puts "Unable to open file #{ARGV[0] }
"
退出
end
def hex_to_base64_digest(hexdigest)
[[hexdigest].pack("H*")].pack("m") 。
結束。
# make md5 and file size[/span]。
file_size = File.size file_path
digest = Digest::MD5.file(file_path).hexdigest
Content_md5 = hex_to_base64_digest digest
p file_path
p "file_size = #{file_size}"/span>
p "digest = #{digest}"。
p "Content_md5 = #{Content_md5}"。
gcloud = Google::Cloud.new project_id, "./config/gcs.json"/span>
storage = gcloud.storage
bucket = storage.bucket bucket_name
p "到目前為止還不錯"。
#p bucket.execution
headers = Hash.new
# bug bug bug
# this should be changed into:.
# sign = Google::Cloud::Storage::File::SignerV2.from_bucket bucket, file_path
sign = Google::Cloud::Storage::File::SignerV2.from_bucket bucket, nil。
generated_url = sign.signed_url method: "PUT"/span>。
expires: 120,
headers: headers,
content_type: @Content_type,
content_md5: Content_md5。
issuer: nil。
client_email: nil,
signing_key: nil。
private_key: nil,
signer: nil。
query: nil, query:
p "生成的url:"/span>
p generated_url
commando = "curl -X PUT " generated_url ' "'
commando = " -H "Content-Type: #{@Content_type}" "
#commando = " -H Content-length:4254548 "
commando = " -H Content-length:#{file_size} "
#commando =" -H "Content-MD5: 4IXaimkAX9KasFuXznB3jg==" "/span>
commando = " -H "Content-MD5: #{Content_md5}" "
commando = " --upload-file #{file_path} "
# p 'curl -X PUT "#{generated_url}" -H "Content-Type: image/jpeg" -H "Content-length: 100" --upload-file test.jpg '
#exec('curl -X PUT "#{generated_url}" -H "Content-Type: image/jpeg" -H "Content-length: 100" --upload-file test.jpg ' )
p commando
exec(commando)
p "End of story !"
我覺得這個網址是正確的,因為在其他情況下,我得到一個不正確的簽名資訊。
我發送一個文本檔案hello_world.txt:
Start
你好,世界
你好,世界
你好世界
結束
命令列是:
ruby gcs_upload_file.rb hello_world.txt plain/text
但是上傳失敗了,腳本的輸出結果是:
"hello_world.txt"/span>
"file_size = 51"。
"digest = dcb2d4acdfc14f7fee21e64bff11892c"/span>
"Content_md5 = 3LLUrN/BT3/uIeZL/xGJLA=="/span>
"到目前為止還不錯"。
"生成的url:"。
"https://storage.googleapis.com/my_bucket/[email protected]&Expires=1630665326& Signature=jboMRUyCXsMWvE5ctNRnP4l0lR521PTEP7l1bAJDqBapOPGDT7e1L
iIOWRBP5M4Q6y4K6CN98wAKMhPW7eXx/rHikuGsy20qQ8qsCEtyGvuS3Q2bizG32bw/6VVNSCARpok0hg+yr6cs2LC6CNUHjQ4mH5bFpxD1L6ZdXS2hsC1LsIzuK+imS1RWo950aCBzKs4oPo1cw8lJoLSrg3Y9yUi3eytRr9zarpZECpqgmQynPeGKTS9aEqVlL
B8pLQ/wfBkhlKYC3AAXWcd0RIJjTBTHqVaL3hw/rVYWn/KmU6U3Q/rwRMm3HAvyGYYpn85LxpXXZYh1mfnjjXTSI6mvA="/span>
"curl -X PUT "https://storage.googleapis.com/my_bucket/[email protected]&Expires=1630665326& Signature=jboMRUyCXsMWvE5ctNRnP4l0lR521PTEP7l1bAJ
DqBapOPGDT7e1LiIOWRBP5M4Q6y4K6CN98wAKMhPW7eXx/rHikuGsy20qQ8qsCEtyGvuS3Q2bizG32bw/6VVNSCARpok0hg+yr6cs2LC6CNUHjQ4mH5bFpxD1L6ZdXS2hsC1LNsIzuK+imS1RWo950aCBzKs4oPo1cw8lJoLSrg3Y9yUi3eytRr9zarpZECpqgmQy
nPeGKTS9aEqVlLB8pLQ/wfBkhlKYC3AAXWcd0RIJjTBTHqVaL3hw/rVYWn/KmU6U3Q/rwRMm3HAvyGYYpn85LxpXXZYh1mfnjjXTSI6mvA==" -H "Content-Type: plain/text" -H Content-length:51 -H "Content-MD5: 3LLUrN/BT
3/uIeZL/xGJLA==" --upload-file hello_world.txt "/span>
<?xml version='1.0' encoding='UTF-8'? ><Error><Code>MalformedBucketConfiguration</Code><Message>你提供的XML格式不正確,或者沒有根據我們公布的模式進行驗證</Message><; Details>;F
未能決議水桶創建配置 開始
你好,世界
你好世界
你好,世界
結束
</Details></Error>
為什么會出現這種情況?
我在Windows上用Ruby 2.7作業,
。uj5u.com熱心網友回復:
你生成的簽名的URL https://storage.googleapis.com/my_bucket/?GoogleAccessId=...是不正確的,因為它缺少物件的路徑。這就是為什么錯誤資訊表明GCS認為你在創建一個桶而不是上傳一個物件。
這種情況發生的原因是你在傳遞nil作為路徑。試著指定路徑:
sign = Google::Cloud::Storage::File::SignerV2.from_bucket bucket, "my_object"/span>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/319779.html
標籤:
上一篇:在CURL中發送APi密鑰和密匙
