Getting Started with Amazon Web Services in NetBeans IDE

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Fri, 6 Jun 2014 12:28:04 +0000 Indexed on 2014/06/07 15:32 UTC
Read the original article Hit count: 253

Filed under:

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>");
        System.out.println(dataAsString.substring(start,end));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

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

public class AmazonAssociatesService {
    private static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } 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(
                    "ecs.amazonaws.com",
                    "
myAccessKeyId",
                    "mySecretKey");
            String sign = helper.sign(queryMap);
            conn = new RestConnection(sign);
        } catch (IllegalArgumentException | UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException ex) {
        }
        sleep(1000);
        return conn.get(null);
    }
}

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

http://code.google.com/p/amazon-product-advertising-api-sample/source/browse/src/com/amazon/advertising/api/sample/SignedRequestsHelper.java

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.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE