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.

About these ads

13 Responses to “Getting album art using Amazon Web Services”


  1. Great tip! I just tried it with Groovy and it works marvelously

  2. Crístian Viana Says:

    very nice tip! and very nice music taste ;)

  3. Ken Says:

    Thanks Cristian…Avenged Sevenfold is a fantastic band!

  4. Tristan Seifert Says:

    The API link is broken. WHere can I downlod them?

  5. tristanseifert Says:

    It works this way! Thanks!

  6. andre Says:

    hi,
    could you tell me how to use this pls. i’m new to aws and fairly new to java.

    i’ve downloaded the amazon api
    but which of the amazon classes do i create a new instance off to make your methods work?

    what is ItemSearchRequest,AmazonA2SClient and ItemSearchResponse? i’m getting an error on those…
    cannot find symbol for all 3 of those words?
    any help pls?

  7. Anasi Says:

    Does it cost anything to use this at Amazon. I didnt found anything about that on the website.


  8. […] like your question is less about Android and more about how to get album art in general. Perhaps this article on retrieving album art from Amazon will be helpful. Once you have a local copy and store it as […]

  9. Vladislav Says:

    the api link is again broken , where can i download the api??

  10. chengsokdara Says:

    This this really free? Will it charge me after going beyond free tier usage of AWS?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: