google storage python JSON client eternal 502 when uploading media to bucket
Question: This is my first time using Google Storage json api, and I am trying to do a
simple image file _UPLOAD_ to a bucket using the python client library. It
seems that I am correctly getting credentials (application default) because I
can _LIST_ the contents of the bucket when I provide the credentials to the
discovery.build() method, and then the same listing call fails when I do not
provide credentials. In terms of the ACL, I am maybe not 100% solid on how to
set the access controls in general, **BUT** I am pretty confident that that is
not the issue, because I have also tried to execute my script on a Compute
Engine VM after configuring it with Cloud Storage API READ and WRITE access in
the console.

Further, I am aware of the documentation's advice to retry promptly (with
'exponential back off') when you get a 502 Bad Gateway response, but I am
getting this error every single time I have tried for a couple hours now. So,
either the Google Storage service is broken in certain ways, or my client is
broken, right? Even further, I am able to push up the jpeg file on the command
line using `gsutil cp ...`, which makes me think I am broken and not Google,
and that the bucket is writable with my default credentials. Sound good?

Here's my very simple client script, which is a jury-rigged from
[this](https://cloud.google.com/storage/docs/json_api/v1/json-api-python-
samples) official sample:

    
    
    import json
    from apiclient import discovery
    from apiclient.http import MediaFileUpload
    from oauth2client.client import GoogleCredentials
    
    credentials = GoogleCredentials.get_application_default()
    service = discovery.build('storage', 'v1', credentials=credentials)
    image = MediaFileUpload('up.jpg', mimetype='image/jpeg')
    req = service.objects().insert_media(bucket='somebucket', media_body=image)
    resp = req.execute()
    print(json.dumps(resp, indent=2))
    

And here is the trace I get:

    
    
    Traceback (most recent call last):
      File "upload.py", line 10, in <module>
        resp = req.execute()
      File "/Envs/py2.7/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper
        return wrapped(*args, **kwargs)
      File "/Envs/py2.7/lib/python2.7/site-packages/googleapiclient/http.py", line 729, in execute
        raise HttpError(resp, content, uri=self.uri)
    googleapiclient.errors.HttpError: <HttpError 502 when requesting https://www.googleapis.com/upload/storage/v1/b/somebucket/o?uploadType=media&alt=media returned "Bad Gateway">
    

I have tried python 2.7 and python 3.5, so I don't think that is the issue
(docs told me google python client is functional in 3.5, but not as well
tested). Also, I have tried a 'resumable upload' variation on this, as per
[here](https://developers.google.com/api-client-
library/python/guide/media_upload), and I get the same, ever-faithful 502 Bad
Gateway. Is google broken? Please help.


Answer: You need to specify a destination name for your object. I believe that you
want the method `service.objects().insert()` instead of
`service.objects().insert_media()`. So the full line would be:

    
    
    req = client.objects().insert(
        bucket='somebucket',
        name='nameOfObject',
        media_body=image)
    

There's [a complete
example](https://cloud.google.com/storage/docs/json_api/v1/objects/insert#examples)
on the objects.insert documentation page.

I'm not sure why you're getting a 502, though. That may be a bug.

