Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to access an existing S3 bucket! #460

Closed
mirkoscotti opened this issue May 17, 2024 · 2 comments
Closed

No way to access an existing S3 bucket! #460

mirkoscotti opened this issue May 17, 2024 · 2 comments

Comments

@mirkoscotti
Copy link

Creating a new S3 bucket through the newFileSystem API works properly. See the example below:

public static void main(String[] args) throws IOException
{
	System.setProperty("aws.accessKeyId", "MY-ACCESS-KEY");
	System.setProperty("aws.secretAccessKey", "MY-SECRET-KEY");
	System.setProperty("aws.region", Region.EU_NORTH_1.id());
	var hostName = InetAddress.getLocalHost().getHostName().toLowerCase();
	var uri = URI.create("s3://%s-test-bucket/".formatted(hostName));
	var classLoader = Thread.currentThread().getContextClassLoader();
	try (var fileSystem = FileSystems.newFileSystem(uri, Map.<String, Object>of(), classLoader))
	{
		fileSystem.getRootDirectories().forEach(System.out::println);
	}
}

Ultimately, I can find the new bucket nbk325-test-bucket successfully created. But if I run the following code (simply replacing the newFileSystem just created before by getFileSystem to access that bucket):

public static void main(String[] args) throws IOException
{
	System.setProperty("aws.accessKeyId", "MY-ACCESS-KEY");
	System.setProperty("aws.secretAccessKey", "MY-SECRET-KEY");
	System.setProperty("aws.region", Region.EU_NORTH_1.id());
	var hostName = InetAddress.getLocalHost().getHostName().toLowerCase();
	var uri = URI.create("s3://%s-test-bucket/".formatted(hostName));
	try (var fileSystem = FileSystems.getFileSystem(uri))
	{
		fileSystem.getRootDirectories().forEach(System.out::println);
	}
}

my program crashes with this trace:

Exception in thread "main" java.nio.file.FileSystemNotFoundException: file system not found for 'nbk325-test-bucket'
	at software.amazon.nio.spi.s3.S3FileSystemProvider.lambda$getFileSystem$6(S3FileSystemProvider.java:777)
	at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1228)
	at software.amazon.nio.spi.s3.S3FileSystemProvider.getFileSystem(S3FileSystemProvider.java:775)
	at software.amazon.nio.spi.s3.S3FileSystemProvider.getFileSystem(S3FileSystemProvider.java:219)
	at java.base/java.nio.file.FileSystems.getFileSystem(FileSystems.java:235)
	at test.Main.main(Main.java:32)

The reason for this failure is that the getFileSystem method in S3FileSystemProvider expects to find the file system already cached, but it is not because at startup the in-memory FS_CACHE is of course empty!!

@Override
public FileSystem getFileSystem(URI uri) {
    return getFileSystem(uri, false);
}
...
S3FileSystem getFileSystem(URI uri, boolean create) {
    var info = fileSystemInfo(uri);
    return FS_CACHE.computeIfAbsent(info.key(), (key) -> {  
        if (!create) { <== HERE IS THE CRASH!!
            throw new FileSystemNotFoundException("file system not found for '" + info.key() + "'");
        }

        var config = new S3NioSpiConfiguration().withEndpoint(info.endpoint()).withBucketName(info.bucket());
        if (info.accessKey() != null) {
            config.withCredentials(info.accessKey(), info.accessSecret());
        }
        return new S3FileSystem(this, config);
    });
}

In my opinion, the create flag does not make sense. I would suggest removing it and converting the code above to the following which works very well:

@Override
public FileSystem getFileSystem(URI uri) {
    var info = fileSystemInfo(uri);
    return FS_CACHE.computeIfAbsent(info.key(), (key) -> {  
        var config = new S3NioSpiConfiguration().withEndpoint(info.endpoint()).withBucketName(info.bucket());
        if (info.accessKey() != null) {
            config.withCredentials(info.accessKey(), info.accessSecret());
        }
        return new S3FileSystem(this, config);
    });
}
@markjschreiber
Copy link
Contributor

Agreed. I don't think there is a need for the create test. I will try to make this fix in the next week or so. If you would like to contribute a PR with this fix I can review it sooner.

markjschreiber added a commit that referenced this issue May 17, 2024
@markjschreiber
Copy link
Contributor

closed by #463

markjschreiber added a commit that referenced this issue May 17, 2024
stefanofornari pushed a commit to stefanofornari/aws-java-nio-spi-for-s3 that referenced this issue May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants