| 1 | ## @file | ||
| 2 | # (Enter your file info here) | ||
| 3 | # | ||
| 4 | # $Id: S3.pm 432 2008-05-02 19:17:09Z damjan $ | ||
| 5 | |||
| 6 | # sample usage: | ||
| 7 | # my $response; | ||
| 8 | # my $s3 = RWDE::DB::S3->new($AWS_ACCESS_KEY_ID, $AWS_SECRET_ACCESS_KEY); | ||
| 9 | # $response = $s3->createBucket($BUCKET_NAME); | ||
| 10 | # $response = $s3->putObject($BUCKET_NAME, $KEY_NAME, 'text/plain', 'file data string'); | ||
| 11 | # $response = $s3->getObject($BUCKET_NAME, $KEY_NAME); | ||
| 12 | # print "response: -".$response->content."-\n"; | ||
| 13 | # $response = $s3->deleteObject($BUCKET_NAME, $KEY_NAME); | ||
| 14 | # $response = $s3->deleteBucket($BUCKET_NAME); | ||
| 15 | |||
| 16 | ## @class RWDE::DB::S3 | ||
| 17 | # (Enter RWDE::DB::S3 info here) | ||
| 18 | package RWDE::DB::S3; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | |||
| 23 | use Error qw(:try); | ||
| 24 | use RWDE::Exceptions; | ||
| 25 | |||
| 26 | use Data::Dumper; | ||
| 27 | use LWP::UserAgent; | ||
| 28 | use Digest::HMAC_SHA1; | ||
| 29 | use HTTP::Date; | ||
| 30 | use MIME::Base64 qw(encode_base64); | ||
| 31 | |||
| 32 | use RWDE::Configuration; | ||
| 33 | |||
| 34 | use vars qw($VERSION); | ||
| 35 | $VERSION = sprintf "%d", q$Revision: 508 $ =~ /(\d+)/; | ||
| 36 | |||
| 37 | ## @cmethod object new() | ||
| 38 | # (Enter new info here) | ||
| 39 | # @return (Enter explanation for return value here) | ||
| 40 | sub new { | ||
| 41 | my $proto = shift; | ||
| 42 | my $class = ref($proto) || $proto; | ||
| 43 | my $self = {}; | ||
| 44 | $self->{AWS_ACCESS_KEY_ID} = RWDE::Configuration->AccessKeyID; | ||
| 45 | $self->{AWS_SECRET_ACCESS_KEY} = RWDE::Configuration->SecretAccessKey; | ||
| 46 | $self->{AGENT} = LWP::UserAgent->new(); | ||
| 47 | bless($self, $class); | ||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | ## @method object setACL() | ||
| 52 | # (Enter setACL info here) | ||
| 53 | # @return (Enter explanation for return value here) | ||
| 54 | sub setACL { | ||
| 55 | my ($self, $bucketName, $keyName, $acl) = @_; | ||
| 56 | my $xml_acl = ''; #replace with xml data string | ||
| 57 | return $self->_create_message('PUT', "$bucketName/$keyName?acl", {}, $xml_acl); | ||
| 58 | } | ||
| 59 | |||
| 60 | ## @method object listBucket() | ||
| 61 | # (Enter listBucket info here) | ||
| 62 | # @return (Enter explanation for return value here) | ||
| 63 | sub listBucket { | ||
| 64 | my ($self, $bucketName) = @_; | ||
| 65 | #options can be added to the path here, $bucketName?... | ||
| 66 | return $self->_create_message('GET', $bucketName); | ||
| 67 | } | ||
| 68 | |||
| 69 | ## @method object createBucket() | ||
| 70 | # (Enter createBucket info here) | ||
| 71 | # @return (Enter explanation for return value here) | ||
| 72 | sub createBucket { | ||
| 73 | my ($self, $bucketName) = @_; | ||
| 74 | return $self->_create_message('PUT', $bucketName); | ||
| 75 | } | ||
| 76 | |||
| 77 | ## @method object deleteBucket() | ||
| 78 | # (Enter deleteBucket info here) | ||
| 79 | # @return (Enter explanation for return value here) | ||
| 80 | sub deleteBucket { | ||
| 81 | my ($self, $bucketName) = @_; | ||
| 82 | return $self->_create_message('DELETE', $bucketName); | ||
| 83 | } | ||
| 84 | |||
| 85 | ## @method object putObject() | ||
| 86 | # (Enter putObject info here) | ||
| 87 | # @return (Enter explanation for return value here) | ||
| 88 | sub putObject { | ||
| 89 | my ($self, $bucketName, $keyName, $contentType, $data, $acl) = @_; | ||
| 90 | $acl ||= 'public-read'; | ||
| 91 | return $self->_create_message('PUT', "$bucketName/$keyName", { "Content-Type" => $contentType }, $data, $acl); | ||
| 92 | } | ||
| 93 | |||
| 94 | ## @method object getObject() | ||
| 95 | # (Enter getObject info here) | ||
| 96 | # @return (Enter explanation for return value here) | ||
| 97 | sub getObject { | ||
| 98 | my ($self, $bucketName, $keyName) = @_; | ||
| 99 | return $self->_create_message('GET', "$bucketName/$keyName"); | ||
| 100 | } | ||
| 101 | |||
| 102 | ## @method object deleteObject() | ||
| 103 | # (Enter deleteObject info here) | ||
| 104 | # @return (Enter explanation for return value here) | ||
| 105 | sub deleteObject { | ||
| 106 | my ($self, $bucketName, $keyName) = @_; | ||
| 107 | return $self->_create_message('DELETE', "$bucketName/$keyName"); | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | ## @method protected object _create_message() | ||
| 112 | # (Enter _create_message info here) | ||
| 113 | # @return (Enter explanation for return value here) | ||
| 114 | sub _create_message { | ||
| 115 | my ($self, $method, $path, $headers, $data, $acl) = @_; | ||
| 116 | $headers ||= {}; | ||
| 117 | $data ||= ''; | ||
| 118 | # $acl ||= 'public-read'; | ||
| 119 | |||
| 120 | # add any headers we were given to our header object | ||
| 121 | my $http_header = HTTP::Headers->new; | ||
| 122 | while (my ($k, $v) = each %$headers) { | ||
| 123 | $http_header->header($k => $v); | ||
| 124 | } | ||
| 125 | |||
| 126 | # header must have a date, add if it we don't have one yet | ||
| 127 | if (not $http_header->header('Date')) { | ||
| 128 | $http_header->header(Date => time2str(time)); | ||
| 129 | } | ||
| 130 | |||
| 131 | #Make the objects readable | ||
| 132 | if(defined($acl)) { | ||
| 133 | $http_header->header('x-amz-acl' => $acl); | ||
| 134 | } | ||
| 135 | |||
| 136 | # add content length header | ||
| 137 | if (length($data) > 0) { | ||
| 138 | $http_header->header('content-length' => length($data)); | ||
| 139 | } | ||
| 140 | |||
| 141 | # hash our request with our secret access key so amazon knows we're legit | ||
| 142 | my $canonical_string = canonical_string($method, $path, $http_header); | ||
| 143 | my $hmac = Digest::HMAC_SHA1->new($self->{AWS_SECRET_ACCESS_KEY}); | ||
| 144 | $hmac->add($canonical_string); | ||
| 145 | my $signature = encode_base64($hmac->digest, ''); | ||
| 146 | $http_header->header(Authorization => "AWS $self->{AWS_ACCESS_KEY_ID}:$signature"); | ||
| 147 | |||
| 148 | # create the actual request | ||
| 149 | my $url = "https://s3.amazonaws.com:443/$path"; | ||
| 150 | my $request = HTTP::Request->new($method, $url, $http_header); | ||
| 151 | $request->content($data); | ||
| 152 | |||
| 153 | # adios, bon voyage | ||
| 154 | my $response = $self->{AGENT}->request($request); | ||
| 155 | throw RWDE::DataBadException({ info => $response->content }) unless $response->is_success; | ||
| 156 | |||
| 157 | return $response; | ||
| 158 | } | ||
| 159 | |||
| 160 | ## @method object trim() | ||
| 161 | # (Enter trim info here) | ||
| 162 | # @return (Enter explanation for return value here) | ||
| 163 | sub trim { | ||
| 164 | my ($value) = @_; | ||
| 165 | |||
| 166 | $value =~ s/^\s+//; | ||
| 167 | $value =~ s/\s+$//; | ||
| 168 | return $value; | ||
| 169 | } | ||
| 170 | |||
| 171 | ## @method object canonical_string() | ||
| 172 | # (Enter canonical_string info here) | ||
| 173 | # @return (Enter explanation for return value here) | ||
| 174 | sub canonical_string { | ||
| 175 | my ($method, $path, $headers, $expires) = @_; | ||
| 176 | my %interesting_headers = (); | ||
| 177 | while (my ($key, $value) = each %$headers) { | ||
| 178 | my $lk = lc $key; | ||
| 179 | if ( $lk eq 'content-md5' | ||
| 180 | or $lk eq 'content-type' | ||
| 181 | or $lk eq 'date' | ||
| 182 | or $lk =~ /^x-amz-/) { | ||
| 183 | $interesting_headers{$lk} = trim($value); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | # these keys get empty strings if they don't exist | ||
| 188 | $interesting_headers{'content-type'} ||= ''; | ||
| 189 | $interesting_headers{'content-md5'} ||= ''; | ||
| 190 | |||
| 191 | # just in case someone used this. it's not necessary in this lib. | ||
| 192 | $interesting_headers{'date'} = '' if $interesting_headers{'x-amz-date'}; | ||
| 193 | |||
| 194 | # if you're using expires for query string auth, then it trumps date | ||
| 195 | # (and x-amz-date) | ||
| 196 | $interesting_headers{'date'} = $expires if $expires; | ||
| 197 | |||
| 198 | my $buf = "$method\n"; | ||
| 199 | foreach my $key (sort keys %interesting_headers) { | ||
| 200 | if ($key =~ /^x-amz-/) { | ||
| 201 | $buf .= "$key:$interesting_headers{$key}\n"; | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | $buf .= "$interesting_headers{$key}\n"; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | # don't include anything after the first ? in the resource... | ||
| 209 | $path =~ /^([^?]*)/; | ||
| 210 | $buf .= "/$1"; | ||
| 211 | |||
| 212 | # ...unless there is an acl or torrent parameter | ||
| 213 | if ($path =~ /[&?]acl($|=|&)/) { | ||
| 214 | $buf .= '?acl'; | ||
| 215 | } | ||
| 216 | elsif ($path =~ /[&?]torrent($|=|&)/) { | ||
| 217 | $buf .= '?torrent'; | ||
| 218 | } | ||
| 219 | elsif ($path =~ /[&?]logging($|=|&)/) { | ||
| 220 | $buf .= '?logging'; | ||
| 221 | } | ||
| 222 | |||
| 223 | return $buf; | ||
| 224 | } | ||
| 225 | |||
| 226 | 1; | ||
| 227 |