1

Following the official documentation I've been trying to read a sample file I stored within GCS, but so far the only thing I've got is a NullPointerException

InputStream sampleStream() {
    String bucketName = "sample";
    String fileName = "sample";

    log.info("Loading file from GCS [bucket: {}, key: {}]", bucketName, fileName);
    try {
        GcsInputChannel readChannel = gcsService().openReadChannel(new GcsFilename(bucketName, fileName), 0);
        return Channels.newInputStream(readChannel);
    } catch(Exception ex) {
        log.error("Error loading file...", ex);
    }
}

private GcsService gcsService() {
    return GcsServiceFactory
        .createGcsService(new RetryParams.Builder()
            .initialRetryDelayMillis(10).retryMaxAttempts(10)
            .totalRetryPeriodMillis(15000)
            .build()
        );
}

But still retrying all the time...

RetryHelper(14.16 ms, 1 attempts, com.google.appengine.tools.cloudstorage.SimpleGcsInputChannelImpl$1@278ef74): Attempt #1 failed [java.io.IOException: java.lang.NullPointerException], sleeping for 9 ms

The NullPointerException is thrown whenever .read() is invoked within the resulting InputStream.

Am I missing credentials (or the path to my local .json auth file) somewhere or should I take into account more things?

Lenny D.
  • 288
  • 1
  • 4
  • 14
  • I am facing the same issue, in case of unit cases in spring boot reading from a bucket getting NPE. Source code is working but unit test cases could not read from storage. – Namjith Aravind Jul 20 '20 at 18:21

1 Answers1

-1

Apparently wrong assumption in the code, returns null object, which causes NPE. This code is easier and very clear I add the link:

How to read a file from Google Cloud Storage in Java