Skip to content

Commit afe441d

Browse files
Allow path in URIs passed to newFileSystem (#1470)
* This makes it easier for users who start with a URI describing a full path to get a FileSystem that can work with that path, since they no longer have to needlessly remove the path from the URI. Note that Oracle's description of newFileSystem [1] puts no restriction on the passed URI. [1] https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystems.html#newFileSystem(java.net.URI,%20java.util.Map)
1 parent f8aa211 commit afe441d

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

java-storage-nio/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,15 @@ public CloudStorageFileSystem getFileSystem(URI uri) {
170170
}
171171

172172
/**
173-
* Returns Cloud Storage file system, provided a URI with no path, e.g. {@code gs://bucket}.
173+
* Returns Cloud Storage file system, provided a URI, e.g. {@code gs://bucket}.
174+
* The URI can include a path component (that will be ignored).
174175
*
175176
* @param uri bucket and current working directory, e.g. {@code gs://bucket}
176177
* @param env map of configuration options, whose keys correspond to the method names of
177178
* {@link CloudStorageConfiguration.Builder}. However you are not allowed to set the working
178179
* directory, as that should be provided in the {@code uri}
179-
* @throws IllegalArgumentException if {@code uri} specifies a user, query, fragment, or scheme is
180-
* not {@value CloudStorageFileSystem#URI_SCHEME}
180+
* @throws IllegalArgumentException if {@code uri} specifies a port, user, query, or fragment, or
181+
* if scheme is not {@value CloudStorageFileSystem#URI_SCHEME}
181182
*/
182183
@Override
183184
public CloudStorageFileSystem newFileSystem(URI uri, Map<String, ?> env) {
@@ -191,11 +192,10 @@ public CloudStorageFileSystem newFileSystem(URI uri, Map<String, ?> env) {
191192
CloudStorageFileSystem.URI_SCHEME, uri);
192193
checkArgument(
193194
uri.getPort() == -1
194-
&& isNullOrEmpty(uri.getPath())
195195
&& isNullOrEmpty(uri.getQuery())
196196
&& isNullOrEmpty(uri.getFragment())
197197
&& isNullOrEmpty(uri.getUserInfo()),
198-
"GCS FileSystem URIs mustn't have: port, userinfo, path, query, or fragment: %s",
198+
"GCS FileSystem URIs mustn't have: port, userinfo, query, or fragment: %s",
199199
uri);
200200
CloudStorageUtil.checkBucket(uri.getHost());
201201
initStorage();

java-storage-nio/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
import java.nio.file.OpenOption;
5555
import java.nio.file.Path;
5656
import java.nio.file.Paths;
57+
import java.util.HashMap;
5758
import java.util.List;
59+
import java.util.Map;
5860

5961
/**
6062
* Unit tests for {@link CloudStorageFileSystemProvider}.
@@ -644,6 +646,12 @@ public void testProviderEquals() {
644646
assertThat(path1.getFileSystem().provider()).isNotEqualTo(path3.getFileSystem().provider());
645647
}
646648

649+
@Test
650+
public void testNewFileSystem() throws IOException {
651+
Map<String,String> env = new HashMap<>();
652+
FileSystems.newFileSystem(URI.create("gs://bucket/path/to/file"), env);
653+
}
654+
647655
private static CloudStorageConfiguration permitEmptyPathComponents(boolean value) {
648656
return CloudStorageConfiguration.builder().permitEmptyPathComponents(value).build();
649657
}

0 commit comments

Comments
 (0)