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

[Issue] Empty folder creation fails for AWS S3 Bucket #176

Open
ManpreetNRI opened this issue Nov 7, 2024 · 0 comments
Open

[Issue] Empty folder creation fails for AWS S3 Bucket #176

ManpreetNRI opened this issue Nov 7, 2024 · 0 comments

Comments

@ManpreetNRI
Copy link

I am trying to create an empty folder in an AWS S3 bucket using the VFS-S3 plugin for the Apache VFS library. Below is a code snippet demonstrating my approach:

FileSystemManager fileSystemManager = VFS.getManager();

// 'root-folder' already exists in 'test-bucket' in the 'eu-central-1' region, as part of the initial configuration
String baseURI = "s3://test-bucket.s3-eu-central-1.amazonaws.com/root-folder";

FileObject rootFolderFileObject = fileSystemManager.resolveFile(baseURI, null);

// Here, I attempt to create an empty 'child-folder' within 'root-folder'
FileObject childFolderFileObject = rootFolderFileObject.resolveFile("child-folder");
childFolderFileObject.createFolder();

However, this code throws the following error:

[ERROR] [stderr] org.apache.commons.vfs2.FileSystemException: Could not create folder "S3FileName{endpoint='s3.eu-central-1.amazonaws.com', urlPrefix='null', pathPrefix='test-bucket', bucket='test-bucket', signingRegion='eu-central-1', platformFeatures=PlatformFeaturesImpl{defaultAllowForOwner=true, allowDenyForOwner=true, supportsServerSideEncryption=true, supportsAuthorizedGroup=true}, accessKey='null', secretKey='null'}".
[ERROR] [stderr] 	at deployment.planon.ear//com.github.vfss3.AbstractFileObject.createFolder(AbstractFileObject.java:386)
[ERROR] [stderr] 	at deployment.planon.ear//com.github.vfss3.S3FileObject.createFolder(S3FileObject.java:38)
...
[ERROR] [stderr] Caused by: org.apache.commons.vfs2.FileSystemException: Could not create folder "S3FileName{endpoint='s3.eu-central-1.amazonaws.com', urlPrefix='null', pathPrefix='test-bucket', bucket='test-bucket', signingRegion='eu-central-1', platformFeatures=PlatformFeaturesImpl{defaultAllowForOwner=true, allowDenyForOwner=true, supportsServerSideEncryption=true, supportsAuthorizedGroup=true}, accessKey='null', secretKey='null'}" because it already exists and is a file.
[ERROR] [stderr] 	at deployment.planon.ear//com.github.vfss3.AbstractFileObject.createFolder(AbstractFileObject.java:375)

Interestingly, when I try to upload a file into a non-existent folder, the library successfully creates the missing folder and uploads the file into it. Here’s the relevant code:

// Create a temporary source file
File tempSourceFile = File.createTempFile("tempSourceFile", ".txt");
tempSourceFile.deleteOnExit();
try (FileWriter fileWriter = new FileWriter(tempSourceFile)) {
    fileWriter.write("This is test data.");
}

FileSystemManager fileSystemManager = VFS.getManager();

// 'root-folder' already exists in 'test-bucket' in the 'eu-central-1' region
String baseURI = "s3://test-bucket.s3-eu-central-1.amazonaws.com/root-folder";

FileObject rootFolderFileObject = fileSystemManager.resolveFile(baseURI, null);

// Attempt to upload 'child-file.txt' to a non-existent 'child-folder' within 'root-folder'
FileObject childFileObject = rootFolderFileObject.resolveFile("child-folder/child-file.txt");
childFileObject.createFile();

try (InputStream sourceInputStream = new FileInputStream(tempSourceFile);
     OutputStream destinationOutputStream = childFileObject.getContent().getOutputStream()) {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = sourceInputStream.read(buffer)) != -1) {
        destinationOutputStream.write(buffer, 0, bytesRead);
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

This code successfully creates the child-folder within root-folder and uploads child-file.txt into it.

It appears that creating an empty folder directly is not possible, while creating a folder through file upload works as expected. Could this be a bug or a limitation of the VFS-S3 plugin? Any insights into why creating an empty folder fails but implicitly creating one with a file succeeds would be greatly appreciated.

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

1 participant