Integrating Amazon S3 in Java via NetBeans IDE

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Sat, 7 Jun 2014 08:25:42 +0000 Indexed on 2014/06/08 15:36 UTC
Read the original article Hit count: 182

Filed under:

To continue from yesterday, let's set up a scenario that enables us to make use of this drag/drop service in NetBeans IDE:

The above service is applicable to Amazon S3, an Amazon storage provider that is typically used to store large binary files. In Amazon S3, every object stored is contained in a bucket. Buckets partition the namespace of objects stored in Amazon S3. More on buckets here. Let's use the tools in NetBeans IDE to create a Java application that accesses our Amazon S3 buckets.

Create a Java application named "AmazonBuckets" with a main class named "AmazonBuckets". Open the main class and then drag the above service into the main method of the class. Now, NetBeans IDE will create all the other classes and the properties file that you see in the screenshot below.

The first thing to do is to open the properties file above and enter the access key and secret:

access_key=SOMETHING
secret=SOMETHINGELSE

Now you're all set up. Make sure to, of course, actually have some buckets available:

Then rewrite the Java class to parse the XML that is returned via the generated code:

package amazonbuckets;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.netbeans.saas.amazon.AmazonS3Service;
import org.netbeans.saas.RestResponse;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class AmazonBuckets {
    public static void main(String[] args) {
        try {
            RestResponse result = AmazonS3Service.getBuckets();
            String dataAsString = result.getDataAsString();
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(
                    new InputSource(new ByteArrayInputStream(dataAsString.getBytes("utf-8"))));
            NodeList bucketList = doc.getElementsByTagName("Bucket");
            for (int i = 0; i < bucketList.getLength(); i++) {
                Node node = bucketList.item(i);
                System.out.println("Bucket Name: " + node.getFirstChild().getTextContent());
            }
        } catch (IOException | ParserConfigurationException | SAXException | DOMException ex) {
        }
    }
}

That's all. This is simpler to setup than the scenario described yesterday.

Also notice that there are other Amazon S3 services you can interact with from your Java code, again after generating a heap of code after drag/drop into a Java source file:

I tried the above, e.g., I created a new Amazon S3 bucket after dragging "createBucket", adding my credentials in the properties file, and then running the code that had been created. I.e., without adding a single line of code I was able to programmatically create new buckets.

The above outlines a handy set of tools and techniques to use if you want to let your users store and access data in Amazon S3 buckets directly from the application you've created for them.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE