| 1 | package OSS; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 0 | ▶ | use OSS::Bucket; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Carp; | ||
| 7 | use HTTP::Date; | ||
| 8 | use Digest::HMAC_SHA1; | ||
| 9 | use MIME::Base64 qw(encode_base64); | ||
| 10 | use base qw(Class::Accessor::Fast); | ||
| 11 | use HTTP::Headers; | ||
| 12 | use LWP::UserAgent::Determined; | ||
| 13 | use XML::Simple; | ||
| 14 | use URI::Escape qw(uri_escape_utf8); | ||
| 15 | |||
| 16 | my $ALI_HEADER_PREFIX ='x-oss'; | ||
| 17 | __PACKAGE__->mk_accessors( | ||
| 18 | qw(ali_access_key_id ali_secret_access_key host secure timeout ua err errstr) | ||
| 19 | ); | ||
| 20 | |||
| 21 | our $VERSION = '0.11'; | ||
| 22 | my $KEEP_ALIVE_CACHESIZE = 10; | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my $class = shift; | ||
| 26 | my $self = $class->SUPER::new(@_); | ||
| 27 | die "No ali_access_key_id" unless $self->ali_access_key_id; | ||
| 28 | die "No ali_secret_access_key" unless $self->ali_secret_access_key; | ||
| 29 | $self->secure(0) if not defined $self->secure; | ||
| 30 | $self->timeout(30) if not defined $self->timeout; | ||
| 31 | |||
| 32 | $self->host('oss-cn-hangzhou.aliyuncs.com') if not defined $self->host; | ||
| 33 | my $ua; | ||
| 34 | $ua = LWP::UserAgent->new( | ||
| 35 | keep_alive => $KEEP_ALIVE_CACHESIZE, | ||
| 36 | requests_redirectable => [qw(GET HEAD DELETE PUT)], | ||
| 37 | ); | ||
| 38 | |||
| 39 | $ua->timeout($self->timeout); | ||
| 40 | $ua->env_proxy; | ||
| 41 | $self->ua($ua); | ||
| 42 | return $self; | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | sub buckets { | ||
| 48 | my $self = shift; | ||
| 49 | my $r = $self->_send_request('GET', '', {}); | ||
| 50 | |||
| 51 | return undef unless $r; | ||
| 52 | |||
| 53 | my $owner_id = $r->{Owner}{ID}; | ||
| 54 | my $owner_displayname = $r->{Owner}{DisplayName}; | ||
| 55 | |||
| 56 | my @buckets; | ||
| 57 | |||
| 58 | if (ref $r->{Buckets}) { | ||
| 59 | my $buckets = $r->{Buckets}{Bucket}; | ||
| 60 | $buckets = [$buckets] unless ref $buckets eq 'ARRAY'; | ||
| 61 | foreach my $node (@$buckets) { | ||
| 62 | push @buckets, | ||
| 63 | OSS::Bucket->new( | ||
| 64 | { bucket => $node->{Name}, | ||
| 65 | creation_date => $node->{CreationDate}, | ||
| 66 | account => $self, | ||
| 67 | } | ||
| 68 | ); | ||
| 69 | |||
| 70 | } | ||
| 71 | } | ||
| 72 | return { | ||
| 73 | owner_id => $owner_id, | ||
| 74 | owner_displayname => $owner_displayname, | ||
| 75 | buckets => \@buckets, | ||
| 76 | }; | ||
| 77 | }; | ||
| 78 | |||
| 79 | sub list_bucket { | ||
| 80 | my ($self, $conf) = @_; | ||
| 81 | my $bucket = delete $conf->{bucket}; | ||
| 82 | croak 'must specify bucket' unless $bucket; | ||
| 83 | $conf ||= {}; | ||
| 84 | |||
| 85 | my $path = $bucket . "/"; | ||
| 86 | |||
| 87 | if (%$conf) { | ||
| 88 | $path .= "?" | ||
| 89 | .join('&', | ||
| 90 | map {$_ . "=" . $self->_urlencode($conf->{$_}) } keys %$conf ); | ||
| 91 | } | ||
| 92 | my $r = $self->_send_request('GET', $path, {}); | ||
| 93 | |||
| 94 | return undef unless $r && !$self->_remember_errors($r); | ||
| 95 | |||
| 96 | |||
| 97 | my $return = { | ||
| 98 | bucket => $r->{Name}, | ||
| 99 | prefix => $r->{Prefix}, | ||
| 100 | marker => $r->{Marker}, | ||
| 101 | next_marker => $r->{NextMarker}, | ||
| 102 | max_keys => $r->{MaxKeys}, | ||
| 103 | is_truncated => ( | ||
| 104 | scalar $r->{IsTruncated} eq 'true' | ||
| 105 | ? 1 | ||
| 106 | : 0 | ||
| 107 | ), | ||
| 108 | }; | ||
| 109 | my @keys; | ||
| 110 | for my $node (@{$r->{Contents}}) { | ||
| 111 | my $etag = $node->{ETag}; | ||
| 112 | $etag =~ s{(^"|"$)}{}g if defined $etag; | ||
| 113 | push @keys,{ | ||
| 114 | key => $node->{Key}, | ||
| 115 | last_modified => $node->{LastModified}, | ||
| 116 | etag => $etag, | ||
| 117 | size => $node->{Size}, | ||
| 118 | storage_class => $node->{StorageClass}, | ||
| 119 | owner_id => $node->{Owner}{ID}, | ||
| 120 | owner_displayname => $node->{Owner}{DisplayName}, | ||
| 121 | }; | ||
| 122 | } | ||
| 123 | $return->{keys} = \@keys; | ||
| 124 | |||
| 125 | if ($conf->{delimiter}) { | ||
| 126 | my @common_prefixes; | ||
| 127 | my $strip_delim = qr/$conf->{delimiter}$/; | ||
| 128 | foreach my $node ($r->{CommonPrefixes}) { | ||
| 129 | my $prefix = $r->{Prefix}; | ||
| 130 | $prefix =~ s/$strip_delim//; | ||
| 131 | push @common_prefixes, $prefix; | ||
| 132 | |||
| 133 | } | ||
| 134 | $return->{common_prefixes} = \@common_prefixes; | ||
| 135 | } | ||
| 136 | |||
| 137 | return $return; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub add_bucket { | ||
| 141 | my ($self, $conf) = @_; | ||
| 142 | my $bucket = $conf->{bucket}; | ||
| 143 | croak 'must specify bucket' unless $bucket; | ||
| 144 | |||
| 145 | #验证bucket访问权限 | ||
| 146 | if ($conf->{acl_short}) { | ||
| 147 | $self->_validate_acl_short($conf->{acl_short}); | ||
| 148 | } | ||
| 149 | |||
| 150 | my $header_ref = | ||
| 151 | ($conf->{acl_short}) | ||
| 152 | ? {'x-oss-acl' => $conf->{acl_short}} | ||
| 153 | : {}; | ||
| 154 | |||
| 155 | #指定bucket所在的数据中文 默认是oss-cn-hangzhou | ||
| 156 | |||
| 157 | my $data = ''; | ||
| 158 | if (defined $conf->{location_constraint}) { | ||
| 159 | $data = | ||
| 160 | "<CreateBucketConfiguration><LocationConstraint>" | ||
| 161 | . $conf->{location_constraint} | ||
| 162 | . "</LocationConstraint></CreateBucketConfiguration>"; | ||
| 163 | } | ||
| 164 | |||
| 165 | return 0 | ||
| 166 | unless $self->_send_request_expect_nothing('PUT', "$bucket/", | ||
| 167 | $header_ref, $data); | ||
| 168 | return $self->bucket($bucket); | ||
| 169 | } | ||
| 170 | |||
| 171 | sub delete_bucket { | ||
| 172 | my ($self, $conf) = @_; | ||
| 173 | my $bucket; | ||
| 174 | if (eval { $conf->isa("OSS::Bucket"); }) { | ||
| 175 | $bucket = $conf->bucket; | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | $bucket = $conf->{bucket}; | ||
| 179 | } | ||
| 180 | croak 'must specify bucket' unless $bucket; | ||
| 181 | return $self->_send_request_expect_nothing('DELETE', $bucket . "/", {}); | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | sub bucket { | ||
| 186 | my ($self, $bucketname) = @_; | ||
| 187 | return OSS::Bucket->new({bucket => $bucketname, account => $self}); | ||
| 188 | } | ||
| 189 | |||
| 190 | sub _send_request_expect_nothing { | ||
| 191 | my $self = shift; | ||
| 192 | my $request = $self->_make_request(@_); | ||
| 193 | |||
| 194 | my $response = $self->_do_http($request); | ||
| 195 | my $content = $response->content; | ||
| 196 | |||
| 197 | return 1 if $response->code =~ /^2\d\d$/; | ||
| 198 | |||
| 199 | # anything else is a failure, and we save the parsed result | ||
| 200 | $self->_remember_errors($response->content); | ||
| 201 | return 0; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub _send_request { | ||
| 205 | my $self = shift; | ||
| 206 | my $request; | ||
| 207 | $request = $self->_make_request(@_); | ||
| 208 | my $response = $self->_do_http($request); | ||
| 209 | my $content = $response->content; | ||
| 210 | return $content unless $response->content_type eq 'application/xml'; | ||
| 211 | return unless $content; | ||
| 212 | return $self->_xpc_of_content($content); | ||
| 213 | } | ||
| 214 | # returns 1 if errors were found | ||
| 215 | |||
| 216 | sub _remember_errors { | ||
| 217 | my ($self, $src) = @_; | ||
| 218 | |||
| 219 | unless (ref $src || $src =~ m/^[[:space:]]*</) { # if not xml | ||
| 220 | (my $code = $src) =~ s/^[[:space:]]*\([0-9]*\).*$/$1/; | ||
| 221 | $self->err($code); | ||
| 222 | $self->errstr($src); | ||
| 223 | return 1; | ||
| 224 | } | ||
| 225 | |||
| 226 | my $r = ref $src ? $src : $self->_xpc_of_content($src); | ||
| 227 | |||
| 228 | if ($r->{Error}) { | ||
| 229 | $self->err($r->{Error}{Code}); | ||
| 230 | $self->errstr($r->{Error}{Message}); | ||
| 231 | return 1; | ||
| 232 | } | ||
| 233 | return 0; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub _make_request { | ||
| 237 | my ($self, $method, $path, $headers) = @_; | ||
| 238 | croak 'must specify method' unless $method; | ||
| 239 | croak 'must specify path' unless defined $path; | ||
| 240 | $headers ||= {}; | ||
| 241 | |||
| 242 | my $http_headers = $self->_merge_meta($headers); | ||
| 243 | $self->_add_auth_header($http_headers, $method, $path) | ||
| 244 | unless exists $headers->{Authorization}; | ||
| 245 | my $protocol = $self->secure ? 'https' : 'http'; | ||
| 246 | my $host = $self->host; | ||
| 247 | my $url = "$protocol://$host/$path"; | ||
| 248 | |||
| 249 | my $request = HTTP::Request->new($method, $url, $http_headers); | ||
| 250 | |||
| 251 | return $request; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub _xpc_of_content { | ||
| 255 | return XMLin($_[1], 'SuppressEmpty' => '', 'ForceArray' => ['Contents']); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub _add_auth_header { | ||
| 259 | my ($self, $headers, $method, $path) = @_; | ||
| 260 | my $ali_access_key_id = $self->ali_access_key_id; | ||
| 261 | my $ali_secret_access_key = $self->ali_secret_access_key; | ||
| 262 | |||
| 263 | if (not $headers->header('Date')) { | ||
| 264 | $headers->header(Date => time2str(time)); | ||
| 265 | } | ||
| 266 | my $canonical_string = $self->_canonical_string($method, $path, $headers); | ||
| 267 | my $encoded_canonical = | ||
| 268 | $self->_encode($ali_secret_access_key, $canonical_string); | ||
| 269 | $headers->header( | ||
| 270 | Authorization => "OSS $ali_access_key_id:$encoded_canonical"); | ||
| 271 | |||
| 272 | } | ||
| 273 | |||
| 274 | sub _encode { | ||
| 275 | my ($self, $ali_secret_access_key, $str, $urlencode) = @_; | ||
| 276 | my $hmac = Digest::HMAC_SHA1->new($ali_secret_access_key); | ||
| 277 | $hmac->add($str); | ||
| 278 | |||
| 279 | my $b64 = encode_base64($hmac->digest); | ||
| 280 | return $b64; | ||
| 281 | |||
| 282 | } | ||
| 283 | |||
| 284 | sub _do_http { | ||
| 285 | my ($self, $request, $filename) = @_; | ||
| 286 | |||
| 287 | # convenient time to reset any error conditions | ||
| 288 | $self->err(undef); | ||
| 289 | $self->errstr(undef); | ||
| 290 | return $self->ua->request($request, $filename); | ||
| 291 | } | ||
| 292 | |||
| 293 | sub _canonical_string { | ||
| 294 | my ($self, $method, $path, $headers) = @_; | ||
| 295 | my %interesting_headers = (); | ||
| 296 | while (my ($key, $value) = each %$headers) { | ||
| 297 | my $lk = lc $key; | ||
| 298 | if ( $lk eq 'content-md5' | ||
| 299 | or $lk eq 'content-type' | ||
| 300 | or $lk eq 'date') { | ||
| 301 | $interesting_headers{$key} = $self->_trim($value); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | $interesting_headers{'content-type'} ||= ''; | ||
| 306 | $interesting_headers{'content-md5'} ||= ''; | ||
| 307 | |||
| 308 | my $buf = "$method\n"; | ||
| 309 | foreach my $key (sort keys %interesting_headers) { | ||
| 310 | if ($key =~/^$ALI_HEADER_PREFIX/) { | ||
| 311 | my $k = lc($key); | ||
| 312 | $buf .= "$key:$interesting_headers{$key}\n"; | ||
| 313 | } else { | ||
| 314 | |||
| 315 | $buf .="$interesting_headers{$key}\n"; | ||
| 316 | } | ||
| 317 | } | ||
| 318 | $path =~ /^([^?]*)/; | ||
| 319 | $buf .= "/$1"; | ||
| 320 | |||
| 321 | return $buf; | ||
| 322 | |||
| 323 | } | ||
| 324 | |||
| 325 | sub _validate_acl_short { | ||
| 326 | my ($self, $policy_name) = @_; | ||
| 327 | |||
| 328 | if (!grep({$policy_name eq $_} | ||
| 329 | qw(private public-read public-read-write ))) | ||
| 330 | { | ||
| 331 | croak "$policy_name is not a supported canned access policy"; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | sub _trim { | ||
| 336 | my ($self, $value) = @_; | ||
| 337 | $value =~ s/^\s+//; | ||
| 338 | $value =~ s/\s+$//; | ||
| 339 | return $value; | ||
| 340 | } | ||
| 341 | |||
| 342 | sub _merge_meta { | ||
| 343 | my ($self, $headers) = @_; | ||
| 344 | $headers ||= {}; | ||
| 345 | |||
| 346 | my $http_header = HTTP::Headers->new; | ||
| 347 | |||
| 348 | while ( my ($k, $v) = each %$headers ) { | ||
| 349 | $http_header->header($k => $v); | ||
| 350 | } | ||
| 351 | |||
| 352 | return $http_header; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub _urlencode { | ||
| 356 | my ($self, $unencoded) = @_; | ||
| 357 | return uri_escape_utf8($unencoded, '^A-Za-z0-9_-'); | ||
| 358 | } | ||
| 359 | 1; | ||
| 360 | |||
| 361 | __END__ | ||