Last month Amazon launch new service - Amazon CloudFront. Amazon CloudFront delivers your content from Amazon S3 using a global network of edge locations. Sweet, really nice solution for quite expensive Akamai (yes, I know Akamai is cheaper at some level, but startups don't have such budget). At start they release only Amazon CloudFront Authentication Tool for Curl. So decide to wrote own tool in ruby (atm there is RightScale Ruby library but when I wrote that there wasn't any such tool).
First, I try use
curb (Libcurl bindings for Ruby), but then notice that can be done more simple only with
Net::HTTPAccording to
Developer Guide correct REST Requests needs URI indicates the particular resource you want to act on, also needs correct Request Headers (Authorization, Content-Length, Content-Type, Date, Host, x-amz-date).
Date/x-amz-date like at provided by Amazon curl script, get from Amazon server:
def self.server_date(url)
server_date=''
uri = URI.parse(url)
http = Net::HTTP.new(uri.host,443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response=http.request_get(uri.path)
response['Date']
end
Authorization generate as Amazon documentation said:
def self.sign(date)
digest = OpenSSL::Digest::Digest.new('sha1')
hmacd=HMAC.new(AWS_SECRET_ACCESS_KEY, digest)
hmacd.update(date)
signature=Base64.encode64(hmacd.digest)
end
So simple list distribution can be done like that:
def ACFList(url)
date=server_date(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host,443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
headers = {
'X-AMZ-Date' => date,
'Authorization' => 'AWS ' + AWS_ACCESS_KEY_ID + ':' + sign(date).chop
}
response=http.request_get(uri.path,headers)
response
end
Example:
./acf.rb list https://cloudfront.amazonaws.com/2008-06-30/distribution
<?xml version="1.0"?>
<DistributionList xmlns="http://cloudfront.amazonaws.com/doc/2008-06-30/"><Marker></Marker><MaxItems>100</MaxItems><IsTruncated>false</IsTruncated></DistributionList>
Hole code can be find
here