0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the tip - will try this out. About the headerString, I just named the variable a string, I assumed specifying headers would be easier with a key-value association like dict, do I need to pass a string to the HTTPSConnection.request()?
HTTPSConnection.request does take a dict for the headers argument, so your code is correct. I was just pointing out the name doesn't accurately reflect the type.
Just as followup - I cracked this problem by using python's Requests library. It really simplified things to a large extent for me. Thanks for the replies and comments - they were useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.