siddharth
siddharth

Reputation: 1

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

I am trying a upload a file to Google Cloud Storage via a python script but keep getting a 404 error! I am sure I am not trying to reference a non-available resource. My code snippet is:

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)

Where

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

Any idea where I'm going wrong?

Upvotes: 0

Views: 754

Answers (1)

Travis Hobrla
Travis Hobrla

Reputation: 5511

If you're doing a resumable upload, you'll need to start with a POST as described here: https://developers.google.com/storage/docs/json_api/v1/how-tos/upload#resumable

However, for a 47-byte object, you can use a simple upload, which will be much ... simpler. Instructions are here: https://developers.google.com/storage/docs/json_api/v1/how-tos/upload#simple

It should be easy enough for you to replace the appropriate lines in your code with:

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

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

As an aside, in your code, headerString is actually a dict, not a string.

Upvotes: 2

Related Questions