1

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?

RRR
  • 31
  • 2
  • Looks like the pathto the file is incorrect – Jens Jan 11 '21 at 07:02
  • This is not the error, the error is that RRR does not initialize properly the stream. More information here actually https://stackoverflow.com/questions/44121510/how-to-read-a-file-from-google-cloud-storage-in-java – NeatNerd Jan 11 '21 at 07:06
  • @Jens No the path to the file is correct. – RRR Jan 11 '21 at 07:16
  • @NeatNerd The link doesn't help. I can use the standard Java API, but in this case the Spring API needs to be used. The issue happens only with the String API. Can you please point out what stream has not been initiated correctly and why? – RRR Jan 11 '21 at 07:17

0 Answers0