VFSLib Amazon S3 Example
Please read the following steps to use the VFSLib Java library. This example demonstrates the usage of the Amazon S3 © provider included in the VFSLib artifacts.
The Java example code is based on the original Apache Commons VFS 2.0 classes like DefaultFileSystemManager and thus should integrate seamlessly into existing implementations.
Step 1: Download VFSLib
First of all please download the current VFSLib source archive on your computer and extract.
In the vfslib-src-2.8.2\src\test\java\com\lf\vfslib\test\s3 folder you can find the Amazon S3 example code to be executed directly in your favorite IDE like IntelliJ IDEA.
Please take a look at the README.md to get the Maven project up and running from pom.xml.
Step 2: Running The S3 Example
Start the example main class(es) via your IDE:
- ExampleAmazonS3.java Based on VFS 2.0 standards only
You will need to run the example in a graphical environment since the classes display dialogs to prompt the user for the parameters necessary for Amazon S3. |
As the figures show you are asked to specify access key and secret key for your AWS account. Please go create a Amazon Web Services (AWS) account on the Amazon website. The access key and secret key generated by Amazon can be used to fill in the dialog input fields (see figures 1 and 2). The bucket name is the globally unique identifier for the bucket (drive) you're gonna access (see figure 3).
Figure 1: Amazon S3 example access key dialog
Figure 2: Amazon S3 example secret key dialog
Figure 3: Amazon S3 example bucket name dialog
If you already have the keys and bucket name feel free to specify as main class params. The input dialogs will not be displayed any longer. |
Step 3: The S3 Example Step By Step
Let's take a closer look at the Amazon S3 example implementation. The first thing to do is to configure the VFSLib class which is the central class of the VFSLib library:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Setup the main VFSLib instance VFSLib vfslib = new VFSLib(); // We use the default file system manager here DefaultFileSystemManager fsmanager = (DefaultFileSystemManager) VFS.getManager(); // Add Amazon S3 provider to VFS S3FileProvider provider = vfslib.addProviderAmazonS3(fsmanager); String scheme = vfslib.getSchemeAmazonS3(); // Pass bucket name over to VFSLib to access the Amazon S3 account FileSystemOptions options = new FileSystemOptions(); S3FileSystemConfigBuilder builder = new S3FileSystemConfigBuilder(); builder.setBucketName(options, bucket); // Setup proper account name, used as user name for Amazon S3 URLs (failsafe is "anonymous"): // s3://[displayname]@aws.amazon.com String username = "johndoe"; builder.setAccountDisplayName(options, username); builder.setAccessKeyID(options, accesskeyid); builder.setSecretKey(options, secretkey); |
Configuration of the VFSLib (see ExampleAmazonS3.java
List a S3 Folder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
String uri = scheme + "://" + username + "@aws.amazon.com"; try { FileObject fileobj = fsmanager.resolveFile(uri, options); if (fileobj.getType().equals(FileType.FOLDER)) { FileObject[] childs = fileobj.getChildren(); for (FileObject next : childs) { if (next.getType().equals(FileType.FOLDER)) { System.out.println(" DIR: " + String.valueOf(next)); } else System.out.println(" FILE: " + String.valueOf(next)); } } else System.out.println(" Entry " + uri + " is not a folder"); } catch (Exception e) { e.printStackTrace(); } |
List a S3 folder (see ExampleAmazonS3.java
Create a S3 Folder
1 2 3 4 5 6 7 8 9 10 11 |
String tempfolder = scheme + "://" + username + "@aws.amazon.com/0123456789abcdefghijklmnopqrstuvwxyz"; boolean success = false; try { FileObject fileobj = fsmanager.resolveFile(tempfolder, options); if (!fileobj.exists()) { fileobj.createFolder(); success = fileobj.exists(); } } catch (Exception e) { e.printStackTrace(); } |
Create a S3 folder (see ExampleAmazonS3.java)
Upload a File To S3, Get File Type, Last Modified, And Size
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
String content = "Hello world!"; String encoding = "ISO-8859-1"; // Upload a file to Amazon S3. Get file type, last modified, and size. String tempfile = tempfolder + "/readme.txt"; try { FileObject fileobj = fsmanager.resolveFile(tempfile, options); fileobj.getContent().setAttribute(VFSLibConstants.ATTR_CONTENT_LENGTH, content.getBytes(encoding).length); OutputStream ostream = fileobj.getContent().getOutputStream(); ostream.write(content.getBytes(encoding)); ostream.flush(); ostream.close(); success = fileobj.exists(); System.out.println(" TYPE: " + fileobj.getType().getName()); System.out.println(" MOD: " + new Date(fileobj.getContent().getLastModifiedTime())); System.out.println(" SIZE: " + fileobj.getContent().getSize()); } catch (Exception e) { e.printStackTrace(); } |
Upload a file to S3, get file type, last modified, and size (see ExampleAmazonS3.java)
Rename a S3 File
1 2 3 4 5 6 7 8 9 |
String tempfilerenamed = tempfolder + "/README"; try { FileObject fileobj = fsmanager.resolveFile(tempfile, options); FileObject fileobjrenamed = fsmanager.resolveFile(tempfilerenamed, options); fileobj.moveTo(fileobjrenamed); success = fileobjrenamed.exists(); if (success) tempfile = tempfilerenamed; } catch (Exception e) { e.printStackTrace(); } |
Rename a S3 file (see ExampleAmazonS3.java)
Download a File From S3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
try { FileObject fileobj = fsmanager.resolveFile(tempfile, options); ByteArrayOutputStream bostream = new ByteArrayOutputStream(); InputStream istream = fileobj.getContent().getInputStream(); int len; byte[] buffer = new byte[1024]; while ((len = istream.read(buffer)) != -1) { bostream.write(buffer, 0, len); } istream.close(); bostream.flush(); bostream.close(); String loaded = new String(bostream.toByteArray(), encoding); success = loaded.equals(content); } catch (Exception e) { e.printStackTrace(); } |
Download a file from S3 (see ExampleAmazonS3.java)
Remove a S3 File And Folder
1 2 3 4 5 6 7 8 9 10 |
try { // Remove file FileObject fileobj = fsmanager.resolveFile(tempfile, options); success = fileobj.delete(); // Remove folder fileobj = fsmanager.resolveFile(tempfolder, options); success = fileobj.delete(); } catch (Exception e) { e.printStackTrace(); } |
Remove a S3 file and folder (see ExampleAmazonS3.java)
How to continue
- View the Howto: VFSLib - Apache Commons VFS Reloaded
- View the API documentation (Javadoc)
- Download the current VFSLib archives
- Do you have any questions? Please send us a support request.