I need to read a text file from Google cloud platform through GET request. Am gonna use ajax to make the call from the java script function and display it in my dashboard. I found this endpoint in GCP site GET https://storage.googleapis.com/storage/v1/b/example-bucket/o/foo%2f%3fbar how to add authentication to this?And is this the right endpoint?
2 Answers
The API to obtain the GCS object data is documented here:
https://cloud.google.com/storage/docs/json_api/v1/objects/get
This matches the URL example that you have found. Take note of the alt=media
query property to get the content of the object as opposed to its metadata.
For security, please read the following on access control options:

- 13,794
- 3
- 38
- 60
Yes, that's a perfectly good endpoint (although for that path, you'll need to specify that you want the contents of the file and not the metadata using the alt parameter: https://storage.googleapis.com/storage/v1/b/bucket/o/foo%2fbar?alt=media
)
You could also use the slightly more readable path https://storage.googleapis.com/bucket/foo
.
Auth can be managed in a few ways. Google Cloud APIs most frequently use OAuth. After doing bit of a dance, you end up with a temporary access key. The access key is usually passed as an HTTP header of the pattern Authorization: Bearer some.lengthykey
.
If you have the gcloud
app installed and you've set up your account with it, you can fetch a key like this: gcloud auth print-access-token
.
So, from the Linux command prompt, here's a quick example of downloading an object:
curl -H "Authorization: Bearer `gcloud auth print-access-token`" \
https://storage.googleapis.com/bucket/object`.
There are a number of other schemes to handle auth as well (signed URLs, service account keys, signed policy documents, application default credentials), depending on your needs, but that's the most straightforward.

- 37,021
- 23
- 116
- 145
NoSuchBucket