<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Amazon code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 02:20:41 GMT</pubDate>
    <description>DZone Snippets: Amazon code</description>
    <item>
      <title>Rake task to set all S3 files public_read</title>
      <link>http://snippets.dzone.com/posts/show/5766</link>
      <description>//  If you ever need to make sure all your Amazon S3 files are set to public_read, here's a rake task&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;namespace :s3 do&lt;br /&gt;  desc "Make all objects in S3 public_read"&lt;br /&gt;  task :make_public_readable do&lt;br /&gt;    require 'aws/s3'&lt;br /&gt;    # you might have this setup as env vars, doesn't work for me as i have more than one AWS account&lt;br /&gt;    AWS::S3::Base.establish_connection!(:access_key_id =&gt; '',:secret_access_key =&gt; '')    &lt;br /&gt;    &lt;br /&gt;    marker = ""&lt;br /&gt;    &lt;br /&gt;    loop do&lt;br /&gt;      objects = AWS::S3::Bucket.objects('your_bucket', :marker=&gt;marker, :max_keys=&gt;100)&lt;br /&gt;      puts "found #{objects.size} objects"&lt;br /&gt;    &lt;br /&gt;      break if objects.size == 0&lt;br /&gt;    &lt;br /&gt;      marker = objects.last.key&lt;br /&gt;      puts "new marker is \"#{marker}\""&lt;br /&gt;    &lt;br /&gt;      public_grant = AWS::S3::ACL::Grant.grant :public_read&lt;br /&gt;  &lt;br /&gt;      objects.each do |o|&lt;br /&gt;        if not o.acl.grants.include? public_grant&lt;br /&gt;          puts "\"#{o.key}\" does not include public_read"&lt;br /&gt;          o.acl.grants &lt;&lt; public_grant&lt;br /&gt;          o.acl(o.acl)&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 15 Jul 2008 01:00:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5766</guid>
      <author>spiceee (Spiceee)</author>
    </item>
    <item>
      <title>Creating a bucket in Amazon S3 through an irb session</title>
      <link>http://snippets.dzone.com/posts/show/5441</link>
      <description>1) Log into an irb session, and enter your S3 login details.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'aws/s3'&lt;br /&gt;&lt;br /&gt;  AWS::S3::Base.establish_connection!(&lt;br /&gt;    :access_key_id     =&gt; 'REPLACE_ME',&lt;br /&gt;    :secret_access_key =&gt; 'REPLACE_ME'&lt;br /&gt;  )&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; #&lt;AWS::S3::Connection:0xb75e0594 @http=#&lt;Net::HTTP s3.amazonaws.com:80 open=false&gt;, @secret_access_key="", @options={:server=&gt;"s3.amazonaws.com", :access_key_id=&gt;"", :port=&gt;80, :secret_access_key=&gt;"", :persistent=&gt;true}, @access_key_id="19S45GYAGWK8DC2B8VG2"&gt;&lt;br /&gt;&lt;br /&gt;2) Browse the existing buckets.&lt;br /&gt;&lt;code&gt;AWS::S3::Service.buckets&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; [#&lt;AWS::S3::Bucket:0xb75cc850 @object_cache=[], @attributes={"name"=&gt;"ogg.twitteraudio.com", "creation_date"=&gt;Sat Apr 26 10:40:16 UTC 2008}&gt;, #&lt;AWS::S3::Bucket:0xb75cc83c @object_cache=[], @attributes={"name"=&gt;"t1000", "creation_date"=&gt;Fri Apr 25 21:35:21 UTC 2008}&gt;, #&lt;AWS::S3::Bucket:0xb75cc814 @object_cache=[], @attributes={"name"=&gt;"t2000", "creation_date"=&gt;Fri Apr 25 21:53:15 UTC 2008}&gt;]&lt;br /&gt;&lt;br /&gt;3) Browse the buckets in a programmatical way.&lt;br /&gt;&lt;code&gt;AWS::S3::Service.buckets.each {|b| puts b.name}&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;ogg.twitteraudio.com&lt;br /&gt;t1000&lt;br /&gt;t2000&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;4) Add a new bucket called t3000.&lt;br /&gt;&lt;code&gt;AWS::S3::Bucket.create('t3000')&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; true&lt;br /&gt;&lt;br /&gt;5) Observe adding the bucket again doesn't cause an error.&lt;br /&gt;&lt;code&gt;AWS::S3::Bucket.create('t3000')&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; true&lt;br /&gt;&lt;br /&gt;6) View the buckets again. &lt;br /&gt;&lt;code&gt;AWS::S3::Service.buckets&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; [#&lt;AWS::S3::Bucket:0xb75cc850 @object_cache=[], @attributes={"name"=&gt;"ogg.twitteraudio.com", "creation_date"=&gt;Sat Apr 26 10:40:16 UTC 2008}&gt;, #&lt;AWS::S3::Bucket:0xb75cc83c @object_cache=[], @attributes={"name"=&gt;"t1000", "creation_date"=&gt;Fri Apr 25 21:35:21 UTC 2008}&gt;, #&lt;AWS::S3::Bucket:0xb75cc814 @object_cache=[], @attributes={"name"=&gt;"t2000", "creation_date"=&gt;Fri Apr 25 21:53:15 UTC 2008}&gt;]&lt;br /&gt;&lt;br /&gt;Note: You would expect t3000 to be in there however it didn't appear possibly because of the bucket permissions.&lt;br /&gt;&lt;br /&gt;7) Let's then look for bucket t3000.&lt;br /&gt;&lt;code&gt;t3000 = AWS::S3::Bucket.find('t3000')&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; #&lt;AWS::S3::Bucket:0xb76df724 @object_cache=[], @attributes={"prefix"=&gt;nil, "name"=&gt;"t3000", "marker"=&gt;nil, "max_keys"=&gt;1000, "is_truncated"=&gt;false, "xmlns"=&gt;"http://s3.amazonaws.com/doc/2006-03-01/"}&gt;&lt;br /&gt;&lt;br /&gt;8) Now that we've found the bucket let's upload a text file called works.txt.&lt;br /&gt;&lt;code&gt;file = "works.txt"&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; "works.txt"&lt;br /&gt;&lt;code&gt;AWS::S3::S3Object.store(file, open(file), 't3000', :access =&gt; :public_read)&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;=&gt; #&lt;AWS::S3::S3Object::Response:0x-608926458 200 OK&gt;&lt;br /&gt;&lt;br /&gt;9) Setting the file access to :public_read allows us to view the file from the http location http://t3000.s3.amazonaws.com/works.txt&lt;br /&gt;&lt;br /&gt;References: &lt;br /&gt;&lt;a href="http://amazon.rubyforge.org/"&gt;http://amazon.rubyforge.org/&lt;/a&gt;&lt;br /&gt;&lt;a href="http://snippets.dzone.com/posts/show/4088"&gt;upload_to_s3 - Ruby S3 upload client&lt;/a&gt; [dzone.com]&lt;br /&gt;&lt;br /&gt;*update: 14:30 30 April 2008 *&lt;br /&gt;I didn't use Bucket.objects(:reload) which is the reason why the bucket t3000 didn't show up with the statement Service.buckets&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://spattendesign.com/2007/12/5/amazon-s3-ruby-and-rails-slides"&gt;spatten design - Amazon S3, Ruby and Rails slides&lt;/a&gt; [spattendesign.com]</description>
      <pubDate>Tue, 29 Apr 2008 17:20:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5441</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Using ruby to upload multiple files to Amazon S3</title>
      <link>http://snippets.dzone.com/posts/show/5431</link>
      <description>This ruby code builds upon the code &lt;a href="http://snippets.dzone.com/posts/show/3062"&gt;S3 upload client for Ruby&lt;/a&gt; [dzone.com] by reading the account details and file details from XML files.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;#file : rubys3file2upload.rb&lt;br /&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'aws/s3'&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class S3FileUpload&lt;br /&gt;&lt;br /&gt;  def initialize(xml_accounts_file, xml_upload_file)&lt;br /&gt;    initialize_account(xml_accounts_file)&lt;br /&gt;    main(xml_upload_file)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def initialize_account(accounts_file)&lt;br /&gt;&lt;br /&gt;    file = File.new(accounts_file)&lt;br /&gt;    doc = Document.new(file)&lt;br /&gt;&lt;br /&gt;    account = doc.root.elements['records/access']&lt;br /&gt;    h = Hash.new&lt;br /&gt;    h[:access_key_id] = account.elements['key_id'].text.to_s&lt;br /&gt;    h[:secret_access_key] = account.elements['secret'].text.to_s&lt;br /&gt;    AWS::S3::Base.establish_connection!(h)&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def main(xml_upload_file)&lt;br /&gt;    file = File.new(xml_upload_file)&lt;br /&gt;    doc = Document.new(file)&lt;br /&gt;puts doc&lt;br /&gt;    doc.root.elements.each('records/file') do |f|&lt;br /&gt;      local_file = f.elements['local'].text.to_s&lt;br /&gt;      bucket = f.elements['bucket'].text.to_s&lt;br /&gt;      mime_type = f.elements['mime_type'].text.to_s&lt;br /&gt;      upload(local_file, mime_type, bucket)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def upload(local_file, mime_type, bucket)&lt;br /&gt;&lt;br /&gt;    base_name = File.basename(local_file)&lt;br /&gt;&lt;br /&gt;    puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"&lt;br /&gt;&lt;br /&gt;    AWS::S3::S3Object.store(&lt;br /&gt;      base_name,&lt;br /&gt;      File.open(local_file),&lt;br /&gt;      bucket,&lt;br /&gt;      :content_type =&gt; mime_type,&lt;br /&gt;      :access =&gt; :public_read&lt;br /&gt;    )&lt;br /&gt;&lt;br /&gt;    puts "Uploaded!"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;  s3fu = S3FileUpload.new('s3accounts.xml', 's3files2upload.xml')&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: s3accounts.xml&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;s3accounts&gt;&lt;br /&gt;  &lt;summary/&gt;&lt;br /&gt;  &lt;records&gt;&lt;br /&gt;    &lt;access id="100"&gt;&lt;name&gt;REPLACE_ME&lt;/name&gt;&lt;key_id&gt;REPLACE_ME&lt;/key_id&gt;&lt;secret&gt;REPLACE_ME&lt;/secret&gt;&lt;/access&gt;&lt;br /&gt;  &lt;/records&gt;&lt;br /&gt;&lt;/s3accounts&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: s3files2upload.xml&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;s3files2upload&gt;&lt;br /&gt;  &lt;summary/&gt;&lt;br /&gt;  &lt;records&gt;&lt;br /&gt;    &lt;file&gt;&lt;br /&gt;      &lt;local&gt;autocomplete.html&lt;/local&gt;&lt;br /&gt;      &lt;bucket&gt;t2000&lt;/bucket&gt;&lt;br /&gt;      &lt;mime_type&gt;text/html&lt;/mime_type&gt;&lt;br /&gt;    &lt;/file&gt;&lt;br /&gt;    &lt;file&gt;&lt;br /&gt;      &lt;local&gt;autocomplete2.html&lt;/local&gt;&lt;br /&gt;      &lt;bucket&gt;t2000&lt;/bucket&gt;&lt;br /&gt;      &lt;mime_type&gt;text/html&lt;/mime_type&gt;&lt;br /&gt;    &lt;/file&gt;&lt;br /&gt;    &lt;file&gt;&lt;br /&gt;      &lt;local&gt;autocomplete3.html&lt;/local&gt;&lt;br /&gt;      &lt;bucket&gt;t2000&lt;/bucket&gt;&lt;br /&gt;      &lt;mime_type&gt;text/html&lt;/mime_type&gt;&lt;br /&gt;    &lt;/file&gt;&lt;br /&gt;  &lt;/records&gt;&lt;br /&gt;&lt;/s3files2upload&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note: The file given the correct permission could be read from http://t2000.s3.amazonaws.com/autocomplete.html&lt;br /&gt;&lt;br /&gt;Reference: http://amazon.rubyforge.org/</description>
      <pubDate>Sat, 26 Apr 2008 14:17:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5431</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Simple S3 utils - copy bucket to bucket</title>
      <link>http://snippets.dzone.com/posts/show/4935</link>
      <description>Some simple S3 utils in Ruby&lt;br /&gt;&lt;br /&gt;Specifically written to copy one bucket to another (for testing on production on staging)&lt;br /&gt;&lt;br /&gt;PLEASE BE CAREFUL WITH THIS - THERE IS CODE THAT DELETES ALL CONTENTS OF A BUCKET&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;a = AmazoneS3Asset.new&lt;br /&gt;a.copy_over_bucket("myapp_production", "myapp_production")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'aws/s3'&lt;br /&gt;require 'mechanize'&lt;br /&gt;&lt;br /&gt;class AmazonS3Asset&lt;br /&gt;  &lt;br /&gt;  include AWS::S3&lt;br /&gt;  S3ID = "your s3 id"&lt;br /&gt;  S3KEY = "your s3 key"&lt;br /&gt;  &lt;br /&gt;  def initialize&lt;br /&gt;    puts "connecting..."&lt;br /&gt;    AWS::S3::Base.establish_connection!(&lt;br /&gt;      :access_key_id     =&gt; S3ID,&lt;br /&gt;      :secret_access_key =&gt; S3KEY&lt;br /&gt;    )&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def delete_key(bucket, key)&lt;br /&gt;    if exists?(bucket, key) &lt;br /&gt;      S3Object.delete key, bucket&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def empty_bucket(bucket)&lt;br /&gt;    bucket_keys(bucket).each do |k|&lt;br /&gt;      puts "deleting #{k}"&lt;br /&gt;      delete_key(bucket,k)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def bucket_keys(bucket)&lt;br /&gt;    b = Bucket.find(bucket)&lt;br /&gt;    b.objects.collect {|o| o.key}&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def copy_over_bucket(from_bucket, to_bucket)&lt;br /&gt;    puts "Replacing #{to_bucket} with contents of #{from_bucket}"&lt;br /&gt;    #delete to_bucket&lt;br /&gt;    empty_bucket(to_bucket)&lt;br /&gt;    bucket_keys(from_bucket).each do |k|&lt;br /&gt;      copy_between_buckets(from_bucket, to_bucket, k)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def copy_between_buckets(from_bucket, to_bucket, from_key, to_key = nil)&lt;br /&gt;    if exists?(from_bucket, from_key)&lt;br /&gt;      to_key = from_key if to_key.nil?&lt;br /&gt;      puts "Copying #{from_bucket}.#{from_key} to #{to_bucket}.#{to_key}"&lt;br /&gt;      url = "http://s3.amazonaws.com/#{from_bucket}/#{from_key}"&lt;br /&gt;      filename = download(url)&lt;br /&gt;      store_file(to_bucket,to_key,filename)&lt;br /&gt;      File.delete(filename)&lt;br /&gt;      return "http://s3.amazonaws.com/#{to_bucket}/#{to_key}"&lt;br /&gt;    else&lt;br /&gt;      puts "#{from_bucket}.#{from_key} didn't exist"&lt;br /&gt;      return nil&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def store_file(bucket, key, filename)&lt;br /&gt;     puts "Storing #{filename} in #{bucket}.#{key}"&lt;br /&gt;     S3Object.store(&lt;br /&gt;      key,&lt;br /&gt;      File.open(filename),&lt;br /&gt;      bucket,&lt;br /&gt;      :access =&gt; :public_read&lt;br /&gt;      )&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def download(url, save_as = nil)&lt;br /&gt;    if save_as.nil?&lt;br /&gt;      Dir.mkdir("amazon_s3_temp") if !File.exists?("amazon_s3_temp")&lt;br /&gt;      save_as = File.join("amazon_s3_temp",File.basename(url))&lt;br /&gt;    end&lt;br /&gt;    begin&lt;br /&gt;      puts "Saving #{url} to #{save_as}"&lt;br /&gt;      agent = WWW::Mechanize.new {|a| a.log = Logger.new(STDERR) }&lt;br /&gt;      img = agent.get(url)&lt;br /&gt;      img.save_as(save_as)&lt;br /&gt;      return save_as&lt;br /&gt;    rescue&lt;br /&gt;      raise "Failed on " + url + "  " + save_as&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def exists?(bucket,key)&lt;br /&gt;    begin&lt;br /&gt;      res = S3Object.find key, bucket&lt;br /&gt;    rescue &lt;br /&gt;      res = nil&lt;br /&gt;    end&lt;br /&gt;    return !res.nil?&lt;br /&gt;  end&lt;br /&gt;      &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 30 Dec 2007 20:13:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4935</guid>
      <author>wiseleyb (ben)</author>
    </item>
    <item>
      <title>upload_to_s3 - Ruby S3 upload client</title>
      <link>http://snippets.dzone.com/posts/show/4088</link>
      <description>Prerequisites:&lt;br /&gt;gem install aws-s3&lt;br /&gt;gem install main&lt;br /&gt;&lt;br /&gt;&lt;code&gt;#!/bin/env ruby&lt;br /&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'main'&lt;br /&gt;require 'aws/s3'&lt;br /&gt;include AWS::S3&lt;br /&gt;&lt;br /&gt;Main {&lt;br /&gt;  argument('source_filename') {&lt;br /&gt;    cast :string&lt;br /&gt;    description 'source filename to copy to S3'&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  argument('bucket_name') {&lt;br /&gt;    cast :string&lt;br /&gt;    description 'bucket to place the file in on S3'&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  option('access_key_id') {&lt;br /&gt;    argument :optional&lt;br /&gt;    description 'specify the access_key_id manually'&lt;br /&gt;    default 'put your access key here if you want'&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  option('secret_access_key') {&lt;br /&gt;    argument :optional&lt;br /&gt;    description 'specify the secret key manually'&lt;br /&gt;    default 'put your secret key here if you want'&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  def run&lt;br /&gt;    bucket_name = params['bucket_name'].value&lt;br /&gt;    source_filename = params['source_filename'].value&lt;br /&gt;&lt;br /&gt;    Base.establish_connection!(&lt;br /&gt;      :access_key_id     =&gt; params['access_key_id'].value,&lt;br /&gt;      :secret_access_key =&gt; params['secret_access_key'].value&lt;br /&gt;    )&lt;br /&gt;&lt;br /&gt;    begin&lt;br /&gt;      Bucket.find(bucket_name)&lt;br /&gt;    rescue&lt;br /&gt;      puts "Need to make bucket #{bucket_name}.."&lt;br /&gt;      Bucket.create(bucket_name)&lt;br /&gt;&lt;br /&gt;      # Confirm its existence..&lt;br /&gt;      Bucket.find(bucket_name)&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    puts "Got bucket.."&lt;br /&gt;    puts "Uploading #{File.basename(source_filename)}.."&lt;br /&gt;    S3Object.store(File.basename(source_filename), open(source_filename), bucket_name)&lt;br /&gt;    puts "Stored!"&lt;br /&gt;&lt;br /&gt;    exit_success!&lt;br /&gt;  end&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Fri, 01 Jun 2007 17:21:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4088</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Ruby Client for Amazon Alexa Site Thumbnail (AST) Service</title>
      <link>http://snippets.dzone.com/posts/show/3087</link>
      <description>It's scrappy, but it does the job.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'cgi'&lt;br /&gt;require 'openssl'&lt;br /&gt;require 'base64'&lt;br /&gt;require 'open-uri'&lt;br /&gt;&lt;br /&gt;access_id = 'YOUR_ACCESS_ID'&lt;br /&gt;secret_id = 'YOUR_SECRET_ID'&lt;br /&gt;&lt;br /&gt;source_url = ARGV.first&lt;br /&gt;&lt;br /&gt;timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")&lt;br /&gt;sig = Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::Digest.new('SHA1'), secret_id, 'Thumbnail' + timestamp)).strip&lt;br /&gt;&lt;br /&gt;url = "http://ast.amazonaws.com/Xino?Action=Thumbnail&amp;AWSAccessKeyId=" + access_id&lt;br /&gt;url &lt;&lt; "&amp;Signature=" + CGI.escape(sig)&lt;br /&gt;url &lt;&lt; "&amp;Timestamp=" + CGI.escape(timestamp)&lt;br /&gt;url &lt;&lt; "&amp;Url=" +  source_url&lt;br /&gt;&lt;br /&gt;begin&lt;br /&gt;  doc = open(url).read&lt;br /&gt;rescue&lt;br /&gt;  puts "Could not access AWS"&lt;br /&gt;  exit&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;m = doc.match(/\&lt;aws:thumbnail[^\&gt;]+exists=\"true\"\&gt;(.+?)\&lt;\//i)&lt;br /&gt;&lt;br /&gt;if m &amp;&amp; m[1]&lt;br /&gt;  thumb_url = m[1]&lt;br /&gt;  thumb_url.gsub!(/\&amp;amp;/, '&amp;')&lt;br /&gt;  File.open("#{source_url}.jpg", "w") { |f| f.write open(thumb_url).read }&lt;br /&gt;  puts "Saved to #{source_url}.jpg"&lt;br /&gt;elsif m &amp;&amp; m.match(/exists=\"false\"/)&lt;br /&gt;  puts "No thumbnail for #{source_url}"&lt;br /&gt;else&lt;br /&gt;  puts "Error"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 05 Dec 2006 18:31:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3087</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>S3 upload client for Ruby</title>
      <link>http://snippets.dzone.com/posts/show/3062</link>
      <description>Uses Marcel Molina's AWS::S3 gem.. gem install aws-s3&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'aws/s3'&lt;br /&gt;&lt;br /&gt;local_file = ARGV[0]&lt;br /&gt;bucket = ARGV[1]&lt;br /&gt;mime_type = ARGV[2] || "application/octet-stream"&lt;br /&gt;&lt;br /&gt;AWS::S3::Base.establish_connection!(&lt;br /&gt;  :access_key_id     =&gt; 'REPLACE_ME',&lt;br /&gt;  :secret_access_key =&gt; 'REPLACE_ME'&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;base_name = File.basename(local_file)&lt;br /&gt;&lt;br /&gt;puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"&lt;br /&gt;&lt;br /&gt;AWS::S3::S3Object.store(&lt;br /&gt;  base_name,&lt;br /&gt;  File.open(local_file),&lt;br /&gt;  bucket,&lt;br /&gt;  :content_type =&gt; mime_type&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;puts "Uploaded!"&lt;/code&gt;</description>
      <pubDate>Thu, 30 Nov 2006 10:27:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3062</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Amazon REST API for newLISP</title>
      <link>http://snippets.dzone.com/posts/show/2812</link>
      <description>&lt;code&gt;&lt;br /&gt;;; (amazon-op "Operation=ItemSearch&amp;SearchIndex=Books&amp;ItemPage=1&amp;Keywords=Cloud+Atlas&amp;Respon\&lt;br /&gt;seGroup=Request,Small")&lt;br /&gt;;;&lt;br /&gt;;; (amazon-op "Operation=SimilarityLookup&amp;ItemId=1400063795,0812966929&amp;SimilarityType=Random\&lt;br /&gt;&amp;ResponseGroup=Request,Large")&lt;br /&gt;;; (amazon-op "Operation=SimilarityLookup&amp;ItemId=1400063795,061873516X,0812966929&amp;Similarity\&lt;br /&gt;Type=Random&amp;ResponseGroup=Request,Small")&lt;br /&gt;&lt;br /&gt;;;  (setq x (xml-digest (amazon-op "Operation=ItemLookup&amp;ItemId=0375507256&amp;ResponseGroup=Req\&lt;br /&gt;uest,Similarities,ListmaniaLists")))&lt;br /&gt;;; Using x get a list of listmanias for an item&lt;br /&gt;;;  (map (lambda (x) (x 1 2 2)) (1 -1 (x (chop (ref 'ListmaniaLists  x)))))&lt;br /&gt;&lt;br /&gt;;; (setq y (amazon-op "Operation=ListLookup&amp;ListType=Listmania&amp;ListId=R21LV6VJEZ794O&amp;Respons\&lt;br /&gt;eGroup=ListFull"))&lt;br /&gt;;; Using y get a list of ISBNs from a listmania&lt;br /&gt;;; (map (lambda(x) (x 4 1 2 )) (8 -1 ( (y (chop (ref 'Lists y)) ) 2))) ; gives list of ISBNs\&lt;br /&gt; from the listmania&lt;br /&gt;&lt;br /&gt;;; get a list of listmania details from a list of listmanias.&lt;br /&gt;;;  (setq z   (map (lambda (x) (xml-digest (amazon-op (append "Operation=ListLookup&amp;ListType\&lt;br /&gt;=Listmania&amp;ListId=" x "&amp;ResponseGroup=ListFull"))))  (map (lambda (x) (x 1 2 2)) (1 -1 (x (c\&lt;br /&gt;hop (ref 'ListmaniaLists  x))))))  )&lt;br /&gt;;;&lt;br /&gt;;; get a list of ISBNs from all of the listmanias for the item&lt;br /&gt;;; (setq z (flat (map (lambda(y) (map (lambda(x) (x 4 1 2 )) (8 -1 ( (y (chop (ref 'Lists y)\&lt;br /&gt;) ) 2))) ) z)))&lt;br /&gt;;;&lt;br /&gt;&lt;br /&gt;;; get item info for all the ISBNs from all listmanias related to the original item&lt;br /&gt;;; (setq w (map (lambda (x) (xml-digest (amazon-op (append "Operation=ItemLookup&amp;ItemId=" x)\&lt;br /&gt;)))  (unique z)))&lt;br /&gt;&lt;br /&gt;;; get a list of pair (title,author) from w above.&lt;br /&gt;;; (map (lambda (x) (list ((x (chop (ref 'Title x))) 1)  (if (ref 'Author x)((x (chop (ref '\&lt;br /&gt;Author x)))1) "???"))) w)&lt;br /&gt;&lt;br /&gt;(define (amazon-op params)&lt;br /&gt;  (get-url (append &lt;br /&gt;"http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&amp;&amp;AWSAccessKeyId=YOUROWNKEY&amp;" params)))&lt;br /&gt;&lt;br /&gt;(define (xml-digest result)&lt;br /&gt;  (xml-type-tags nil nil nil nil)&lt;br /&gt;  (setq xresult (xml-parse result (+ 1 2 4 8 16))))&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 13 Oct 2006 10:16:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2812</guid>
      <author>frontera000 (bob bae)</author>
    </item>
    <item>
      <title>library mashup</title>
      <link>http://snippets.dzone.com/posts/show/2737</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;;; combine APIs and other web available bibliographic data.&lt;br /&gt;;; Use amazon, LibraryThing, and OCLC Worldcat.&lt;br /&gt;;; Get ISBN numbers for a book by searching author and title.&lt;br /&gt;;; Get the list of available ISBNs from different sources.&lt;br /&gt;;; Get "tags" available for a book from LibraryThing for&lt;br /&gt;;; a given book.&lt;br /&gt;;; etc.&lt;br /&gt;;;&lt;br /&gt;;; N.B. A person from LibraryThing.com posted a comment&lt;br /&gt;;; regarding this program, indicating that the tag cloud data&lt;br /&gt;;; is copyrighted. &lt;br /&gt;;;&lt;br /&gt;;; Here's the quoted message:&lt;br /&gt;;;&lt;br /&gt;;; Tim's message: "And it's fine here, as a test, and probably in other &lt;br /&gt;;; contexts where it could be considered "fair use" under copyright. &lt;br /&gt;;; But--unlike the thingISBN service and some other LibraryThing &lt;br /&gt;;; APIs--LibraryThing's tag clouds are not free for public use, &lt;br /&gt;;; outside of the RSS feeds and widgets we provide. &lt;br /&gt;;; We're not sure how and under what license to &lt;br /&gt;;; release them when we do."&lt;br /&gt;;;&lt;br /&gt;;; So, I wrote this for fun.  If I were you I wouldn't run it, just read&lt;br /&gt;;; the code.  :-)&lt;br /&gt;;;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(define (search-worldcat title author)&lt;br /&gt;  (setq title (replace " " title "+"))&lt;br /&gt;  (setq author (replace " " author "+"))&lt;br /&gt;  (get-url (append "http://worldcatlibraries.org/search?q=ti:" title "+au:" author "&amp;qt=advanced")))&lt;br /&gt;&lt;br /&gt;(setq oclc-url-1-pattern {&lt;div class="name"&gt;&lt;a href=})&lt;br /&gt;&lt;br /&gt;(define (oclc-url-1 str)&lt;br /&gt;  (setq loc (find oclc-url-1-pattern str))&lt;br /&gt;  (setq loc (+ loc (length oclc-url-1-pattern)))&lt;br /&gt;  (setq loc (+ 1 loc))&lt;br /&gt;  (setq loc-end (find "&gt;" (loc -1 str)))&lt;br /&gt;  (setq loc-end (- loc-end 1))&lt;br /&gt;  (setq loc-url (loc loc-end str))&lt;br /&gt;  (println (append "http://worldcatlibraries.org" loc-url))&lt;br /&gt;  (get-url (append "http://worldcatlibraries.org" loc-url)))&lt;br /&gt;&lt;br /&gt;(setq oclc-isbn-pattern "&lt;strong&gt;ISBN: &lt;/strong&gt;")&lt;br /&gt;&lt;br /&gt;(define (find-isbn str)&lt;br /&gt;  (setq loc (find oclc-isbn-pattern str))&lt;br /&gt;  (setq loc (+ loc (length oclc-isbn-pattern)))&lt;br /&gt;  (setq loc-end (find "&lt;/li&gt;" (loc -1 str)))&lt;br /&gt;  (loc loc-end str))&lt;br /&gt;&lt;br /&gt;(define (thing-isbn isbn)&lt;br /&gt;  (xml-type-tags nil nil nil nil)&lt;br /&gt;  (setq isbn-data &lt;br /&gt;      (xml-parse (get-url  &lt;br /&gt;          (append "http://www.librarything.com/api/thingISBN/" isbn)))))&lt;br /&gt;&lt;br /&gt;(define (get-isbn-list isbn-data)&lt;br /&gt;  (setq indexer (ref "isbn" isbn-data))&lt;br /&gt;  (nth-set 2 indexer 2)&lt;br /&gt;  (setq num-isbns (length  (isbn-data 0)))&lt;br /&gt;  (setq isbn-list '())&lt;br /&gt;  (for (idx (first (1 indexer))  (- num-isbns 1))&lt;br /&gt;        (push (isbn-data (first (0 indexer)) idx (first (2 indexer))) isbn-list -1))&lt;br /&gt;   isbn-list)&lt;br /&gt;&lt;br /&gt;(define (add-explorer-path)&lt;br /&gt;   (env "PATH" (append (env "PATH" ) ";c:\\program files\\internet explorer")))&lt;br /&gt;&lt;br /&gt;(define (show-amazon-isbn isbn)&lt;br /&gt;   (process (append "iexplore " "http://www.amazon.com/exec/obidos/ASIN/"&lt;br /&gt;           isbn )))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(define (get-librarything-isbn isbn)&lt;br /&gt;    (get-url (append "http://librarything.com/isbn/" isbn)))&lt;br /&gt;&lt;br /&gt;(define (get-librarything-tagsection str)&lt;br /&gt;    (setq loc (find "Tags used" str ))&lt;br /&gt;    (setq mystr (loc -1 str))&lt;br /&gt;    (setq loc-end (find "&lt;/div&gt;" mystr))&lt;br /&gt;     (0 loc-end mystr))&lt;br /&gt;&lt;br /&gt;(define (get-librarything-taglist mystr)&lt;br /&gt;    (setq tags-list '())&lt;br /&gt;    (while (setq tag-loc (find "/tag/" mystr))&lt;br /&gt;        (setq tag-loc-end (find "target=" (tag-loc -1 mystr)))&lt;br /&gt;        (push ((+ tag-loc 5) (- tag-loc-end 7) mystr) tags-list -1)&lt;br /&gt;        (setq mystr ((+ tag-loc tag-loc-end ) -1 mystr)))&lt;br /&gt;    tags-list)&lt;br /&gt;&lt;br /&gt;(define (show-librarything-tag tag)&lt;br /&gt;   (process (append "iexplore " "http://www.librarything.com/tag/"&lt;br /&gt;           tag )))&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 30 Sep 2006 03:52:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2737</guid>
      <author>frontera000 (bob bae)</author>
    </item>
    <item>
      <title>Amazon Statistically Improbable Phrases Search Bookmarklet</title>
      <link>http://snippets.dzone.com/posts/show/177</link>
      <description>&lt;code&gt;javascript:q=document.getSelection();for(i=0;i&lt;frames.length;i++){q=frames[i].document.getSelection();if(q)break;}if(!q)void(q=prompt('Statistically Improbable Phrase:',''));if(q)location.href='http://www.amazon.com/gp/phrase/'+escape(q)+'/ref=sip_bod_0/103-6410487-8187016'&lt;/code&gt;</description>
      <pubDate>Sat, 16 Apr 2005 01:58:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/177</guid>
      <author>sage (Sage)</author>
    </item>
  </channel>
</rss>
