amazon snippets

How to access Amazon product data with Ruby/AWS and the Amazon Associates Web Services API

Tagged ruby-aws, amazon-ecs, amazon  Languages ruby

First you need to decide which of the following Ruby libraries you want to use:

* Ruby/AWS * amazon-ecs

You can also find the code on GitHub.

In this example I've decided to go with Ruby/AWS because [insert reason here].

Installing Ruby/AWS

curl -O
tar zxvf ruby-aws-0.7.0.tar.gz
cd ruby-aws-0.7.0
ruby setup.rb config
ruby setup.rb setup
sudo ruby setup.rb install

Using Ruby/AWS

With Ruby/AWS installed you can now run this code:

#!/usr/bin/ruby -w

require 'amazon/aws'
require 'amazon/aws/search'
include Amazon::AWS
include Amazon::AWS::Search

# Example of a batch operation, using the ASIN as the shared ID.
# The MerchantId restriction is to ensure that we retrieve only items that
# are for sale by Amazon. This is important when we later want to retrieve the
# availability status.
il = 'ASIN', { 'ItemId' => 'B000AE4QEC', 'MerchantId' => 'Amazon' })
# You can have multiple response groups.
rg = 'Medium', 'Offers', 'Reviews' )
req =
req.locale = 'uk'
resp = il, rg )
item_sets = resp.item_lookup_response[0].items
item_sets.each do |item_set|
  item_set.item.each do |item|
    attribs = item.item_attributes[0]
    puts attribs.label
    if attribs.list_price
      puts attribs.title, attribs.list_price[0].formatted_price
    # Availability has become a cumbersome thing to retrieve in AWSv4.
    puts 'Availability: %s' %
      [ item.offers[0].offer[0].offer_listing[0].availability ]
    puts 'Average rating: %s' % [ item.customer_reviews[0].average_rating ]
    puts 'Reviewed by %s customers.' %
      [ item.customer_reviews[0].total_reviews ]
    puts 'Customers said:'
    item.customer_reviews[0].review.each do |review|
      puts ' %s (%s votes)' % [ review.summary, review.total_votes ]

You'll get this error:

/opt/local/lib/ruby/site_ruby/1.8/amazon/aws.rb:138:in get_page': HTTP response code 400 (Amazon::AWS::HTTPError)

You probably ran the code after August 2009 which is when Amazon started requiring signed requests.

Now if you run the code with ruby -d code.rb you'll see that the request signature is missing:

<?xml version="1.0"?>
<ItemSearchErrorResponse xmlns=""><Error><Code>MissingParameter</Code><Message>The request must contain the parameter Signature.</Message></Error><RequestID>13563d70-7696-4b8f-afa3-064184874620</RequestID></ItemSearchErrorResponse>

This takes us to the next topic.

Configuring Ruby/AWS

Create a file called ~/.amazonrc and put the following in it:

key_id = 'xxx'
secret_key_id = 'xxx'
associate = 'xxx-20'
cache = false
locale = 'uk'
encoding = 'iso-8859-15'

Next sign up you need to:

That's it. If you want, have a look at the examples

How to scrape a Amazon Listmania list with Hpricot and Ruby

Tagged amazon, hpricot, scrape  Languages ruby
require 'open-uri'
require 'hpricot'
html =  open('')

page = Hpricot(html)

xpath = "td[@class='listItem']//input[@name='asin.1']" do |book|
  puts book['value']

How to configure a no-www redirection on Amazon AWS (S3 and Cloudfront)

Tagged no-www, amazon, s3, cloudfront  Languages bash

How do you configure a site hosted on Amazon AWS, e.g. a static website, to redirect from "www" to the "no-www" domain (aka "naked domain")? The tool for the job is the AWS CLI.

For example, this creates a redirection from to for a bucket named

aws s3api put-bucket-website --bucket --website-configuration '{
    "RedirectAllRequestsTo": {
        "HostName": ""

Use this command to check that the configuration is set properly:

aws s3api get-bucket-website --bucket

How to Create and Configure a Static Website Hosted on Amazon S3

Tagged amazon, s3, aws, static, website  Languages bash

This will create two S3 buckets and configure them for hosting a static website:

aws s3 mb s3://
aws s3 mb s3://

aws s3api put-bucket-website --bucket --website-configuration '{
    "RedirectAllRequestsTo": {
        "HostName": ""

aws s3api put-bucket-website --bucket --website-configuration '{
    "IndexDocument": {
        "Suffix": "index.html"
    "ErrorDocument": {
        "Key": "404/index.html"

aws s3api get-bucket-website --bucket
aws s3api get-bucket-website --bucket

Next, allow anyone to access the files:

aws s3api put-bucket-policy --bucket --policy '{
  "Version": "2012-10-17",
  "Statement": [
      "Sid": "AddPerm",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      "Action": "s3:GetObject",
      "Resource": "*"

Next, upload your website to S3:

aws s3 sync build s3:// --acl public-read --delete

In the above example all files are located in a folder named build.

Done. Your website can be found at

Note: * if you're serious about hosting your website, use AWS Cloudfront or Cloudflare + S3 * the "www" URL redirects to the "non-www" URL