Friday Jun 06, 2014

Getting Started with Amazon Web Services in NetBeans IDE

When you need to connect to Amazon Web Services, NetBeans IDE gives you a nice start. You can drag and drop the "itemSearch" service into a Java source file and then various Amazon files are generated for you.

From there, you need to do a little bit of work because the request to Amazon needs to be signed before it can be used.

Here are some references and places that got me started:

You definitely need to sign up to the Amazon Associates program and also register/create an Access Key ID, which will also get you a Secret Key, as well.

Here's a simple Main class that I created that hooks into the generated RestConnection/RestResponse code created by NetBeans IDE:

public static void main(String[] args) {
    try {
        String searchIndex = "Books";
        String keywords = "Romeo and Juliet";
        RestResponse result = AmazonAssociatesService.itemSearch(searchIndex, keywords);
        String dataAsString = result.getDataAsString();
        int start = dataAsString.indexOf("<Author>")+8;
        int end = dataAsString.indexOf("</Author>");
    } catch (Exception ex) {

Then I deleted the generated properties file and the authenticator and changed the generated file to the following:

public class AmazonAssociatesService {
    private static void sleep(long millis) {
        try {
        } catch (Throwable th) {
    public static RestResponse itemSearch(String searchIndex, String keywords) throws IOException {
        SignedRequestsHelper helper;
        RestConnection conn = null;
        Map queryMap = new HashMap();
        queryMap.put("Service", "AWSECommerceService");
        queryMap.put("AssociateTag", "myAssociateTag");
        queryMap.put("AWSAccessKeyId", "myAccessKeyId");
        queryMap.put("Operation", "ItemSearch");
        queryMap.put("SearchIndex", searchIndex);
        queryMap.put("Keywords", keywords);
        try {
            helper = SignedRequestsHelper.getInstance(
            String sign = helper.sign(queryMap);
            conn = new RestConnection(sign);
        } catch (IllegalArgumentException | UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException ex) {
        return conn.get(null);

Finally, I copied this class into my application, which you can see is referred to above:

Here's the completed app, mostly generated via the drag/drop shown at the start, but slightly edited as shown above:

That's all, now everything works as you'd expect.


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.


« June 2014 »