Integrating Amazon S3 in Java via NetBeans IDE

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.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2015
SunMonTueWedThuFriSat
   
4
5
11
12
18
19
20
21
22
23
24
25
26
27
28
29
30
  
       
Today