In below I set the variable in application.properties and the tried using default service instance:
private static void testListGCPBucket() String configPath) throws IOException {
Storage storage = StorageOptions.getDefaultInstance().getService();
System.out.println("Buckets:");
Page<Bucket> buckets = storage.list();
for (Bucket bucket : buckets.iterateAll()) {
System.out.println(bucket.toString());
}
System.out.println("completed...");
}
application.config file content:
GOOGLE_APPLICATION_CREDENTIALS=classpath:config.json
But this is not working I am getting below error:
Caused by: com.google.api.client.http.HttpResponseException: 403 Forbidden
POST https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/proj-workload-id-svc-acct@idmp-mii-dev-ddb5.iam.gserviceaccount.com:generateAccessToken
{
"error": {
"code": 403,
"message": "The caller does not have permission",
"errors": [
{
"message": "The caller does not have permission",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1116)
at com.google.auth.oauth2.ImpersonatedCredentials.refreshAccessToken(ImpersonatedCredentials.java:441)
... 36 more
Based on my understanding, if using the DefaultService then it will look at your environment variables. Setting it into your application.properties will not modify your environmental variables.
You will need to point to your file instead. Adding it to your application.properties, it will not be picked up.
Or according to the official documentation, you can use Spring Cloud GCP Starter, and configure it using your application.properties file.
https://docs.spring.io/spring-cloud-gcp/docs/1.1.0.M1/reference/html/spring-cloud-gcp-core.html#_credentials
2 Credentials