簡體   English   中英

無法使用python將文件上傳到Google雲存儲 - 得到404錯誤

[英]Unable to upload file to Google cloud storage using python - get a 404 error

我正在嘗試通過python腳本將文件上傳到Google雲端存儲,但一直收到404錯誤! 我確信我不是在嘗試引用不可用的資源。 我的代碼片段是:

uploadFile = open("testUploadFile.txt", "r")
httpObj = httplib.HTTPSConnection("googleapis.com", timeout = 10)
httpObj.request("PUT", requestString, uploadFile, headerString)
uploadResponse = httpObj.getresponse()
print "Request string is:" + requestString
print "Return status:" + str(uploadResponse.status)
print "Reason:" + str(uploadResponse.reason)

哪里

requestString = /upload/storage/v1beta2/b/bucket_id_12345678/o?uploadType=resumable&name=1%2FtestUploadFile.txt%7Calm_1391258335&upload_id=AbCd-1234
headerString = {'Content-Length': '47', 'Content-Type': 'text/plain'}

知道我哪里錯了嗎?

如果您正在進行可恢復上傳,則需要按照此處所述開始POST: https//developers.google.com/storage/docs/json_api/v1/how-tos/upload#resumable

但是,對於47字節的對象,您可以使用簡單的上傳,這將更加簡單。 說明如下: https//developers.google.com/storage/docs/json_api/v1/how-tos/upload#simple

您可以很容易地用以下代碼替換代碼中的相應行:

httpObj.request("POST", requestString, uploadFile, headerString)

requestString = /upload/storage/v1beta2/b/bucket_id_12345678/o?uploadType=media&name=1%2FtestUploadFile.txt%7Calm_1391258335

順便說一下,在你的代碼中,headerString實際上是一個字典,而不是字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM