I am trying to use Spring Boot to read a file stored in GCS using this tutorial: https://codelabs.developers.google.com/codelabs/spring-cloud-gcp-gcs#4
My code as follows:
Resource gcsFile = resourceLoader.getResource(gcsPath);
//InputStream inputStream = gcsFile.getInputStream();
return StreamUtils.copyToString(
gcsFile.getInputStream(),
StandardCharsets.UTF_8);
resourceLoader
is injected as a dependency using @Autowired
annotation. The gcsPath
variable is a String to the gcs file location (example: gs://REPLACE_WITH_YOUR_BUCKET/my-file.txt
).
I get the below exception:
java.lang.NullPointerException: null
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:878)
at com.google.cloud.storage.BlobId.of(BlobId.java:109)
at org.springframework.cloud.gcp.storage.GoogleStorageResource.getBlobId(GoogleStorageResource.java:108)
at org.springframework.cloud.gcp.storage.GoogleStorageResource.resolve(GoogleStorageResource.java:113)
at org.springframework.cloud.gcp.storage.GoogleStorageResource.getInputStream(GoogleStorageResource.java:167)
During debugging, I found that the following line is what triggers this exception:
InputStream inputStream = gcsFile.getInputStream();
I have not been able to figure out how and why this is happening. Can someone please help?