无法使用python将文件上传到Google云存储 - 得到404错误

时间:2014-02-03 16:45:36

标签: python google-cloud-storage

我正在尝试通过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'}

知道我哪里出错了?

1 个答案:

答案 0 :(得分:2)

如果您正在进行可恢复上传,则需要按照此处所述开始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实际上是一个字典,而不是一个字符串。