0

I am trying to read a file form google cloud storage using the following API, in a spring boot application: GET https://storage.googleapis.com/storage/v1/b/bucket/o/object This is my code:

@Component("GetObjFromGCP")
class GetObjFromGCP{
    

    
    public void readFromFile() throws UnirestException{

        HttpResponse<String> jsonResponse 
          = Unirest.get("https://storage.googleapis.com/storage/v1/b/sagessapp_test/o/apache%20camel%20notes.txt?alt=media")
          .header("accept", "application/json")
          .asString();

        System.out.println(jsonResponse.getBody());
    }
}

However I am getting this error:

Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.

I have a json file containing the service account credentials and the appropriate role to read files from my bucket. the application.properties file: spring.cloud.gcp.credentials.location=classpath:/gcp-credentials.json

So any idea how can I solve this error? Thank you

Jenny
  • 375
  • 4
  • 6
  • 25

1 Answers1

0

I recommend you use one of Google's client libraries; these provide a higher-level abstraction and handle authentication too. See Cloud Storage client libraries (Java)

If you do not want to use the client libraries, to use the low-level REST API directly, you will need to include an Authorization header. In the case of Google (Cloud Platform), the easiest way to acquire an access token is to use gcloud (see below):

(either) For testing using your own account credentials:

TOKEN=$(gcloud auth application-default print-access-token)

(or) To use the service account:

gcloud auth activate-service-account \
--key-file=/path/to/your/key.json

TOKEN=$(gcloud auth print-access-token)

You'll need to add the header Authorization: Bearer ${TOKEN} (replacing ${TOKEN} with the value).

BUT Please use a client library.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88