*
*
|
|
Amazon Web Services Sample Code
Zamples® makes it easy for you to play with
Amazon's
Web Services Toolkit. You don't need to install any software, just click the 'Try It!'
buttons below. Currently we offer examples written in Perl, Java and Python.
Let
us know if there are any other languages that you would like us to support.
Amazon has a comprehensive Web Services implementation, but it requires considerable
knowledge to use. We have done all the work for you and made it available
on this Zamples installation, so you don't have spend any time learning, installing
and debugging in order to get real experience with Amazon's Web Services.
Java programmers: Amazon doesn't provide Javadocs for their Web Services implementation, because
they don't ship a Web Services jar. Instead, programmers must generate
their own from Amazon's WSDL.
We used the wsdl2java tool provided by
Apache Axis 1.1 to autogenerate the skeleton code, created
Javadocs
for the generated code, converted them to work with Zamples. We then compiled
the code and created a jar, which we added to this Zamples runtime library.
You are welcome to examine the Javadocs, but remember that if you use
a different version of Apache Axis, or a different wsdl2java tool from
another vendor that the Javadocs may not match up with your code.
You may be interested to know that the Javadoc describes 51 classes or interfaces
and 974 methods for the current version of AWS. That's a fair sized API to learn - and the next
version of AWS will be even bigger. Stay tuned!
After you click on a 'Try It!' button in a community posting, or after you press a 'New' button
to start a new discussion thread,
edit your code, select the type of program (JDK 1.4/5.0 Fragment or Class, Perl or Python), and then 'Run It!' until it works (or
you give up). When you click on the 'Save' button, the New Posting Wizard will appear. The wizard will prompt
you for information about your code example, and then save it to the database. Each posting contains
a code example, plus comments about the code. Next time you refresh this page
your new posting will appear.
You can also play with the Javadoc for Amazon Web Services.
At the Amazon Web Services workshop at SD Forum on March 17, 2004, Amazon provided
this link for
some reference material. BTW, the code behind "AWS in Java" on Sun's site doesn't
download.
Learn more about Zamples and play with samples for other APIs.
Our live JDK 1.5 examples are very popular!
We would love to hear your feedback.
News Flash
On April 16, 2004, SOAP and XML obsoleted references to the version 1 services, so they no longer function.
This caused Python applications to break.
The Python library behind the examples on this page has had an initial update, and the maintainer,
Michael Josephson seems to suggest that further work is still
required. We have not had any problems getting the Python code examples to work.
Simple Search using Java (take 2)
Purpose: bvbvcb
Description: bvcbcb |
AmazonSearchPort port = null;
AmazonSearchService service = new AmazonSearchServiceLocator();
try {
port = service.getAmazonSearchPort();
} catch (javax.xml.rpc.ServiceException e) { e.printStackTrace(); }
KeywordRequest req = new KeywordRequest();
try {
req.setKeyword(java.net.URLEncoder.encode("web services", "utf-8"));
} catch (java.io.UnsupportedEncodingException ignored) {}
req.setMode("books");
req.setType("lite");
ProductInfo res = null;
try {
res = port.keywordSearchRequest(req);
} catch (Exception e) { e.printStackTrace(); }
Details[] details = res.getDetails();
for (int i = 0; i < details.length; i++) {
String resultTitle = details[i].getProductName();
System.out.println(i + ": " + resultTitle);
}
|
Simple Search using Java - cleaned up but still doesn't work
Purpose: A minimal example to show a simple search. You can change the keyword, to change the search.Run it and see!
Description: . |
AmazonSearchPort port = null;
AmazonSearchService service = new AmazonSearchServiceLocator();
try {
port = service.getAmazonSearchPort();
} catch (javax.xml.rpc.ServiceException e) { e.printStackTrace(); }
KeywordRequest req = new KeywordRequest();
try {
req.setKeyword(URLEncoder.encode("web services", "UTF8"));
} catch (Exception ignored) {}
req.setMode("books");
req.setType("lite");
ProductInfo res = null;
try {
res = port.keywordSearchRequest(req);
} catch (Exception e) { e.printStackTrace(); }
Details[] details = res.getDetails();
for (int i = 0; i < details.length; i++) {
String resultTitle = details[i].getProductName();
System.out.println(i + ": " + resultTitle);
}
|
Simple Search using Java (take 2)
Purpose:
Description: This fixes a bug in the previous posting. There was not try/catch around the URLencode().should work much better now! |
AmazonSearchPort port = null;
AmazonSearchService service = new AmazonSearchServiceLocator();
try {
port = service.getAmazonSearchPort();
} catch (javax.xml.rpc.ServiceException e) { e.printStackTrace(); }
KeywordRequest req = new KeywordRequest();
try {
req.setKeyword(java.net.URLEncoder.encode("web services", "utf-8"));
} catch (java.io.UnsupportedEncodingException ignored) {}
req.setMode("books");
req.setType("lite");
ProductInfo res = null;
try {
res = port.keywordSearchRequest(req);
} catch (Exception e) { e.printStackTrace(); }
Details[] details = res.getDetails();
for (int i = 0; i < details.length; i++) {
String resultTitle = details[i].getProductName();
System.out.println(i + ": " + resultTitle);
}
|
Simple Search using Java
Purpose:
Description: A minimal example to show a simple search. You can change the keyword, to change the search.Run it and see! |
AmazonSearchPort port = null;
AmazonSearchService service = new AmazonSearchServiceLocator();
try {
port = service.getAmazonSearchPort();
} catch (javax.xml.rpc.ServiceException e) { e.printStackTrace(); }
KeywordRequest req = new KeywordRequest();
req.setKeyword(java.net.URLEncoder.encode("web services", "UTF8"));
req.setMode("books");
req.setType("lite");
ProductInfo res = null;
try {
res = port.keywordSearchRequest(req);
} catch (Exception e) { e.printStackTrace(); }
Details[] details = res.getDetails();
for (int i = 0; i < details.length; i++) {
String resultTitle = details[i].getProductName();
System.out.println(i + ": " + resultTitle);
}
|
|
| to add a new posting or reply to an existing posting. |
|
Simple Search using Perl
Purpose:
Description: Change the keyword under the word 'Stylesheet' from 'happiness' to any other word, and rerun the scriptThis code example was taken from "Amazon Hacks", published by O'Reilly & Associates. |
#!/usr/bin/perl
# amazon_http.pl
# A typical Amazon Web API Perl script using the XML/HTTP interface
# Usage: amazon_http.pl <keyword>
#Your Amazon developer's token
my $dev_key='yourTokenHere';
#Your Amazon affiliate code
my $af_tag='whatever';
#Take the keyword from the command-line
my $keyword =shift @ARGV or die "Usage:perl amazon_http.pl <keyword>
";
#Assemble the URL
my $url = "http://xml.amazon.com/onca/xml3?t=" . $af_tag .
"&dev-t=" . $dev_key .
"&type=lite&f=xml&mode=books&" .
"KeywordSearch=" . $keyword;
use strict;
#Use the XML::Parser and LWP::Simple Perl modules
use XML::Simple;
use LWP::Simple;
my $content = get($url);
die "Could not retrieve $url" unless $content;
my $xmlsimple = XML::Simple->new( );
my $response = $xmlsimple->XMLin($content);
foreach my $result (@{$response->{Details}}){
#Print out the main bits of each result
print
join "
",
$result->{ProductName}||"no title",
"ASIN: " . $result->{Asin} . ", " .
$result->{OurPrice} . "
";
}
|
|
| to add a new posting or reply to an existing posting. |
|
Simple Search Using Python (better formatting)
Purpose: Shows how to use Python to search via Amazon Web Services
Description: Change the keyword from 'happiness' to any other word, and rerun the search. This code example is taken from "Amazon Hacks" by O'Reilly & Associates. |
#!/usr/bin/python
# amazon_wrap.py
# A typical Amazon Web API Python script using Mark Pilgrim's
# pyAmazon Amazon Web Service API wrapper
# [http://diveintomark.org/projects/#pyamazon]
# Usage: python amazon_wrap.py <keyword>
import sys
# Use the pyAmazon Functions
import amazon
# Set your dev token
amazon.setLicense('yourTokenHere')
# Get the Keyword from input
if sys.argv [1:]:
actor = sys.argv [1 ]
else:
sys.exit('Usage: python amazon_wrap.py <keyword>')
# Make the Request
pythonBooks = amazon.searchByKeyword(actor)
# Print the Results
for book in pythonBooks:
print book.ProductName
print "by",
authorList = book.Authors.Author
if len(authorList) < 5:
for author in authorList:
print author + ", ",
else:
print book.Authors.Author,
print book.OurPrice + "\n"
|
Simple Search Using Python
Purpose:
Description: Change the keyword from 'happiness' to any other word, and rerun the search This code example is taken from "Amazon Hacks" by O'Reilly & Associates. |
#!/usr/bin/python
# amazon_wrap.py
# A typical Amazon Web API Python script using Mark Pilgrim's
# pyAmazon Amazon Web Service API wrapper
# [http://diveintomark.org/projects/#pyamazon]
# Usage: python amazon_wrap.py <keyword>
import sys
# Use the pyAmazon Functions
import amazon
# Set your dev token
amazon.setLicense('yourTokenHere')
# Get the Keyword from input
if sys.argv [1:]:
actor = sys.argv [1 ]
else:
sys.exit('Usage: python amazon_wrap.py <keyword>')
# Make the Request
pythonBooks = amazon.searchByKeyword(actor)
# Print the Results
for book in pythonBooks:
print book.ProductName
print "by",
authorList = book.Authors.Author
if len(authorList) < 5:
for author in authorList:
print author + ", ",
else:
print book.Authors.Author,
print book.OurPrice + "
"
|
|
| to add a new posting or reply to an existing posting. |
|
Another Perl search (H464)
Purpose:
Description: This is from O'Reilly's Amazon Hacks bookyou can alter the keyword to search |
|
|
| to add a new posting or reply to an existing posting. |
|
Add New Code Samples Here
If you have code samples that don't fit into any of the topics above which you would like to contribute,
please put them here.
We'll sort them out into their own sections as appropriate later.
|
| to add a new posting or reply to an existing posting. |
|
|