0

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?

Kolban
  • 13,794
  • 3
  • 38
  • 60
Manya
  • 11
  • 4

2 Answers2

1

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:

https://cloud.google.com/storage/docs/access-control/

Kolban
  • 13,794
  • 3
  • 38
  • 60
0

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.

Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145
  • Thanks!! Actually my text file is inside multiple object(folders) under the bucket So do I have to give the entire path in the place of object? I get 400 error when I give entire path or even just give the file name in place of object And can this be executed from post man once using OAuth authentication? – Manya Nov 20 '19 at 19:33
  • For the "/storage/v1" path, remember to escape slashes in the object name. For the other path, it's not necessary. Also, I'm not famliar with Postman, but I see that there are some articles about using it with this style of auth: https://medium.com/@pnatraj/google-cloud-api-with-postman-f4cf070e665f – Brandon Yarbrough Nov 20 '19 at 20:16
  • I tried the curl command from putty but file gets downloaded with the below content NoSuchBucket The specified bucket does not exist. My bucket does exist – Manya Nov 21 '19 at 06:10
  • Just to be clear, you got "NoSuchBucket" from a URL beginning "https://storage.googleapis.com/nameOfYourBucketHere/" ? – Brandon Yarbrough Nov 21 '19 at 18:10
  • Sorry,I had given the wrong bucket name.This was just the start for me, am actually trying to read the file through the GCP libraries and saw your solution in the this link https://stackoverflow.com/questions/44121510/how-to-read-a-file-from-google-cloud-storage-in-java. Am getting java.security.cert.CertificateException: No subject alternative names matching IP address in this line Blob blob = storage.get(BUCKET_NAME, OBJECT_NAME); Can you help me with what could be the issue? – Manya Nov 24 '19 at 09:25
  • Well, that's a weird one. You definitely shouldn't be running into invalid certificates. Is it possible that you are behind a corporate firewall or equivalent that's trying to swap out a certificate and is misconfigured? – Brandon Yarbrough Nov 25 '19 at 07:14