Getting album art using Amazon Web Services
June 24, 2008
![]()
Ever wanted to get CD album art from within a custom Java app? Amazon Web Services are the perfect tool for the job. In fact, you can get a whole lot more than just album art – you could, if you wanted, build a full Amazon store front using there vast APIs.
But lets just dip our toe in the water and see how easy it is to get CD artwork using the Amazon Associates Web Service. Start by signing up for a free account here. Amazon will send you a access key which you’ll need when calling their web services. This key doesn’t have to be kept super secret, as it will be passed as plain text through the internet (though I chose not to include my key in the demo below).
Next, download the Java APIs here (you need to be logged in with the account you created above).
Now all you need to do is call the service with code like this (note that this is a class I created and isn’t part of the API):
public class AmazonWebServiceUtils {
public static ImageIcon getAlbumArtImageIcon(
String artist, String album, String accessKeyId) throws Exception {
// build the search request that looks for images of music.
ItemSearchRequest request = new ItemSearchRequest();
request.setSearchIndex("Music");
request.setResponseGroup(Arrays.asList("Images"));
request.setArtist(artist);
request.setTitle(album);
// create a new amazon client using the access key. sign up for an
// amazon web services account here:
// https://aws-portal.amazon.com/gp/aws/developer/registration/index.html
AmazonA2SClient client = new AmazonA2SClient(accessKeyId, "");
// create a search response from the search request.
ItemSearchResponse response = client.itemSearch(request);
// get the URL to the amazon image (if one was returned).
String url = response.getItems().get(0).getItem().get(0).getLargeImage().getURL();
// create an ImageIcon from the returned URL. if the URL is null, then
// the icon returned will also be null.
return createImageIcon(url);
}
private static ImageIcon createImageIcon(String url) {
ImageIcon icon = null;
try {
icon = url == null ? null : new ImageIcon(new URL(url));
} catch (MalformedURLException e) {
// do nothing - don't care.
}
return icon;
}
}
If you look at the Javadoc for ItemSearchRequest, you’ll notice that you can easily lookup many more types of objects, like books and movies and can get more than just image data about them.
June 25, 2008 at 2:00 am
Great tip! I just tried it with Groovy and it works marvelously
July 11, 2008 at 9:49 pm
very nice tip! and very nice music taste ;)
July 12, 2008 at 1:05 am
Thanks Cristian…Avenged Sevenfold is a fantastic band!
October 29, 2009 at 12:29 am
The API link is broken. WHere can I downlod them?
October 29, 2009 at 1:32 pm
I believe this is the new link:
http://developer.amazonwebservices.com/connect/entry.jspa?externalID=132&categoryID=47
I’ve update the post.
November 24, 2009 at 5:21 am
It works this way! Thanks!