Using a JFileChooser to browse AWS S3
After a bit of searching I was unable to find an off the shelf solution to let me browse S3 with a JFileChooser. The closest I found was an S3 FileSystem implementation, but that doesn't seem to be used by JFileChooser. Instead you need a custom FileSystemView, which I implemented using minio as a simple client library.
The code is not incredibly complicated so I wont explain it in detail. You can see that the custom S3FileSystemView is simply passed to JFileChooser to insert the S3 logic. The most interesting method is getFiles, that is where I decided to insert the buckets at the top level, so there is one switch which decides whether to call listBuckets or listObjects. From that point my custom VirtualFile objects are carefully constructed so that the File class plays nicely with JFileChooser.
The result from selecting a file is something like s3:/bucket/path/to/file.txt which you will need to download yourself. VirtualFile could probably be extended to work like a normal file, but that was beyond my needs.
The code is not incredibly complicated so I wont explain it in detail. You can see that the custom S3FileSystemView is simply passed to JFileChooser to insert the S3 logic. The most interesting method is getFiles, that is where I decided to insert the buckets at the top level, so there is one switch which decides whether to call listBuckets or listObjects. From that point my custom VirtualFile objects are carefully constructed so that the File class plays nicely with JFileChooser.
The result from selecting a file is something like s3:/bucket/path/to/file.txt which you will need to download yourself. VirtualFile could probably be extended to work like a normal file, but that was beyond my needs.
Comments
Post a Comment