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 ZIP/TGZ archives.
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 ZIP/TGZ
- Step 2: Running The S3 Example
- Step 3: The S3 Example Step By Step
First of all please download the current VFSLib ZIP/TGZ archive on your computer. Choose the archive which can be best extracted from your computer (e.g. ZIP for Windows, TGZ for UNIX, ...). The ZIP archive may be unzipped directly from Ant build scripts using the unzip task into a local directory of your choice. Of course you can extract the VFSLib ZIP archive manually with WinZIP or other ZIP extraction tools.
The listing below shows the contents of the current VFSLib archives (list shows relevant files/folders only, library versions may change for newer VFSLib versions):
The ZIP archive contains various files and resources for VFSLib. The API (Javadoc) can be displayed by opening the index.html file with your favorite Internet browser. In the example folder you can find the precompiled Amazon S3 example code to be executed directly or using Apache Ant.
The extlib folder contains the additional libraries needed to run the VFSLib properly. If you plan to use the Amazon S3 provider you will have to add the Commons libraries, AWS/Http, and Jaxen/JDOM to the classpath. The lib folder contains the VFSLib libraries including some language packs (English and German at the moment). The English language pack is mandatory and must be included always along with the main VFSLib JAR file. The other language packs are only needed if your application supports these languages.
In the license folder you can find the VFSLib license as well as the licenses for the 3rd party JAR files residing in extlib. Please take a look at the README.html as a condensed overview of the package. The current release notes for the package can be found in the release_notes.html file.
A compatible Java Runtime Environment (JRE) must be installed prior to the installation of Apache Ant. Most developers can use their normal development environment here. Some IDEs like Netbeans which include a compatible JRE and Ant installation may support the direct execution of Ant build scripts.
The VFSLib provider for Amazon S3 example can be easily executed using Apache Ant. If not already installed please download and install a recent Ant for your operating system. Then you should add the Ant installation folder with the Ant executables to your local classpath so that the ant command may be executed directly from the extracted ZIP/TGZ archive folder (see secion Download VFSLib ZIP/TGZ).
Please open a command line (e.g. MS-DOS under Windows) and type the following commands:
cd [VFSLib installation folder]\example
ant -file build-examples.xml run-amazon-s3
Anyway 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 figure 1 shows a dialog is being displayed then where you can specify the (evaluation) license for VFSLib. If the license is not valid the example class exits with a message. If you specified a valid license the example proceeds with showing the next dialogs.
Figure 1: Amazon S3 example license chooser dialog
Figure 2: Amazon S3 example access key dialog
Figure 3: Amazon S3 example secret key dialog
Figure 4: Amazon S3 example bucket name dialog
You are next 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 2 and 3). The bucket name is the globally unique identifier for the bucket (drive) you're gonna access (see figure 4).
If you specified an evaluation license for VFSLib now an additional dialog will show up where you have to press the "Continue" button to move on. If you have obtained a commercial license already this dialog does not appear.
If all input has been done the example will continue in the background by performing some simple file operations using the specified Amazon S3 account. Please check out the output of the example script.
If you already have the keys and bucket name feel free to edit the build-examples.xml file and insert your values. You will have to uncomment the arg tag line. The input dialogs will not be displayed any longer.
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:
Configuration of the VFSLib (see ExampleAmazonS3.java)
List a S3 folder (see ExampleAmazonS3.java)
Create a S3 folder (see ExampleAmazonS3.java)
Upload a file to S3, get file type, last modified, and size (see ExampleAmazonS3.java)
Rename a S3 file (see ExampleAmazonS3.java)
Download a file from S3 (see ExampleAmazonS3.java)
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
- Get premium support for VFSLib
- Do you have any questions? Please send us a support request.