diff --git a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java index 0e16a81e..c25d1e7e 100644 --- a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java +++ b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java @@ -963,8 +963,13 @@ public String toString() { public boolean requesterPays(String bucketName) { initStorage(); try { - // instead of true/false, this method returns true/null. - Boolean isRP = storage.get(bucketName).requesterPays(); + final Bucket bucket = storage.get(bucketName); + // If the bucket doesn't exist it can't be requester pays. + if (bucket == null) { + return false; + } + // instead of true/false, this method returns true/null + Boolean isRP = bucket.requesterPays(); return isRP != null && isRP.booleanValue(); } catch (StorageException ex) { if ("userProjectMissing".equals(ex.getReason())) { diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java index 55a341d4..6ea1efd7 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java @@ -49,6 +49,7 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; @@ -327,6 +328,24 @@ public void testAutodetectWhenNotRequesterPays() throws IOException { ""); } + @Test + public void assertNoCrashWhenBucketDoesntExist() throws URISyntaxException { + CloudStorageConfiguration config = + CloudStorageConfiguration.builder() + .autoDetectRequesterPays(true) + .userProject(project) + .usePseudoDirectories(true) + .build(); + + final String bucketThatDoesntExist = "abuckethatdoesntexist"; + final String subPath = "hello"; + CloudStorageFileSystem testBucket = + CloudStorageFileSystem.forBucket(bucketThatDoesntExist, config, storageOptions); + final CloudStoragePath aPathThatDoesntExist = testBucket.getPath(subPath); + Assert.assertEquals( + aPathThatDoesntExist.toUri().toString(), "gs://" + bucketThatDoesntExist + "/" + subPath); + } + @Test public void testAutoDetectNoUserProject() throws IOException { CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, "");