#!/usr/bin/perl -w # by Kenneth Berland use strict; use MIME::Base64; use Digest::HMAC_SHA1; use Date::Format; use LWP; use HTTP::Request; use XML::Parser; use utf8; use File::Slurp; use Getopt::Std; my %opt; getopts('dm:b:k:c:pf:',\%opt); &checkOpts; my $key = $opt{k}; my $ContentType=$opt{c};; my $HTTPVerb=$opt{m}; my $bucket=$opt{b}; my $CanonicalizedAmzHeaders="x-amz-acl:public-read\n" if $opt{p}; { my ($authorization,$Date)=&getAWSAuthorization($HTTPVerb,$bucket); my $results = &doAWSRequest($authorization,$Date,$HTTPVerb,$bucket); print $results->content if ($results->is_success); } exit; sub doAWSRequest { my ($authorization,$Date,$HTTPVerb,$bucket)=@_; my $ua=LWP::UserAgent->new; my $url="http://s3.amazonaws.com/"; $url.=$bucket if $bucket; $url.=$key if $key; print STDERR "URL: $url\n" if $opt{d}; my $req = HTTP::Request->new($HTTPVerb=>$url); $req->header( "Date" => $Date ); $req->header( "Authorization" => $authorization); $req->header( "Content-Type" => $ContentType ); $req->header( "x-amz-acl" => "public-read") if $opt{p}; if ($HTTPVerb eq "PUT" && $opt{f} && -e $opt{f}){ my $data=read_file($opt{f}); $req->content($data); } print STDERR "request->as_string: ".$req->as_string if ($opt{d}); my $res=$ua->request($req); if ($res->is_success) { print STDERR localtime()." SUCCESS:".$res->status_line."\n" if ($opt{d}); }else{ warn localtime()." FAILED:".$res->status_line."\n"; print STDERR $res->content."\n"; } $res; } sub getAWSAuthorization { my ($HTTPVerb,$bucket)=@_; my $template="%a, %d %b %Y %T %z"; my $Date = time2str($template, time); my $aws_access_key_id = "your id here"; my $aws_secret_access_key = "your key here"; my $host = "s3.amazonaws.com"; my $ContentMD5=""; my $CanonicalizedResource; $CanonicalizedResource="/"; $CanonicalizedResource.=$bucket if $opt{b}; $CanonicalizedResource.=$key if $opt{k}; my $stringToSign; $stringToSign =$HTTPVerb."\n".$ContentMD5."\n"; $stringToSign.=$ContentType if $ContentType; $stringToSign.="\n"; $stringToSign.=$Date."\n"; $stringToSign.=$CanonicalizedAmzHeaders if $CanonicalizedAmzHeaders; $stringToSign.=$CanonicalizedResource; print "StringToSign:".$stringToSign."\n" if $opt{d}; my $hmac = Digest::HMAC_SHA1->new($aws_secret_access_key); $hmac->add($stringToSign); my $signature = $hmac->b64digest; my $authorization = "AWS"." ".$aws_access_key_id.":".$signature."="; ($authorization,$Date); } sub checkOpts{ die "Method (-m) must be one of PUT, GET or DELETE\n" if (!$opt{m} || ($opt{m} ne "GET" && $opt{m} ne "PUT" && $opt{m} ne "DELETE")); }