Net/Amazon/S3.pm

filter: Statementstmt 91.7% 243 / 265 Branchbran 75.0% 27 / 36 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 98.3% 60 / 61 Podpod 60.0% 6 / 10 totaltotal 88.7% 339 / 382 SCAR 31.7
CC
23
Cov
89%
list_bucket_all
16.4
_build_ua
6.0
_build_arg_vendor
5.0
CRAP 23.8
« Net/Amazon/S3/Signature/V4Implementation.pm ↑ Summary Net/Amazon/S3/Response.pm »
1 package Net::Amazon::S3;
2 # ABSTRACT: Use the Amazon S3 - Simple Storage Service
3 99 $Net::Amazon::S3::VERSION = '0.991';
4 99 ▶use Moose 0.85;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 99 ▶use MooseX::StrictConstructor 0.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 99 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 99 ▶use Digest::HMAC_SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 99 ▶use Safe::Isa ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 99 ▶use Net::Amazon::S3::Bucket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 99 ▶use Net::Amazon::S3::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 99 ▶use Net::Amazon::S3::Client::Bucket;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 99 ▶use Net::Amazon::S3::Client::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 99 ▶use Net::Amazon::S3::Error::Handler::Legacy;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 99 ▶use Net::Amazon::S3::HTTPRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 99 ▶use Net::Amazon::S3::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 99 ▶use Net::Amazon::S3::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 99 ▶use Net::Amazon::S3::Operation::Bucket::Acl::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 99 ▶use Net::Amazon::S3::Operation::Bucket::Acl::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 99 ▶use Net::Amazon::S3::Operation::Bucket::Create;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 99 ▶use Net::Amazon::S3::Operation::Bucket::Delete;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 99 ▶use Net::Amazon::S3::Operation::Bucket::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 99 ▶use Net::Amazon::S3::Operation::Bucket::Tags::Add;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 99 ▶use Net::Amazon::S3::Operation::Bucket::Tags::Delete;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 99 ▶use Net::Amazon::S3::Operation::Buckets::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 99 ▶use Net::Amazon::S3::Operation::Object::Acl::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 99 ▶use Net::Amazon::S3::Operation::Object::Acl::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 99 ▶use Net::Amazon::S3::Operation::Object::Add;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 99 ▶use Net::Amazon::S3::Operation::Object::Delete;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 99 ▶use Net::Amazon::S3::Operation::Object::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 99 ▶use Net::Amazon::S3::Operation::Object::Head;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 99 ▶use Net::Amazon::S3::Operation::Object::Restore;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 99 ▶use Net::Amazon::S3::Operation::Object::Tags::Add;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 99 ▶use Net::Amazon::S3::Operation::Object::Tags::Delete;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 99 ▶use Net::Amazon::S3::Operation::Object::Upload::Abort;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 99 ▶use Net::Amazon::S3::Operation::Object::Upload::Complete;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 99 ▶use Net::Amazon::S3::Operation::Object::Upload::Create;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 99 ▶use Net::Amazon::S3::Operation::Object::Upload::Part;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 99 ▶use Net::Amazon::S3::Operation::Object::Upload::Parts;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 99 ▶use Net::Amazon::S3::Operation::Objects::Delete;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 99 ▶use Net::Amazon::S3::Operation::Objects::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 99 ▶use Net::Amazon::S3::Signature::V2;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 99 ▶use Net::Amazon::S3::Signature::V4;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 99 ▶use Net::Amazon::S3::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 99 ▶use Net::Amazon::S3::Vendor;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 99 ▶use Net::Amazon::S3::Vendor::Amazon;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 99 ▶use LWP::UserAgent::Determined;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 99 ▶use URI::Escape qw(uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 99 my $AMAZON_S3_HOST = 's3.amazonaws.com';
52  
53 99 has authorization_context => (
54 is => 'ro',
55 isa => 'Net::Amazon::S3::Authorization',
56 required => 0,
57  
58 handles => {
59 aws_access_key_id => 'aws_access_key_id',
60 aws_secret_access_key => 'aws_secret_access_key',
61 aws_session_token => 'aws_session_token',
62 },
63 );
64  
65 99 has vendor => (
66 is => 'ro',
67 isa => 'Net::Amazon::S3::Vendor',
68 required => 1,
69  
70 handles => {
71 authorization_method => 'authorization_method',
72 host => 'host',
73 secure => 'use_https',
74 use_virtual_host => 'use_virtual_host',
75 },
76 );
77  
78 99 has 'timeout' => ( is => 'ro', isa => 'Num', required => 0, default => 30 );
79 99 has 'retry' => ( is => 'ro', isa => 'Bool', required => 0, default => 0 );
80 99 has 'ua'
81 => is => 'rw'
82 => isa => 'LWP::UserAgent'
83 => required => 0
84 => lazy => 1
85 => builder => '_build_ua'
86 ;
87 99 has 'err' => ( is => 'rw', isa => 'Maybe[Str]', required => 0 );
88 99 has 'errstr' => ( is => 'rw', isa => 'Maybe[Str]', required => 0 );
89 99 has keep_alive_cache_size => ( is => 'ro', isa => 'Int', required => 0, default => 10 );
90  
91 99 has error_handler_class => (
92 is => 'ro',
93 lazy => 1,
94 default => 'Net::Amazon::S3::Error::Handler::Legacy',
95 );
96  
97 has error_handler => (
98 is => 'ro',
99 lazy => 1,
100 107 ▶ default => sub { $_[0]->error_handler_class->new (s3 => $_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
101 99 );
102  
103 99 has bucket_class => (
104 is => 'ro',
105 init_arg => undef,
106 lazy => 1,
107 default => 'Net::Amazon::S3::Bucket',
108 );
109  
110 sub _build_arg_authorization_context {
111 415 ▶ my ($args) = @_;
Subroutine _build_arg_authorization_context: called CC=4 SCAR=14.0
112  
113 415 my $aws_access_key_id = delete $args->{aws_access_key_id};
114 415 my $aws_secret_access_key = delete $args->{aws_secret_access_key};
115 415 my $use_iam_role = delete $args->{use_iam_role};
116 415 my $aws_session_token = delete $args->{aws_session_token};
117  
118 415 ▶ if ($args->{authorization_context}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"authorization_context"}) 0 415 415
119 0 return $args->{authorization_context};
120 }
121  
122 415 ▶ if ($use_iam_role || $aws_session_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_iam_role or $aws_session_token) 1 414 415
Condition66%2/3
$use_iam_role or $aws_session_token
l!l&&r!l&&!r
01414
MC/DC50%1/2
$use_iam_role or $aws_session_token
$use_iam_role $aws_session_token
Truth table66%2/3
A or B
A= $use_iam_roleB= $aws_session_token
ABresult
1X1
011
000
123 1 require Net::Amazon::S3::Authorization::IAM;
124  
125 1 return Net::Amazon::S3::Authorization::IAM->new (
126 aws_access_key_id => $aws_access_key_id,
127 aws_secret_access_key => $aws_secret_access_key,
128 aws_session_token => $aws_session_token,
129 )
130 }
131  
132 414 require Net::Amazon::S3::Authorization::Basic;
133  
134 414 return Net::Amazon::S3::Authorization::Basic->new (
135 aws_access_key_id => $aws_access_key_id,
136 aws_secret_access_key => $aws_secret_access_key,
137 );
138 }
139  
140 sub _build_arg_vendor {
141 415 ▶ my ($args) = @_;
Subroutine _build_arg_vendor: called CC=5 SCAR=16.1
142  
143 my %backward =
144 1288 map { $_ => delete $args->{$_} }
145 415 grep { exists $args->{$_} }
146 qw[ host secure use_virtual_host authorization_method ]
147 ;
148  
149 return $args->{vendor}
150 415 ▶ if $args->{vendor};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"vendor"} 2 413 415
151  
152 $backward{host} = $AMAZON_S3_HOST
153 413 ▶ unless exists $backward{host};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $backward{"host"} 340 73 413
154  
155 $backward{use_https} = delete $backward{secure}
156 413 ▶ if exists $backward{secure};
Branch: 1 branch
BranchTrueFalseTotal
if exists $backward{"secure"} 405 8 413
157  
158 413 ▶ my $vendor_class = $backward{host} eq $AMAZON_S3_HOST
Branch: 1 branch
BranchTrueFalseTotal
$backward{'host'} eq $AMAZON_S3_HOST ? : 410 3 413
159 ? 'Net::Amazon::S3::Vendor::Amazon'
160 : 'Net::Amazon::S3::Vendor'
161 ;
162  
163 413 return $vendor_class->new (%backward);
164 }
165  
166 around BUILDARGS => sub {
167 415 ▶ my ($orig, $class) = (shift, shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
168 415 my $args = $class->$orig (@_);
169  
170 # support compat authorization arguments
171 415 $args->{authorization_context} = _build_arg_authorization_context $args;
172 415 $args->{vendor} = _build_arg_vendor $args;
173  
174 415 $args;
175 99 };
176  
177  
178 sub _build_ua {
179 0 ▶ my $self = shift;
Subroutine _build_ua: not called CC=2 SCAR=17.9
180  
181 0 my $ua;
182 0 ▶ if ( $self->retry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->retry) { } 0 0 0
183 0 $ua = LWP::UserAgent::Determined->new(
184 keep_alive => $self->keep_alive_cache_size,
185 requests_redirectable => [qw(GET HEAD DELETE PUT POST)],
186 );
187 0 $ua->timing('1,2,4,8,16,32');
188 } else {
189 0 $ua = LWP::UserAgent->new(
190 keep_alive => $self->keep_alive_cache_size,
191 requests_redirectable => [qw(GET HEAD DELETE PUT POST)],
192 );
193 }
194  
195 0 $ua->timeout( $self->timeout );
196 0 $ua->env_proxy;
197  
198 0 return $ua;
199 }
200  
201 sub buckets {
202 5 ▶ my ($self, %args) = @_;
Subroutine buckets: called CC=3 SCAR=11.0
Pod documented
203  
204 5 my $response = $self->_perform_operation (
205 'Net::Amazon::S3::Operation::Buckets::List',
206 %args,
207 );
208  
209 4 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 2 2 4
210  
211 2 my $owner_id = $response->owner_id;;
212 2 my $owner_displayname = $response->owner_displayname;
213  
214 2 my @buckets;
215 2 foreach my $bucket ($response->buckets) {
216 push @buckets, $self->bucket_class->new (
217 account => $self,
218 bucket => $bucket->{name},
219 creation_date => $bucket->{creation_date},
220 4 );
221  
222 }
223  
224 return +{
225 2 owner_id => $owner_id,
226 owner_displayname => $owner_displayname,
227 buckets => \@buckets,
228 };
229 }
230  
231 sub add_bucket {
232 27 ▶ my $self = shift;
Subroutine add_bucket: called CC=2 SCAR=6.9
Pod documented
233 27 my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);
234  
235 27 my $response = $self->_perform_operation (
236 'Net::Amazon::S3::Operation::Bucket::Create',
237  
238 %args,
239 );
240  
241 11 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 4 7 11
242  
243 7 return $self->bucket ($args{bucket});
244 }
245  
246 sub bucket {
247 149 ▶ my $self = shift;
Subroutine bucket: called CC=2 SCAR=6.9
Pod documented
248 149 my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);
249  
250 return $args{bucket}
251 149 ▶ if $args{bucket}->$Safe::Isa::_isa ($self->bucket_class);
Branch: 1 branch
BranchTrueFalseTotal
if $args{"bucket"}->$Safe::Isa::_isa($self->bucket_class) 1 148 149
252  
253 return $self->bucket_class->new ({
254 account => $self,
255 bucket => $args{bucket},
256 (region => $args{region}) x defined $args{region},
257 148 });
258 }
259  
260 sub delete_bucket {
261 9 ▶ my $self = shift;
Subroutine delete_bucket: called CC=3 SCAR=11.0
Pod documented
262 9 my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);
263  
264 croak 'must specify bucket'
265 9 ▶ unless defined $args{bucket};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"bucket"} 0 9 9
266  
267 9 my $response = $self->_perform_operation (
268 'Net::Amazon::S3::Operation::Bucket::Delete',
269 %args,
270 );
271  
272 5 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 4 1 5
273  
274 1 return 1;
275 }
276  
277 sub list_bucket {
278 16 ▶ my $self = shift;
Subroutine list_bucket: called CC=4 SCAR=13.9
Pod documented
279 16 my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);
280  
281 16 my $response = $self->_perform_operation (
282 'Net::Amazon::S3::Operation::Objects::List',
283 %args,
284 );
285  
286 8 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 3 5 8
287  
288 5 my $return = {
289 bucket => $response->bucket,
290 prefix => $response->prefix,
291 marker => $response->marker,
292 next_marker => $response->next_marker,
293 max_keys => $response->max_keys,
294 is_truncated => $response->is_truncated,
295 };
296  
297 5 my @keys;
298 5 foreach my $node ($response->contents) {
299 push @keys, {
300 key => $node->{key},
301 last_modified => $node->{last_modified},
302 etag => $node->{etag},
303 size => $node->{size},
304 storage_class => $node->{storage_class},
305 owner_id => $node->{owner}{id},
306 owner_displayname => $node->{owner}{displayname},
307 7 };
308 }
309 5 $return->{keys} = \@keys;
310  
311 5 ▶ if ( $args{delimiter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"delimiter"}) 2 3 5
312 2 $return->{common_prefixes} = [ $response->common_prefixes ];
313 }
314  
315 5 return $return;
316 }
317  
318 sub list_bucket_all {
319 4 ▶ my $self = shift;
Subroutine list_bucket_all: called CC=5 SCAR=28.0
Pod documented
320 4 my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);
321  
322 4 my $bucket = $args{bucket};
323 4 ▶ croak 'must specify bucket' unless $bucket;
Branch: 1 branch
BranchTrueFalseTotal
unless $bucket 0 4 4
324  
325 4 my $response = $self->list_bucket (%args);
326 0 ▶ return $response unless $response->{is_truncated};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"is_truncated"} 0 0 0
327 0 my $all = $response;
328  
329 0 while (1) {
330 my $next_marker = $response->{next_marker}
331 0 ▶ || $response->{keys}->[-1]->{key};
Condition0%0/3
$response->{'next_marker'} || $response->{'keys'}[-1]{'key'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->{'next_marker'} || $response->{'keys'}[-1]{'key'}
$response->{'next_marker'} $response->{'keys'}[-1]{'key'}
Truth table0%0/3
A || B
A= $response->{'next_marker'}B= $response->{'keys'}[-1]{'key'}
ABresult
1X1
011
000
332 0 $response = $self->list_bucket (
333 %args,
334 marker => $next_marker,
335 );
336 0 push @{ $all->{keys} }, @{ $response->{keys} };
337 0 ▶ last unless $response->{is_truncated};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"is_truncated"} 0 0 0
338 }
339  
340 0 delete $all->{is_truncated};
341 0 delete $all->{next_marker};
342 0 return $all;
343 }
344  
345 # compat wrapper; deprecated as of 2005-03-23
346 sub add_key {
347 2 ▶ my $self = shift;
Subroutine add_key: called CC=1 SCAR=0.0
Pod undocumented
348 2 my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket_and_object (\@_);
349  
350 2 my $bucket = $self->bucket (delete $args{bucket});
351 2 return $bucket->add_key (%args);
352 }
353  
354 # compat wrapper; deprecated as of 2005-03-23
355 sub get_key {
356 2 ▶ my $self = shift;
Subroutine get_key: called CC=1 SCAR=0.0
Pod undocumented
357 2 my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket_and_object (\@_);
358  
359 2 my $bucket = $self->bucket (delete $args{bucket});
360 2 return $bucket->get_key (%args);
361 }
362  
363 # compat wrapper; deprecated as of 2005-03-23
364 sub head_key {
365 1 ▶ my ( $self, $conf ) = @_;
Subroutine head_key: called CC=1 SCAR=0.0
Pod undocumented
366 1 my $bucket = $self->bucket (delete $conf->{bucket});
367 1 return $bucket->head_key( $conf->{key} );
368 }
369  
370 # compat wrapper; deprecated as of 2005-03-23
371 sub delete_key {
372 2 ▶ my $self = shift;
Subroutine delete_key: called CC=1 SCAR=0.0
Pod undocumented
373 2 my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket_and_object (\@_);
374  
375 2 my $bucket = $self->bucket (delete $args{bucket});
376 2 return $bucket->delete_key (%args);
377 }
378  
379 sub _perform_operation {
380 210 ▶ my ($self, $operation, %params) = @_;
Subroutine _perform_operation: called CC=2 SCAR=6.9
381  
382 210 my $error_handler = delete $params{error_handler};
383 210 ▶ $error_handler = $self->error_handler unless defined $error_handler;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error_handler 107 103 210
384  
385 210 my $request_class = $operation . '::Request';
386 210 my $response_class = $operation . '::Response';
387 210 my $filename = delete $params{filename};
388  
389 210 my $request = $request_class->new (s3 => $self, %params);
390 210 my $http_response = $self->ua->request ($request->http_request, $filename);
391 210 my $response = $response_class->new (http_response => $http_response);
392  
393 210 $error_handler->handle_error ($response);
394  
395 138 return $response;
396 }
397  
398 sub _urlencode {
399 394 ▶ my ( $self, $unencoded ) = @_;
Subroutine _urlencode: called CC=1 SCAR=0.0
400 394 return uri_escape_utf8( $unencoded, '^A-Za-z0-9_~\-\.' );
401 }
402  
403 99 __PACKAGE__->meta->make_immutable;
404  
405 99 1;
406  
407 __END__
408  
409 =pod
410  
411 =encoding UTF-8
412  
413 =head1 NAME
414  
415 Net::Amazon::S3 - Use the Amazon S3 - Simple Storage Service
416  
417 =head1 VERSION
418  
419 version 0.991
420  
421 =head1 SYNOPSIS
422  
423 use Net::Amazon::S3;
424 use Net::Amazon::S3::Authorization::Basic;
425 use Net::Amazon::S3::Authorization::IAM;
426 my $aws_access_key_id = 'fill me in';
427 my $aws_secret_access_key = 'fill me in too';
428  
429 my $s3 = Net::Amazon::S3->new (
430 authorization_context => Net::Amazon::S3::Authorization::Basic->new (
431 aws_access_key_id => $aws_access_key_id,
432 aws_secret_access_key => $aws_secret_access_key,
433 ),
434 retry => 1,
435 );
436  
437 # or use an IAM role.
438 my $s3 = Net::Amazon::S3->new (
439 authorization_context => Net::Amazon::S3::Authorization::IAM->new (
440 aws_access_key_id => $aws_access_key_id,
441 aws_secret_access_key => $aws_secret_access_key,
442 ),
443 retry => 1,
444 );
445  
446 # a bucket is a globally-unique directory
447 # list all buckets that i own
448 my $response = $s3->buckets;
449 foreach my $bucket ( @{ $response->{buckets} } ) {
450 print "You have a bucket: " . $bucket->bucket . "\n";
451 }
452  
453 # create a new bucket
454 my $bucketname = 'acmes_photo_backups';
455 my $bucket = $s3->add_bucket( { bucket => $bucketname } )
456 or die $s3->err . ": " . $s3->errstr;
457  
458 # or use an existing bucket
459 $bucket = $s3->bucket($bucketname);
460  
461 # store a file in the bucket
462 $bucket->add_key_filename( '1.JPG', 'DSC06256.JPG',
463 { content_type => 'image/jpeg', },
464 ) or die $s3->err . ": " . $s3->errstr;
465  
466 # store a value in the bucket
467 $bucket->add_key( 'reminder.txt', 'this is where my photos are backed up' )
468 or die $s3->err . ": " . $s3->errstr;
469  
470 # list files in the bucket
471 $response = $bucket->list_all
472 or die $s3->err . ": " . $s3->errstr;
473 foreach my $key ( @{ $response->{keys} } ) {
474 my $key_name = $key->{key};
475 my $key_size = $key->{size};
476 print "Bucket contains key '$key_name' of size $key_size\n";
477 }
478  
479 # fetch file from the bucket
480 $response = $bucket->get_key_filename( '1.JPG', 'GET', 'backup.jpg' )
481 or die $s3->err . ": " . $s3->errstr;
482  
483 # fetch value from the bucket
484 $response = $bucket->get_key('reminder.txt')
485 or die $s3->err . ": " . $s3->errstr;
486 print "reminder.txt:\n";
487 print " content length: " . $response->{content_length} . "\n";
488 print " content type: " . $response->{content_type} . "\n";
489 print " etag: " . $response->{content_type} . "\n";
490 print " content: " . $response->{value} . "\n";
491  
492 # delete keys
493 $bucket->delete_key('reminder.txt') or die $s3->err . ": " . $s3->errstr;
494 $bucket->delete_key('1.JPG') or die $s3->err . ": " . $s3->errstr;
495  
496 # and finally delete the bucket
497 $bucket->delete_bucket or die $s3->err . ": " . $s3->errstr;
498  
499 =head1 DESCRIPTION
500  
501 This module provides a Perlish interface to Amazon S3. From the
502 developer blurb: "Amazon S3 is storage for the Internet. It is
503 designed to make web-scale computing easier for developers. Amazon S3
504 provides a simple web services interface that can be used to store and
505 retrieve any amount of data, at any time, from anywhere on the web. It
506 gives any developer access to the same highly scalable, reliable,
507 fast, inexpensive data storage infrastructure that Amazon uses to run
508 its own global network of web sites. The service aims to maximize
509 benefits of scale and to pass those benefits on to developers".
510  
511 To find out more about S3, please visit: http://s3.amazonaws.com/
512  
513 To use this module you will need to sign up to Amazon Web Services and
514 provide an "Access Key ID" and " Secret Access Key". If you use this
515 module, you will incurr costs as specified by Amazon. Please check the
516 costs. If you use this module with your Access Key ID and Secret
517 Access Key you must be responsible for these costs.
518  
519 I highly recommend reading all about S3, but in a nutshell data is
520 stored in values. Values are referenced by keys, and keys are stored
521 in buckets. Bucket names are global.
522  
523 Note: This is the legacy interface, please check out
524 L<Net::Amazon::S3::Client> instead.
525  
526 Development of this code happens here: https://github.com/rustyconover/net-amazon-s3
527  
528 =head2 Bucket names with dots, HTTPS, and Signature V4
529  
530 At the moment Amazon S3 doesn't play well with HTTPS and virtual bucket hosts
531 if bucket name contains dots.
532  
533 Due the current implementation of Signature V4 handling you should use workaround
534 consisting of usage of region hostnames
535  
536 my $bucket_region = $global_s3->bucket ($bucket)->_head_region;
537  
538 my $region_s3 = Net::Amazon:S3->new (
539 ...,
540 vendor => Net::Amazon::S3::Vendor::Amazon->new (
541 host => "s3-$bucket_region.amazonaws.com",
542 use_virtual_host => 0,
543 ),
544 );
545  
546 my $bucket = $region_s3->bucket ($bucket);
547  
548 And use bucket instance / region s3 connection.
549  
550 =head1 METHODS
551  
552 =head2 new
553  
554 Create a new S3 client object. Takes some arguments:
555  
556 =over
557  
558 =item authorization_context
559  
560 Class that provides authorization information.
561  
562 See one of available implementations for more
563  
564 =over
565  
566 =item L<Net::Amazon::S3::Authorization::Basic>
567  
568 =item L<Net::Amazon::S3::Authorization::IAM>
569  
570 =back
571  
572 =item vendor
573  
574 Instance of L<Net::Amazon::S3::Vendor> holding vendor specific deviations.
575  
576 S3 became widely used object storage protocol with many vendors providing
577 different feature sets and different compatibility level.
578  
579 One common difference is bucket's HEAD request to determine its region.
580  
581 To maintain currently known differences along with any differencies that
582 may rise in feature it's better to hold vendor specification in dedicated
583 classes. This also allows users to build their own fine-tuned vendor classes.
584  
585 =over
586  
587 =item L<Net::Amazon::S3::Vendor::Amazon>
588  
589 =item L<Net::Amazon::S3::Vendor::Generic>
590  
591 =back
592  
593 =item aws_access_key_id
594  
595 Deprecated.
596  
597 When used it's used to create authorization context.
598  
599 Use your Access Key ID as the value of the AWSAccessKeyId parameter
600 in requests you send to Amazon Web Services (when required). Your
601 Access Key ID identifies you as the party responsible for the
602 request.
603  
604 =item aws_secret_access_key
605  
606 Deprecated.
607  
608 When used it's used to create authorization context.
609  
610 Since your Access Key ID is not encrypted in requests to AWS, it
611 could be discovered and used by anyone. Services that are not free
612 require you to provide additional information, a request signature,
613 to verify that a request containing your unique Access Key ID could
614 only have come from you.
615  
616 DO NOT INCLUDE THIS IN SCRIPTS OR APPLICATIONS YOU DISTRIBUTE. YOU'LL BE SORRY
617  
618 =item aws_session_token
619  
620 Deprecated.
621  
622 When used it's used to create authorization context.
623  
624 If you are using temporary credentials provided by the AWS Security Token
625 Service, set the token here, and it will be added to the request in order to
626 authenticate it.
627  
628 =item use_iam_role
629  
630 Deprecated.
631  
632 When used it's used to create authorization context.
633  
634 If you'd like to use IAM provided temporary credentials, pass this option
635 with a true value.
636  
637 =item secure
638  
639 Deprecated.
640  
641 Set this to C<0> if you don't want to use SSL-encrypted connections when talking
642 to S3. Defaults to C<1>.
643  
644 To use SSL-encrypted connections, LWP::Protocol::https is required.
645  
646 See L<#vendor> and L<Net::Amazon::S3::Vendor>.
647  
648 =item keep_alive_cache_size
649  
650 Set this to C<0> to disable Keep-Alives. Default is C<10>.
651  
652 =item timeout
653  
654 How many seconds should your script wait before bailing on a request to S3? Defaults
655 to 30.
656  
657 =item retry
658  
659 If this library should retry upon errors. This option is recommended.
660 This uses exponential backoff with retries after 1, 2, 4, 8, 16, 32 seconds,
661 as recommended by Amazon. Defaults to off.
662  
663 When retry is on, request will be automatically retried when one of following
664 HTTP statuses happens
665  
666 =over
667  
668 =item 408 - Request Timeout
669  
670 =item 500 - Internal Server Error
671  
672 =item 502 - Bad Gateway
673  
674 =item 503 - Service Unavailable
675  
676 =item 504 - Gateway Timeout
677  
678 =back
679  
680 For more details see L<LWP::UserAgent::Determined>.
681  
682 =item host
683  
684 Deprecated.
685  
686 The S3 host endpoint to use. Defaults to 's3.amazonaws.com'. This allows
687 you to connect to any S3-compatible host.
688  
689 See L<#vendor> and L<Net::Amazon::S3::Vendor>.
690  
691 =item use_virtual_host
692  
693 Deprecated.
694  
695 Use the virtual host method ('bucketname.s3.amazonaws.com') instead of specifying the
696 bucket at the first part of the path. This is particularly useful if you want to access
697 buckets not located in the US-Standard region (such as EU, Asia Pacific or South America).
698 See L<http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html> for the pros and cons.
699  
700 See L<#vendor> and L<Net::Amazon::S3::Vendor>.
701  
702 =item authorization_method
703  
704 Deprecated.
705  
706 Authorization implementation package name.
707  
708 This library provides L<< Net::Amazon::S3::Signature::V2 >> and L<< Net::Amazon::S3::Signature::V4 >>
709  
710 Default is Signature 4 if host is C<< s3.amazonaws.com >>, Signature 2 otherwise
711  
712 See L<#vendor> and L<Net::Amazon::S3::Vendor>.
713  
714 =item error_handler_class
715  
716 Error handler class name (package name), see L<< Net::Amazon::S3::Error::Handler >>
717 for more.
718  
719 Default: L<< Net::Amazon::S3::Error::Handler::Legacy >>
720  
721 =item error_handler
722  
723 Instance of error handler class.
724  
725 =back
726  
727 =head3 Notes
728  
729 When using L<Net::Amazon::S3> in child processes using fork (such as in
730 combination with the excellent L<Parallel::ForkManager>) you should create the
731 S3 object in each child, use a fresh LWP::UserAgent in each child, or disable
732 the L<LWP::ConnCache> in the parent:
733  
734 $s3->ua( LWP::UserAgent->new(
735 keep_alive => 0, requests_redirectable => [qw'GET HEAD DELETE PUT POST'] );
736  
737 =head2 buckets
738  
739 Returns undef on error, else hashref of results
740  
741 =head2 add_bucket
742  
743 # Create new bucket with default location
744 my $bucket = $s3->add_bucket ('new-bucket');
745  
746 # Create new bucket in another location
747 my $bucket = $s3->add_bucket ('new-bucket', location_constraint => 'eu-west-1');
748 my $bucket = $s3->add_bucket ('new-bucket', { location_constraint => 'eu-west-1' });
749 my $bucket = $s3->add_bucket (bucket => 'new-bucket', location_constraint => 'eu-west-1');
750 my $bucket = $s3->add_bucket ({ bucket => 'new-bucket', location_constraint => 'eu-west-1' });
751  
752 Method creates and returns new bucket.
753  
754 In case of error it reports it and returns C<undef> (refer L</"ERROR HANDLING">).
755  
756 Recognized positional arguments (refer L</"CALLING CONVENTION">)
757  
758 =over
759  
760 =item bucket
761  
762 Required, recognized as positional.
763  
764 The name of the bucket you want to add.
765  
766 =back
767  
768 Recognized optional arguments
769  
770 =over
771  
772 =item acl
773  
774 acl => 'private'
775 acl => Net::Amazon::S3::ACL::Canned->PRIVATE
776 acl => Net::Amazon::S3::ACL::Set->grant_read (email => 'foo@bar.baz')
777  
778 I<Available since v0.94>
779  
780 Set ACL to the newly created bucket. Refer L<Net::Amazon::S3::ACL> for possibilities.
781  
782 =item acl_short (deprecated)
783  
784 I<Deprecated since v0.94>
785  
786 When specified its value is used to populate C<acl> argument (unless it exists).
787  
788 =item location_constraint
789  
790 Optional.
791  
792 Sets the location constraint of the new bucket. If left unspecified, the
793 default S3 datacenter location will be used.
794  
795 This library recognizes regions according Amazon S3 documentation
796  
797 =over
798  
799 =item →
800  
801 L<https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region>
802  
803 =item →
804  
805 L<https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html#API_CreateBucket_RequestSyntax>
806  
807 =back
808  
809 =back
810  
811 Provides operation L<CreateBucket|https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html>.
812  
813 =head2 bucket BUCKET
814  
815 # build bucket with guessed region
816 $s3->bucket ('foo');
817 $s3->bucket (bucket => 'foo');
818 $s3->bucket (name => 'foo');
819  
820 # build with explicit region
821 $s3->bucket ('foo', region => 'bar');
822  
823 Returns an (unverified) bucket object from an account. Does no network access.
824  
825 However, when guessing region, C<HeadRegion> operation may be called before
826 first network access.
827  
828 Region is mandatory when using Signature V4 authorization, which is default
829 for AWS. AWS limits number of HTTP requests, see L<https://aws.amazon.com/premiumsupport/knowledge-center/s3-request-limit-avoid-throttling/>
830  
831 =head2 delete_bucket
832  
833 $s3->delete_bucket ($bucket);
834 $s3->delete_bucket (bucket => $bucket);
835  
836 Deletes bucket from account.
837  
838 Returns C<true> if the bucket is successfully deleted.
839  
840 Returns C<false> and reports an error otherwise (refer L</"ERROR HANDLING">)
841  
842 Positional arguments (refer L</"CALLING CONVENTION">)
843  
844 =over
845  
846 =item bucket
847  
848 Required.
849  
850 The name of the bucket or L<Net::Amazon::S3::Bucket> instance you want to delete.
851  
852 =back
853  
854 Provides operation L<"DeleteBucket"|https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucket.html>
855  
856 =head2 list_bucket
857  
858 List all keys in this bucket.
859  
860 Takes a hashref of arguments:
861  
862 MANDATORY
863  
864 =over
865  
866 =item bucket
867  
868 The name of the bucket you want to list keys on
869  
870 =back
871  
872 OPTIONAL
873  
874 =over
875  
876 =item prefix
877  
878 Restricts the response to only contain results that begin with the
879 specified prefix. If you omit this optional argument, the value of
880 prefix for your query will be the empty string. In other words, the
881 results will be not be restricted by prefix.
882  
883 =item delimiter
884  
885 If this optional, Unicode string parameter is included with your
886 request, then keys that contain the same string between the prefix
887 and the first occurrence of the delimiter will be rolled up into a
888 single result element in the CommonPrefixes collection. These
889 rolled-up keys are not returned elsewhere in the response. For
890 example, with prefix="USA/" and delimiter="/", the matching keys
891 "USA/Oregon/Salem" and "USA/Oregon/Portland" would be summarized
892 in the response as a single "USA/Oregon" element in the CommonPrefixes
893 collection. If an otherwise matching key does not contain the
894 delimiter after the prefix, it appears in the Contents collection.
895  
896 Each element in the CommonPrefixes collection counts as one against
897 the MaxKeys limit. The rolled-up keys represented by each CommonPrefixes
898 element do not. If the Delimiter parameter is not present in your
899 request, keys in the result set will not be rolled-up and neither
900 the CommonPrefixes collection nor the NextMarker element will be
901 present in the response.
902  
903 =item max-keys
904  
905 This optional argument limits the number of results returned in
906 response to your query. Amazon S3 will return no more than this
907 number of results, but possibly less. Even if max-keys is not
908 specified, Amazon S3 will limit the number of results in the response.
909 Check the IsTruncated flag to see if your results are incomplete.
910 If so, use the Marker parameter to request the next page of results.
911 For the purpose of counting max-keys, a 'result' is either a key
912 in the 'Contents' collection, or a delimited prefix in the
913 'CommonPrefixes' collection. So for delimiter requests, max-keys
914 limits the total number of list results, not just the number of
915 keys.
916  
917 =item marker
918  
919 This optional parameter enables pagination of large result sets.
920 C<marker> specifies where in the result set to resume listing. It
921 restricts the response to only contain results that occur alphabetically
922 after the value of marker. To retrieve the next page of results,
923 use the last key from the current page of results as the marker in
924 your next request.
925  
926 See also C<next_marker>, below.
927  
928 If C<marker> is omitted,the first page of results is returned.
929  
930 =back
931  
932 Returns undef on error and a hashref of data on success:
933  
934 The hashref looks like this:
935  
936 {
937 bucket => $bucket_name,
938 prefix => $bucket_prefix,
939 common_prefixes => [$prefix1,$prefix2,...]
940 marker => $bucket_marker,
941 next_marker => $bucket_next_available_marker,
942 max_keys => $bucket_max_keys,
943 is_truncated => $bucket_is_truncated_boolean
944 keys => [$key1,$key2,...]
945 }
946  
947 Explanation of bits of that:
948  
949 =over
950  
951 =item common_prefixes
952  
953 If list_bucket was requested with a delimiter, common_prefixes will
954 contain a list of prefixes matching that delimiter. Drill down into
955 these prefixes by making another request with the prefix parameter.
956  
957 =item is_truncated
958  
959 B flag that indicates whether or not all results of your query were
960 returned in this response. If your results were truncated, you can
961 make a follow-up paginated request using the Marker parameter to
962 retrieve the rest of the results.
963  
964 =item next_marker
965  
966 A convenience element, useful when paginating with delimiters. The
967 value of C<next_marker>, if present, is the largest (alphabetically)
968 of all key names and all CommonPrefixes prefixes in the response.
969 If the C<is_truncated> flag is set, request the next page of results
970 by setting C<marker> to the value of C<next_marker>. This element
971 is only present in the response if the C<delimiter> parameter was
972 sent with the request.
973  
974 =back
975  
976 Each key is a hashref that looks like this:
977  
978 {
979 key => $key,
980 last_modified => $last_mod_date,
981 etag => $etag, # An MD5 sum of the stored content.
982 size => $size, # Bytes
983 storage_class => $storage_class # Doc?
984 owner_id => $owner_id,
985 owner_displayname => $owner_name
986 }
987  
988 =head2 list_bucket_all
989  
990 List all keys in this bucket without having to worry about
991 'marker'. This is a convenience method, but may make multiple requests
992 to S3 under the hood.
993  
994 Takes the same arguments as list_bucket.
995  
996 =head2 _perform_operation
997  
998 my $response = $s3->_perform_operation ('Operation' => (
999 # ... operation request parameters
1000 ));
1001  
1002 Internal operation implementation method, takes request construction parameters,
1003 performs necessary HTTP requests(s) and returns Response instance.
1004  
1005 Method takes same named parameters as realted Request class.
1006  
1007 Method provides available contextual parameters by default (eg s3, bucket)
1008  
1009 Method invokes contextual error handler.
1010  
1011 =head1 CALLING CONVENTION
1012  
1013 I<Available since v0.97> - calling convention extentend
1014  
1015 In order to make method calls somehow consistent, backward compatible,
1016 and extendable, API's methods support multiple ways how to provide their arguments
1017  
1018 =over
1019  
1020 =item plain named arguments (preferred)
1021  
1022 method (named => 'argument', another => 'argument');
1023  
1024 =item trailing configuration hash
1025  
1026 method ({ named => 'argument', another => 'argument' });
1027 method (positional, { named => 'argument', another => 'argument' } );
1028  
1029 Last argument of every method can be configuration hash, treated as additional
1030 named arguments. Can be combined with named arguments.
1031  
1032 =item positional arguments with optional named arguments
1033  
1034 method (positional, named => 'argument', another => 'argument');
1035 method (positional, { named => 'argument', another => 'argument' } );
1036  
1037 For methods supporting mandatory positional arguments additional named
1038 arguments and/or configuration hash is supported.
1039  
1040 Named arguments or configuration hash can specify value of positional
1041 arguments as well removing it from list of required positional arguments
1042 for given call (see example)
1043  
1044 $s3->bucket->add_key ('key', 'value', acl => $acl);
1045 $s3->bucket->add_key ('value', key => 'key', acl => $acl);
1046 $s3->bucket->add_key (key => 'key', value => 'value', acl => $acl);
1047  
1048 =back
1049  
1050 =head1 ERROR HANDLING
1051  
1052 L<Net::Amazon::S3> supports pluggable error handling via
1053 L<Net::Amazon::S3::Error::Handler>.
1054  
1055 When response ends up with an error, every method reports it, and in case it
1056 receives control back (no exception), it returns C<undef>.
1057  
1058 Default error handling for L<Net::Amazon::S3> is L<Net::Amazon::S3::Error::Handler::Legacy>
1059 which (mostly) sets C<err> and C<errstr>.
1060  
1061 =head1 LICENSE
1062  
1063 This module contains code modified from Amazon that contains the
1064 following notice:
1065  
1066 # This software code is made available "AS IS" without warranties of any
1067 # kind. You may copy, display, modify and redistribute the software
1068 # code either by itself or as incorporated into your code; provided that
1069 # you do not remove any proprietary notices. Your use of this software
1070 # code is at your own risk and you waive any claim against Amazon
1071 # Digital Services, Inc. or its affiliates with respect to your use of
1072 # this software code. (c) 2006 Amazon Digital Services, Inc. or its
1073 # affiliates.
1074  
1075 =head1 TESTING
1076  
1077 Testing S3 is a tricky thing. Amazon wants to charge you a bit of
1078 money each time you use their service. And yes, testing counts as using.
1079 Because of this, the application's test suite skips anything approaching
1080 a real test unless you set these three environment variables:
1081  
1082 =over
1083  
1084 =item AMAZON_S3_EXPENSIVE_TESTS
1085  
1086 Doesn't matter what you set it to. Just has to be set
1087  
1088 =item AWS_ACCESS_KEY_ID
1089  
1090 Your AWS access key
1091  
1092 =item AWS_ACCESS_KEY_SECRET
1093  
1094 Your AWS sekkr1t passkey. Be forewarned that setting this environment variable
1095 on a shared system might leak that information to another user. Be careful.
1096  
1097 =back
1098  
1099 =head1 AUTHOR
1100  
1101 Leon Brocard <acme@astray.com> and unknown Amazon Digital Services programmers.
1102  
1103 Brad Fitzpatrick <brad@danga.com> - return values, Bucket object
1104  
1105 Pedro Figueiredo <me@pedrofigueiredo.org> - since 0.54
1106  
1107 Branislav Zahradník <barney@cpan.org> - since v0.81
1108  
1109 =head1 SEE ALSO
1110  
1111 L<Net::Amazon::S3::Bucket>
1112  
1113 =head1 AUTHOR
1114  
1115 Branislav Zahradník <barney@cpan.org>
1116  
1117 =head1 COPYRIGHT AND LICENSE
1118  
1119 This software is copyright (c) 2022 by Amazon Digital Services, Leon Brocard, Brad Fitzpatrick, Pedro Figueiredo, Rusty Conover, Branislav Zahradník.
1120  
1121 This is free software; you can redistribute it and/or modify it under
1122 the same terms as the Perl 5 programming language system itself.
1123  
1124 =cut
« Net/Amazon/S3/Signature/V4Implementation.pm ↑ Summary Net/Amazon/S3/Response.pm »