blib/lib/Net/Async/Webservice/S3.pm

filter: Statementstmt 96.1% 370 / 385 Branchbran 85.3% 111 / 130 Conditioncond 63.4% 66 / 104 MC/DCmcdc 35.9% 23 / 64 Subroutinesub 98.7% 77 / 78 Podpod 100% 7 / 7 totaltotal 85.1% 654 / 768 CC 112 SCAR 16.2
CC
112
Cov
88%
_make_request
12.6
_gen_auth_header
12.0
_md5sum_wrap
11.4
CRAP 5.1
↑ Summary Build »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2013-2014 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::Webservice::S3;
7  
8 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use base qw( IO::Async::Notifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 our $VERSION = '0.18';
13  
14 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 ▶use Digest::HMAC_SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Digest::MD5 qw( md5 );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Future 0.21; # ->then_with_f
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Future::Utils 0.16 qw( repeat try_repeat fmap1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use HTTP::Date qw( time2str );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use IO::Async::Timer::Countdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use List::Util qw( sum );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 10 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use URI::Escape qw( uri_escape_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 10 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 10 my $libxml = XML::LibXML->new;
31  
32 10 ▶use constant DEFAULT_S3_HOST => "s3.amazonaws.com";
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 =head1 NAME
35  
36 C<Net::Async::Webservice::S3> - use Amazon's S3 web service with C<IO::Async>
37  
38 =head1 SYNOPSIS
39  
40 use IO::Async::Loop;
41 use Net::Async::Webservice::S3;
42  
43 my $loop = IO::Async::Loop->new;
44  
45 my $s3 = Net::Async::Webservice::S3->new(
46 access_key => ...,
47 secret_key => ...,
48 bucket => "my-bucket-here",
49 );
50 $loop->add( $s3 );
51  
52 my $put_f = $s3->put_object(
53 key => "the-key",
54 value => "A new value for the key\n";
55 );
56  
57 my $get_f = $s3->get_object(
58 key => "another-key",
59 );
60  
61 $loop->await_all( $put_f, $get_f );
62  
63 print "The value is:\n", $get_f->get;
64  
65 =head1 DESCRIPTION
66  
67 This module provides a webservice API around Amazon's S3 web service for use
68 in an L<IO::Async>-based program. Each S3 operation is represented by a method
69 that returns a L<Future>; this future, if successful, will eventually return
70 the result of the operation.
71  
72 =cut
73  
74 sub _init
75 {
76 9 ▶ my $self = shift;
Subroutine _init: called CC=7 SCAR=23.3
77 9 my ( $args ) = @_;
78  
79 9 ▶ $args->{http} ||= do {
Condition33%1/3
$args->{"http"} ||= do { require Net::Async::HTTP; "Net::Async::HTTP"->VERSION("0.33"); my $http = "Net::Async::HTTP"->new; $self->add_child($http); $http }
l!l&&r!l&&!r
900
MC/DC0%0/2
$args->{"http"} ||= do { require Net::Async::HTTP; "Net::Async::HTTP"->VERSION("0.33"); my $http = "Net::Async::HTTP"->new; $self->add_child($http); $http }
$args->{"http"} do { require Net::Async::HTTP; "Net::Async::HTTP"->VERSION("0.33"); my $http = "Net::Async::HTTP"->new; $self->add_child($http); $http }
Truth table33%1/3
A ||= B
A= $args->{"http"}B= do { require Net::Async::HTTP; "Net::Async::HTTP"->VERSION("0.33"); my $http = "Net::Async::HTTP"->new; $self->add_child($http); $http }
ABresult
1X1
011
000
80 0 require Net::Async::HTTP;
81 0 Net::Async::HTTP->VERSION( '0.33' ); # 'timeout' and 'stall_timeout' failures
82 0 my $http = Net::Async::HTTP->new;
83 0 $self->add_child( $http );
84 0 $http;
85 };
86  
87 9 ▶ $args->{max_retries} //= 3;
Condition100%2/2
$args->{"max_retries"} //= 3
l!l
81
MC/DC100%1/1
$args->{"max_retries"} //= 3
$args->{"max_retries"}
Truth table100%2/2
A
A= $args->{"max_retries"}
Aresult
11
00
88 9 ▶ $args->{list_max_keys} //= 1000;
Condition50%1/2
$args->{"list_max_keys"} //= 1000
l!l
09
MC/DC0%0/1
$args->{"list_max_keys"} //= 1000
$args->{"list_max_keys"}
Truth table50%1/2
A
A= $args->{"list_max_keys"}
Aresult
11
00
89 9 ▶ $args->{read_size} //= 64*1024; # 64 KiB
Condition50%1/2
$args->{"read_size"} //= 65536
l!l
09
MC/DC0%0/1
$args->{"read_size"} //= 65536
$args->{"read_size"}
Truth table50%1/2
A
A= $args->{"read_size"}
Aresult
11
00
90  
91 # S3 docs suggest > 100MB should use multipart. They don't actually
92 # document what size of parts to use, but we'll use that again.
93 9 ▶ $args->{part_size} //= 100*1024*1024;
Condition50%1/2
$args->{"part_size"} //= 104857600
l!l
09
MC/DC0%0/1
$args->{"part_size"} //= 104857600
$args->{"part_size"}
Truth table50%1/2
A
A= $args->{"part_size"}
Aresult
11
00
94  
95 9 ▶ $args->{host} //= DEFAULT_S3_HOST;
Condition50%1/2
$args->{"host"} //= "s3.amazonaws.com"
l!l
09
MC/DC0%0/1
$args->{"host"} //= "s3.amazonaws.com"
$args->{"host"}
Truth table50%1/2
A
A= $args->{"host"}
Aresult
11
00
96  
97 9 return $self->SUPER::_init( @_ );
98 }
99  
100 =head1 PARAMETERS
101  
102 The following named parameters may be passed to C<new> or C<configure>:
103  
104 =over 8
105  
106 =item http => Net::Async::HTTP
107  
108 Optional. Allows the caller to provide a specific asynchronous HTTP user agent
109 object to use. This will be invoked with a single method, as documented by
110 L<Net::Async::HTTP>:
111  
112 $response_f = $http->do_request( request => $request, ... )
113  
114 If absent, a new instance will be created and added as a child notifier of
115 this object. If a value is supplied, it will be used as supplied and I<not>
116 specifically added as a child notifier. In this case, the caller must ensure
117 it gets added to the underlying L<IO::Async::Loop> instance, if required.
118  
119 =item access_key => STRING
120  
121 =item secret_key => STRING
122  
123 The twenty-character Access Key ID and forty-character Secret Key to use for
124 authenticating requests to S3.
125  
126 =item ssl => BOOL
127  
128 Optional. If given a true value, will use C<https> URLs over SSL. Defaults to
129 off. This feature requires the optional L<IO::Async::SSL> module if using
130 L<Net::Async::HTTP>.
131  
132 =item bucket => STRING
133  
134 Optional. If supplied, gives the default bucket name to use, at which point it
135 is optional to supply to the remaining methods.
136  
137 =item prefix => STRING
138  
139 Optional. If supplied, this prefix string is prepended to any key names passed
140 in methods, and stripped from the response from C<list_bucket>. It can be used
141 to keep operations of the object contained within the named key space. If this
142 string is supplied, don't forget that it should end with the path delimiter in
143 use by the key naming scheme (for example C</>).
144  
145 =item host => STRING
146  
147 Optional. Sets the hostname to talk to the S3 service. Usually the default of
148 C<s3.amazonaws.com> is sufficient. This setting allows for communication with
149 other service providers who provide the same API as S3.
150  
151 =item max_retries => INT
152  
153 Optional. Maximum number of times to retry a failed operation. Defaults to 3.
154  
155 =item list_max_keys => INT
156  
157 Optional. Maximum number of keys at a time to request from S3 for the
158 C<list_bucket> method. Larger values may be more efficient as fewer roundtrips
159 will be required per method call. Defaults to 1000.
160  
161 =item part_size => INT
162  
163 Optional. Size in bytes to break content for using multipart upload. If an
164 object key's size is no larger than this value, multipart upload will not be
165 used. Defaults to 100 MiB.
166  
167 =item read_size => INT
168  
169 Optional. Size in bytes to read per call to the C<$gen_value> content
170 generation function in C<put_object>. Defaults to 64 KiB. Be aware that too
171 large a value may lead to the C<PUT> stall timer failing to be invoked on slow
172 enough connections, causing spurious timeouts.
173  
174 =item timeout => NUM
175  
176 Optional. If configured, this is passed into individual requests of the
177 underlying C<Net::Async::HTTP> object, except for the actual content C<GET> or
178 C<PUT> operations. It is therefore used by C<list_bucket>, C<delete_object>,
179 and the multi-part metadata operations used by C<put_object>. To apply an
180 overall timeout to an individual C<get_object> or C<put_object> operation,
181 pass a specific C<timeout> argument to those methods specifically.
182  
183 =item stall_timeout => NUM
184  
185 Optional. If configured, this is passed into the underlying
186 C<Net::Async::HTTP> object and used for all content uploads and downloads.
187  
188 =item put_concurrent => INT
189  
190 Optional. If configured, gives a default value for the C<concurrent> parameter
191 to C<put_object>.
192  
193 =back
194  
195 =cut
196  
197 sub configure
198 {
199 19 ▶ my $self = shift;
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
200 19 my %args = @_;
201  
202 19 foreach (qw( http access_key secret_key ssl bucket prefix host max_retries
203 list_max_keys part_size read_size timeout stall_timeout
204 put_concurrent )) {
205 266 ▶ exists $args{$_} and $self->{$_} = delete $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} and $self->{$_} = delete $args{$_} 85 181 266
206 }
207  
208 19 $self->SUPER::configure( %args );
209 }
210  
211 =head1 METHODS
212  
213 The following methods all support the following common arguments:
214  
215 =over 8
216  
217 =item timeout => NUM
218  
219 =item stall_timeout => NUM
220  
221 Optional. Passed directly to the underlying C<< Net::Async::HTTP->request >>
222 method.
223  
224 =back
225  
226 Each method below that yields a C<Future> is documented in the form
227  
228 $s3->METHOD( ARGS ) ==> YIELDS
229  
230 Where the C<YIELDS> part indicates the values that will eventually be returned
231 by the C<get> method on the returned Future object, if it succeeds.
232  
233 =cut
234  
235 sub _make_request
236 {
237 84 ▶ my $self = shift;
Subroutine _make_request: called CC=12 SCAR=25.3
238 84 my %args = @_;
239  
240 84 my $method = $args{method};
241 84 ▶ defined $args{content} or croak "Missing 'content'";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"content"} or croak("Missing 'content'") 0 84 84
242  
243 84 my @params;
244 84 foreach my $key ( sort keys %{ $args{query_params} } ) {
245 101 ▶ next unless defined( my $value = $args{query_params}->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $value = $args{"query_params"}{$key}) 12 89 101
246 89 $key =~ s/_/-/g;
247 89 push @params, $key . "=" . uri_escape_utf8( $value, "^A-Za-z0-9_-" );
248 }
249  
250 84 ▶ my $bucket = $args{bucket} // $self->{bucket};
Condition66%2/3
$args{"bucket"} // $self->{"bucket"}
l!l&&r!l&&!r
8040
MC/DC0%0/2
$args{"bucket"} // $self->{"bucket"}
$args{"bucket"} $self->{"bucket"}
Truth table66%2/3
A // B
A= $args{"bucket"}B= $self->{"bucket"}
ABresult
1X1
011
000
251 84 ▶ my $path = $args{abs_path} // join "", grep { defined } $self->{prefix}, $args{path};
Condition66%2/3
$args{"abs_path"} // join("", grep({defined $_;} $self->{"prefix"}, $args{"path"}))
l!l&&r!l&&!r
13710
MC/DC0%0/2
$args{"abs_path"} // join("", grep({defined $_;} $self->{"prefix"}, $args{"path"}))
$args{"abs_path"} join("", grep({defined $_;} $self->{"prefix"}, $args{"path"}))
Truth table66%2/3
A // B
A= $args{"abs_path"}B= join("", grep({defined $_;} $self->{"prefix"}, $args{"path"}))
ABresult
1X1
011
000
252  
253 84 ▶ my $scheme = $self->{ssl} ? "https" : "http";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ssl'} ? : 0 84 84
254  
255 84 my $uri;
256 84 ▶ if( length $bucket <= 63 and $bucket =~ m{^[A-Z0-9][A-Z0-9.-]+$}i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bucket <= 63 and $bucket =~ /^[A-Z0-9][A-Z0-9.-]+$/i) { } 84 0 84
Condition33%1/3
length $bucket <= 63 and $bucket =~ /^[A-Z0-9][A-Z0-9.-]+$/i
!ll&&!rl&&r
0084
MC/DC0%0/2
length $bucket <= 63 and $bucket =~ /^[A-Z0-9][A-Z0-9.-]+$/i
length $bucket <= 63 $bucket =~ /^[A-Z0-9][A-Z0-9.-]+$/i
Truth table33%1/3
A and B
A= length $bucket <= 63B= $bucket =~ /^[A-Z0-9][A-Z0-9.-]+$/i
ABresult
0X0
100
111
257 84 $uri = "$scheme://$bucket.$self->{host}/$path";
258 }
259 else {
260 0 $uri = "$scheme://$self->{host}/$bucket/$path";
261 }
262 84 ▶ $uri .= "?" . join( "&", @params ) if @params;
Branch: 1 branch
BranchTrueFalseTotal
if @params 42 42 84
263  
264 84 my $s3 = $self->{s3};
265  
266 84 ▶ my $meta = $args{meta} || {};
Condition100%2/2
$args{'meta'} || {}
l!l
381
MC/DC100%1/1
$args{'meta'} || {}
$args{'meta'}
Truth table100%2/2
A
A= $args{'meta'}
Aresult
11
00
267  
268 my @headers = (
269 Date => time2str( time ),
270 84 ▶ %{ $args{headers} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$args{"headers"} or {} 54 30 84
271 84 ( map { +"X-Amz-Meta-$_" => $meta->{$_} } sort keys %$meta ),
272 );
273  
274 84 my $request = HTTP::Request->new( $method, $uri, \@headers, $args{content} );
275 84 $request->content_length( length $args{content} );
276  
277 84 $self->_gen_auth_header( $request, $bucket, $path );
278  
279 84 return $request;
280 }
281  
282 sub _gen_auth_header
283 {
284 84 ▶ my $self = shift;
Subroutine _gen_auth_header: called CC=12 SCAR=24.9
285 84 my ( $request, $bucket, $path ) = @_;
286  
287 # See also
288 # http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#ConstructingTheAuthenticationHeader
289  
290 84 my $canon_resource = "/$bucket/$path";
291 84 ▶ if( defined $request->uri->query and length $request->uri->query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $request->uri->query and length $request->uri->query) 51 33 84
Condition66%2/3
defined $request->uri->query and length $request->uri->query
!ll&&!rl&&r
33051
MC/DC50%1/2
defined $request->uri->query and length $request->uri->query
defined $request->uri->query length $request->uri->query
Truth table66%2/3
A and B
A= defined $request->uri->queryB= length $request->uri->query
ABresult
0X0
100
111
292 51 my %params = $request->uri->query_form;
293 51 my %params_to_sign;
294 51 foreach (qw( partNumber uploadId )) {
295 102 ▶ $params_to_sign{$_} = $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 49 53 102
296 }
297  
298 51 my @params_to_sign;
299 51 foreach ( sort keys %params_to_sign ) {
300 49 ▶ push @params_to_sign, defined $params{$_} ? "$_=$params{$_}" : $_;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{$_} ? : 49 0 49
301 }
302  
303 51 ▶ $canon_resource .= "?" . join( "&", @params_to_sign ) if @params_to_sign;
Branch: 1 branch
BranchTrueFalseTotal
if @params_to_sign 29 22 51
304 }
305  
306 84 my %x_amz_headers;
307 $request->scan( sub {
308 197 ▶ $x_amz_headers{lc $_[0]} = $_[1] if $_[0] =~ m/^X-Amz-/i;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^X-Amz-/i 3 194 197
309 84 });
310  
311 84 my $x_amz_headers = "";
312 84 $x_amz_headers .= "$_:$x_amz_headers{$_}\n" for sort keys %x_amz_headers;
313  
314 84 ▶ my $buffer = join( "\n",
Condition83%5/6
$request->header("Content-MD5") // ""
l!l
975
$request->header("Content-Type") // ""
l!l
975
$request->header("Date") // ""
l!l
840
MC/DC100%1/1
$request->header("Content-MD5") // ""
$request->header("Content-MD5")
MC/DC100%1/1
$request->header("Content-Type") // ""
$request->header("Content-Type")
MC/DC0%0/1
$request->header("Date") // ""
$request->header("Date")
Truth table100%2/2
A
A= $request->header("Content-MD5")
Aresult
11
00
Truth table100%2/2
A
A= $request->header("Content-Type")
Aresult
11
00
Truth table50%1/2
A
A= $request->header("Date")
Aresult
11
00
315 $request->method,
316 $request->header( "Content-MD5" ) // "",
317 $request->header( "Content-Type" ) // "",
318 $request->header( "Date" ) // "",
319 $x_amz_headers . $canon_resource );
320  
321 84 my $s3 = $self->{s3};
322  
323 84 my $hmac = Digest::HMAC_SHA1->new( $self->{secret_key} );
324 84 $hmac->add( $buffer );
325  
326 84 my $access_key = $self->{access_key};
327 84 my $authkey = encode_base64( $hmac->digest, "" );
328  
329 84 $request->header( Authorization => "AWS $access_key:$authkey" );
330 }
331  
332 # Turn non-2xx results into errors
333 sub _do_request
334 {
335 84 ▶ my $self = shift;
Subroutine _do_request: called CC=1 SCAR=0.0
336 84 my ( $request, %args ) = @_;
337  
338 $self->{http}->do_request(
339 request => $request,
340 SSL => ( $request->uri->scheme eq "https" ),
341 %args
342 )->then_with_f( sub {
343 81 ▶ my ( $f, $resp ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
344  
345 81 my $code = $resp->code;
346 77 ▶ if( $code !~ m/^2/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code =~ /^2/) 11 66 77
347 11 my $message = $resp->message;
348 11 $message =~ s/\r$//; # HTTP::Response leaves the \r on this
349  
350 11 return Future->new->fail(
351 "$code $message", http => $resp, $request
352 );
353 }
354  
355 66 return $f;
356 84 });
357 }
358  
359 # Convert response into an XML XPathContext tree
360 sub _do_request_xpc
361 {
362 31 ▶ my $self = shift;
Subroutine _do_request_xpc: called CC=1 SCAR=0.0
363 31 my ( $request, @args ) = @_;
364  
365 $self->_do_request( $request, @args )->then( sub {
366 26 ▶ my $resp = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
367  
368 26 my $xpc = XML::LibXML::XPathContext->new( $libxml->parse_string( $resp->content ) );
369 26 $xpc->registerNs( s3 => "http://s3.amazonaws.com/doc/2006-03-01/" );
370  
371 26 return Future->wrap( $xpc );
372 31 });
373 }
374  
375 sub _retry
376 {
377 77 ▶ my $self = shift;
Subroutine _retry: called CC=1 SCAR=0.0
378 77 my ( $method, @args ) = @_;
379  
380 77 my $delay = 0.5;
381  
382 77 my $retries = $self->{max_retries};
383 try_repeat {
384 80 ▶ my ( $prev_f ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
385  
386 # Add a small delay after failure before retrying
387 80 ▶ my $delay_f =
Branch: 1 branch
BranchTrueFalseTotal
$prev_f ? : 3 77 80
388 $prev_f ? $self->loop->delay_future( after => ( $delay *= 2 ) )
389 : Future->new->done;
390  
391 80 ▶ $delay_f->then( sub { $self->$method( @args ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
392 } while => sub {
393 78 ▶ my $f = shift;
Subroutine __ANON__: called CC=6 SCAR=17.9
394 78 ▶ my ( $failure, $name, $response, $request ) = $f->failure or return 0; # success
Branch: 1 branch
BranchTrueFalseTotal
() = $f->failure or return 0 64 14 78
395 14 ▶ return 0 if defined $name and $name eq "http" and
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and $name eq "http" and $response and $response->code =~ /^4/ 5 9 14
Condition88%8/9
defined $name and $name eq "http" and $response and $response->code =~ /^4/
!ll&&!rl&&r
545
defined $name and $name eq "http" and $response
!ll&&!rl&&r
509
defined $name and $name eq "http"
!ll&&!rl&&r
419
MC/DC75%3/4
defined $name and $name eq "http" and $response and $response->code =~ /^4/
defined $name $name eq "http" $response $response->code =~ /^4/
Truth table80%4/5
A and B and C and D
A= defined $nameB= $name eq "http"C= $responseD= $response->code =~ /^4/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
396 $response and $response->code =~ m/^4/; # don't retry HTTP 4xx
397 9 return --$retries;
398 77 };
399 }
400  
401 =head2 $s3->list_bucket( %args ) ==> ( $keys, $prefixes )
402  
403 Requests a list of the keys in a bucket, optionally within some prefix.
404  
405 Takes the following named arguments:
406  
407 =over 8
408  
409 =item bucket => STR
410  
411 The name of the S3 bucket to query
412  
413 =item prefix => STR
414  
415 =item delimiter => STR
416  
417 Optional. If supplied, the prefix and delimiter to use to divide up the key
418 namespace. Keys will be divided on the C<delimiter> parameter, and only the
419 key space beginning with the given prefix will be queried.
420  
421 =back
422  
423 The Future will return two ARRAY references. The first provides a list of the
424 keys found within the given prefix, and the second will return a list of the
425 common prefixes of further nested keys.
426  
427 Each key in the C<$keys> list is given in a HASH reference containing
428  
429 =over 8
430  
431 =item key => STRING
432  
433 The key's name
434  
435 =item last_modified => STRING
436  
437 The last modification time of the key given in ISO 8601 format
438  
439 =item etag => STRING
440  
441 The entity tag of the key
442  
443 =item size => INT
444  
445 The size of the key's value, in bytes
446  
447 =item storage_class => STRING
448  
449 The S3 storage class of the key
450  
451 =back
452  
453 Each key in the C<$prefixes> list is given as a plain string.
454  
455 =cut
456  
457 sub _list_bucket
458 {
459 12 ▶ my $self = shift;
Subroutine _list_bucket: called CC=1 SCAR=0.0
460 12 my %args = @_;
461  
462 12 my @keys;
463 my @prefixes;
464  
465 my $f = repeat {
466 13 ▶ my ( $prev_f ) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
467  
468 13 ▶ my $marker = $prev_f ? $prev_f->get : undef;
Branch: 1 branch
BranchTrueFalseTotal
$prev_f ? : 1 12 13
469  
470 my $req = $self->_make_request(
471 method => "GET",
472 bucket => $args{bucket},
473 abs_path => "",
474 query_params => {
475 26 prefix => join( "", grep { defined } $self->{prefix}, $args{prefix} ),
476 delimiter => $args{delimiter},
477 marker => $marker,
478 max_keys => $self->{list_max_keys},
479 },
480 13 content => "",
481 );
482  
483 $self->_do_request_xpc( $req,
484 timeout => $args{timeout} // $self->{timeout},
485 stall_timeout => $args{stall_timeout} // $self->{stall_timeout},
486 )->then( sub {
487 8 ▶ my $xpc = shift;
Subroutine __ANON__: called CC=6 SCAR=17.9
488  
489 8 my $last_key;
490 8 foreach my $node ( $xpc->findnodes( ".//s3:Contents" ) ) {
491 4 my $key = $xpc->findvalue( ".//s3:Key", $node );
492 4 $last_key = $key;
493  
494 4 ▶ $key =~ s/^\Q$self->{prefix}\E// if defined $self->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"prefix"} 1 3 4
495 4 push @keys, {
496 key => $key,
497 last_modified => $xpc->findvalue( ".//s3:LastModified", $node ),
498 etag => $xpc->findvalue( ".//s3:ETag", $node ),
499 size => $xpc->findvalue( ".//s3:Size", $node ),
500 storage_class => $xpc->findvalue( ".//s3:StorageClass", $node ),
501 };
502 }
503  
504 8 foreach my $node ( $xpc->findnodes( ".//s3:CommonPrefixes" ) ) {
505 4 my $key = $xpc->findvalue( ".//s3:Prefix", $node );
506  
507 4 ▶ $key =~ s/^\Q$self->{prefix}\E// if defined $self->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"prefix"} 2 2 4
508 4 push @prefixes, $key;
509 }
510  
511 8 ▶ if( $xpc->findvalue( ".//s3:IsTruncated" ) eq "true" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpc->findvalue(".//s3:IsTruncated") eq "true") 1 7 8
512 1 return Future->wrap( $last_key );
513 }
514 7 return Future->new->done;
515 13 ▶ });
Condition66%4/6
$args{"timeout"} // $self->{"timeout"}
l!l&&r!l&&!r
1111
$args{"stall_timeout"} // $self->{"stall_timeout"}
l!l&&r!l&&!r
0013
MC/DC100%2/2
$args{"timeout"} // $self->{"timeout"}
$args{"timeout"} $self->{"timeout"}
MC/DC0%0/2
$args{"stall_timeout"} // $self->{"stall_timeout"}
$args{"stall_timeout"} $self->{"stall_timeout"}
Truth table100%3/3
A // B
A= $args{"timeout"}B= $self->{"timeout"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args{"stall_timeout"}B= $self->{"stall_timeout"}
ABresult
1X1
011
000
516 } while => sub {
517 13 ▶ my $f = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
518 12 ▶ !$f->failure and $f->get };
Branch: 1 branch
BranchTrueFalseTotal
not $f->failure and $f->get 8 5 13
519  
520 $f->then( sub {
521 7 ▶ return Future->wrap( \@keys, \@prefixes );
Subroutine __ANON__: called CC=1 SCAR=0.0
522 12 });
523 }
524  
525 sub list_bucket
526 {
527 9 ▶ my $self = shift;
Subroutine list_bucket: called CC=1 SCAR=0.0
Pod documented
528 9 $self->_retry( "_list_bucket", @_ );
529 }
530  
531 =head2 $s3->get_object( %args ) ==> ( $value, $response, $meta )
532  
533 Requests the value of a key from a bucket.
534  
535 Takes the following named arguments:
536  
537 =over 8
538  
539 =item bucket => STR
540  
541 The name of the S3 bucket to query
542  
543 =item key => STR
544  
545 The name of the key to query
546  
547 =item on_chunk => CODE
548  
549 Optional. If supplied, this code will be invoked repeatedly on receipt of more
550 bytes of the key's value. It will be passed the L<HTTP::Response> object
551 received in reply to the request, and a byte string containing more bytes of
552 the value. Its return value is not important.
553  
554 $on_chunk->( $header, $bytes )
555  
556 If this is supplied then the key's value will not be accumulated, and the
557 final result of the Future will be an empty string.
558  
559 =item byte_range => STRING
560  
561 Optional. If supplied, is used to set the C<Range> request header with
562 C<bytes> as the units. This gives a range of bytes of the object to fetch,
563 rather than fetching the entire content. The value must be as specified by
564 HTTP/1.1; i.e. a comma-separated list of ranges, where each range specifies a
565 start and optionally an inclusive stop byte index, separated by hypens.
566  
567 =item if_match => STRING
568  
569 Optional. If supplied, is used to set the C<If-Match> request header to the
570 given string, which should be an entity etag. If the requested object no
571 longer has this etag, the request will fail with an C<http> failure whose
572 response code is 412.
573  
574 =back
575  
576 The Future will return a byte string containing the key's value, the
577 L<HTTP::Response> that was received, and a hash reference containing any of
578 the metadata fields, if found in the response. If an C<on_chunk> code
579 reference is passed, the C<$value> string will be empty.
580  
581 If the entire content of the object is requested (i.e. if C<byte_range> is not
582 supplied) then stall timeout failures will be handled specially. If a stall
583 timeout happens while receiving the content, the request will be retried using
584 the C<Range> header to resume from progress so far. This will be repeated
585 while every attempt still makes progress, and such resumes will not be counted
586 as part of the normal retry count. The resume request also uses C<If-Match> to
587 ensure it only resumes the resource with matching ETag. If a resume request
588 fails for some reason (either because the ETag no longer matches or something
589 else) then this error is ignored, and the original stall timeout failure is
590 returned.
591  
592 =cut
593  
594 sub _head_then_get_object
595 {
596 18 ▶ my $self = shift;
Subroutine _head_then_get_object: called CC=2 SCAR=6.9
597 18 my %args = @_;
598  
599 18 my $if_match = $args{if_match};
600 18 my $byte_range = $args{byte_range};
601  
602 # TODO: This doesn't handle retries correctly
603 # But that said neither does the rest of this module, wrt: on_chunk streaming
604  
605 18 my $on_chunk = delete $args{on_chunk};
606  
607 18 my $header;
608 18 my $head_future = $self->loop->new_future;
609 18 my $value_future;
610 18 my $value_len = 0;
611 18 my $stall_failure_f;
612  
613 18 my $resume_on_stall = 1;
614  
615 # TODO: Right now I can't be bothered to write the logic required to update
616 # the user-requested byte_range (which may in complex cases contain multiple
617 # discontinuous ranges) after a stall to resume it. This probably could be
618 # done at some stage.
619 18 ▶ $resume_on_stall = 0 if defined $byte_range;
Branch: 1 branch
BranchTrueFalseTotal
if defined $byte_range 1 17 18
620  
621 ( try_repeat {
622 21 ▶ if( my $pos = $value_len ) {
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if (my $pos = $value_len) 3 18 21
623 3 $byte_range = "$pos-";
624 }
625  
626 my $request = $self->_make_request(
627 method => $args{method},
628 bucket => $args{bucket},
629 path => $args{key},
630 21 ▶ headers => {
Branch: 2 branches
BranchTrueFalseTotal
defined $if_match ? : 4 17 21
defined $byte_range ? : 4 17 21
631 ( defined $if_match ? ( "If-Match" => $if_match ) : () ),
632 ( defined $byte_range ? ( Range => "bytes=$byte_range" ) : () ),
633 },
634 content => "",
635 );
636  
637 $self->_do_request( $request,
638 timeout => $args{timeout},
639 stall_timeout => $args{stall_timeout} // $self->{stall_timeout},
640 on_header => sub {
641 21 ▶ my ( $this_header ) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
642 21 my $code = $this_header->code;
643  
644 21 ▶ if( $head_future->is_cancelled or $code !~ m/^2/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head_future->is_cancelled or not $code =~ /^2/) 7 14 21
Condition100%3/3
$head_future->is_cancelled or not $code =~ /^2/
l!l&&r!l&&!r
2514
MC/DC100%2/2
$head_future->is_cancelled or not $code =~ /^2/
$head_future->is_cancelled not $code =~ /^2/
Truth table100%3/3
A or not(B)
A= $head_future->is_cancelledB= not $code =~ /^2/
ABresult
1X1
011
000
645 # Just eat the body on cancellation or if it's not a 2xx
646 # For failures this will cause ->on_fail to occur and fail the
647 # $head_future
648 return sub {
649 14 ▶ return if @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if @_ 7 7 14
650 7 return $this_header;
651 7 };
652 }
653  
654 14 my %meta;
655 $this_header->scan( sub {
656 17 ▶ $_[0] =~ m/^X-Amz-Meta-(.*)$/i and $meta{$1} = $_[1];
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^X-Amz-Meta-(.*)$/i and $meta{$1} = $_[1] 2 15 17
657 14 });
658  
659 14 ▶ if( !$value_future ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value_future) 12 2 14
660 # First response
661 12 $header = $this_header;
662 # If we're going to retry this, ensure we only request this exact ETag
663 12 ▶ $if_match ||= $header->header( "ETag" );
Condition66%2/3
$if_match ||= $header->header("ETag")
l!l&&r!l&&!r
039
MC/DC50%1/2
$if_match ||= $header->header("ETag")
$if_match $header->header("ETag")
Truth table66%2/3
A ||= B
A= $if_matchB= $header->header("ETag")
ABresult
1X1
011
000
664  
665 12 $value_future = $head_future->new;
666 12 $head_future->done( $value_future, $header, \%meta );
667 }
668  
669 return sub {
670 # Code here could be 200 (OK), 206 (Partial Content) or 204 (No Content)
671 25 ▶ return $this_header if $code == 204;
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if $code == 204 0 25 25
672  
673 25 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 11 25
674 14 $value_len += length $_[0];
675 14 ▶ if( $on_chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_chunk) { } 3 11 14
676 3 $on_chunk->( $header, @_ );
677 }
678 else {
679 11 $header->add_content( $_[0] );
680 }
681 14 return;
682 }
683 11 return $header; # with no body content
684 14 };
685 }
686 21 ▶ );
Condition33%1/3
$args{"stall_timeout"} // $self->{"stall_timeout"}
l!l&&r!l&&!r
0021
MC/DC0%0/2
$args{"stall_timeout"} // $self->{"stall_timeout"}
$args{"stall_timeout"} $self->{"stall_timeout"}
Truth table33%1/3
A // B
A= $args{"stall_timeout"}B= $self->{"stall_timeout"}
ABresult
1X1
011
000
687 } while => sub {
688 21 ▶ my ( $prev_f ) = @_;
Subroutine __ANON__: called CC=6 SCAR=18.2
689 # repeat while it keeps failing with a stall timeout
690 21 ▶ return 0 if !$resume_on_stall or !$prev_f->failure;
Branch: 1 branch
BranchTrueFalseTotal
unless $resume_on_stall and $prev_f->failure 12 9 21
Condition100%3/3
$resume_on_stall and $prev_f->failure
!ll&&!rl&&r
1119
MC/DC100%2/2
$resume_on_stall and $prev_f->failure
$resume_on_stall $prev_f->failure
Truth table100%3/3
A and B
A= $resume_on_stallB= $prev_f->failure
ABresult
0X0
100
111
691  
692 9 ▶ my $op = ( $prev_f->failure )[1] // "";
Condition50%1/2
($prev_f->failure)[1] // ""
l!l
90
MC/DC0%0/1
($prev_f->failure)[1] // ""
($prev_f->failure)[1]
Truth table50%1/2
A
A= ($prev_f->failure)[1]
Aresult
11
00
693 9 ▶ return 0 if $op ne "stall_timeout";
Branch: 1 branch
BranchTrueFalseTotal
if $op ne "stall_timeout" 6 3 9
694  
695 3 ▶ $stall_failure_f ||= $prev_f;
Condition33%1/3
$stall_failure_f ||= $prev_f
l!l&&r!l&&!r
030
MC/DC0%0/2
$stall_failure_f ||= $prev_f
$stall_failure_f $prev_f
Truth table33%1/3
A ||= B
A= $stall_failure_fB= $prev_f
ABresult
1X1
011
000
696 3 return 1;
697 } )->on_done( sub {
698 12 ▶ my ( $response ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
699 12 ▶ return if $head_future->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $head_future->is_cancelled 1 11 12
700  
701 11 $value_future->done( $response->content, ( $head_future->get )[1,2] );
702 })->on_fail( sub {
703 6 ▶ my $f = $value_future || $head_future;
Subroutine __ANON__: called CC=4 SCAR=13.9
Condition66%2/3
$value_future || $head_future
l!l&&r!l&&!r
150
MC/DC0%0/2
$value_future || $head_future
$value_future $head_future
Truth table66%2/3
A || B
A= $value_futureB= $head_future
ABresult
1X1
011
000
704 # If we have a $stall_failure_f it means we must have attempted a resume
705 # after a stall timeout, then failed for a different reason. Ignore that
706 # second reason and just pretend to the caller that we stalled.
707 6 ▶ $f->fail( $stall_failure_f ? $stall_failure_f->failure : @_ ) if !$f->is_cancelled;
Branch: 2 branches
BranchTrueFalseTotal
if not $f->is_cancelled 5 1 6
$stall_failure_f ? : 1 4 5
708 18 });
709  
710 18 return $head_future;
711 }
712  
713 sub get_object
714 {
715 12 ▶ my $self = shift;
Subroutine get_object: called CC=1 SCAR=0.0
Pod documented
716 12 my %args = @_;
717  
718 $self->_retry( sub {
719 $self->_head_then_get_object( %args, method => "GET" )
720 12 ▶ ->then( sub { my ( $value_f ) = @_; $value_f }); # wait on the value
Subroutine __ANON__: called CC=1 SCAR=0.0
721 12 });
722 }
723  
724 =head2 $s3->head_object( %args ) ==> ( $response, $meta )
725  
726 Requests the value metadata of a key from a bucket. This is similar to the
727 C<get_object> method, but uses the C<HEAD> HTTP verb instead of C<GET>.
728  
729 Takes the same named arguments as C<get_object>, but will ignore an
730 C<on_chunk> callback, if provided.
731  
732 The Future will return the L<HTTP::Response> object and metadata hash
733 reference, without the content string (as no content is returned to a C<HEAD>
734 request).
735  
736 =cut
737  
738 sub head_object
739 {
740 2 ▶ my $self = shift;
Subroutine head_object: called CC=1 SCAR=0.0
Pod documented
741 2 my %args = @_;
742  
743 $self->_retry( sub {
744 $self->_head_then_get_object( %args, method => "HEAD" )
745 1 ▶ ->then( sub { my ( $value_f ) = @_; $value_f }) # wait on the empty body
Subroutine __ANON__: called CC=1 SCAR=0.0
746 2 ▶ ->then( sub { shift; Future->wrap( @_ ) }); # remove (empty) value
Subroutine __ANON__: called CC=1 SCAR=0.0
747 2 });
748 }
749  
750 =head2 $s3->head_then_get_object( %args ) ==> ( $value_f, $response, $meta )
751  
752 Performs a C<GET> operation similar to C<get_object>, but allows access to the
753 metadata header before the body content is complete.
754  
755 Takes the same named arguments as C<get_object>.
756  
757 The returned Future completes as soon as the metadata header has been received
758 and yields a second future (the body future), the L<HTTP::Response> and a hash
759 reference containing the metadata fields. The body future will eventually
760 yield the actual body, along with another copy of the response and metadata
761 hash reference.
762  
763 $value_f ==> $value, $response, $meta
764  
765 =cut
766  
767 sub head_then_get_object
768 {
769 4 ▶ my $self = shift;
Subroutine head_then_get_object: called CC=1 SCAR=0.0
Pod documented
770 4 $self->_retry( "_head_then_get_object", @_, method => "GET" );
771 }
772  
773 =head2 $s3->put_object( %args ) ==> ( $etag, $length )
774  
775 Sets a new value for a key in the bucket.
776  
777 Takes the following named arguments:
778  
779 =over 8
780  
781 =item bucket => STRING
782  
783 The name of the S3 bucket to put the value in
784  
785 =item key => STRING
786  
787 The name of the key to put the value in
788  
789 =item value => STRING
790  
791 =item value => Future giving STRING
792  
793 Optional. If provided, gives a byte string as the new value for the key or a
794 L<Future> which will eventually yield such.
795  
796 =item value => CODE
797  
798 =item value_length => INT
799  
800 Alternative form of C<value>, which is a C<CODE> reference to a generator
801 function. It will be called repeatedly to generate small chunks of content,
802 being passed the position and length it should yield.
803  
804 $chunk = $value->( $pos, $len )
805  
806 Typically this can be provided either by a C<substr> operation on a larger
807 string buffer, or a C<sysseek> and C<sysread> operation on a filehandle.
808  
809 In normal operation the function will just be called in a single sweep in
810 contiguous regions up to the extent given by C<value_length>. If however, the
811 MD5sum check fails at the end of upload, it will be called again to retry the
812 operation. The function must therefore be prepared to be invoked multiple
813 times over its range.
814  
815 =item value => Future giving ( CODE, INT )
816  
817 Alternative form of C<value>, in which a C<Future> eventually yields the value
818 generation C<CODE> reference and length. The C<CODE> reference is invoked as
819 documented above.
820  
821 ( $gen_value, $value_len ) = $value->get;
822  
823 $chunk = $gen_value->( $pos, $len );
824  
825 =item gen_parts => CODE
826  
827 Alternative to C<value> in the case of larger values, and implies the use of
828 multipart upload. Called repeatedly to generate successive parts of the
829 upload. Each time C<gen_parts> is called it should return one of the forms of
830 C<value> given above; namely, a byte string, a C<CODE> reference and size
831 pair, or a C<Future> which will eventually yield either of these forms.
832  
833 ( $value ) = $gen_parts->()
834  
835 ( $gen_value, $value_length ) = $gen_parts->()
836  
837 ( $value_f ) = $gen_parts->(); $value = $value_f->get
838 ( $gen_value, $value_length ) = $value_f->get
839  
840 Each case is analogous to the types that the C<value> key can take.
841  
842 =item meta => HASH
843  
844 Optional. If provided, gives additional user metadata fields to set on the
845 object, using the C<X-Amz-Meta-> fields.
846  
847 =item timeout => NUM
848  
849 Optional. For single-part uploads, this sets the C<timeout> argument to use
850 for the actual C<PUT> request. For multi-part uploads, this argument is
851 currently ignored.
852  
853 =item meta_timeout => NUM
854  
855 Optional. For multipart uploads, this sets the C<timeout> argument to use for
856 the initiate and complete requests, overriding a configured C<timeout>.
857 Ignored for single-part uploads.
858  
859 =item part_timeout => NUM
860  
861 Optional. For multipart uploads, this sets the C<timeout> argument to use for
862 the individual part C<PUT> requests. Ignored for single-part uploads.
863  
864 =item on_write => CODE
865  
866 Optional. If provided, this code will be invoked after each successful
867 C<syswrite> call on the underlying filehandle when writing actual file
868 content, indicating that the data was at least written as far as the
869 kernel. It will be passed the total byte length that has been written for this
870 call to C<put_object>. By the time the call has completed, this will be the
871 total written length of the object.
872  
873 $on_write->( $bytes_written )
874  
875 Note that because of retries it is possible this count will decrease, if a
876 part has to be retried due to e.g. a failing MD5 checksum.
877  
878 =item concurrent => INT
879  
880 Optional. If present, gives the number of parts to upload concurrently. If
881 absent, a default of 1 will apply (i.e. no concurrency).
882  
883 =back
884  
885 The Future will return a string containing the S3 ETag of the newly-set key,
886 and the length of the value in bytes.
887  
888 For single-part uploads the ETag will be the MD5 sum in hex, surrounded by
889 quote marks. For multi-part uploads this is a string in a different form,
890 though details of its generation are not specified by S3.
891  
892 The returned MD5 sum from S3 during upload will be checked against an
893 internally-generated MD5 sum of the content that was sent, and an error result
894 will be returned if these do not match.
895  
896 =cut
897  
898 sub _md5sum_wrap
899 {
900 35 ▶ my $content = shift;
Subroutine _md5sum_wrap: called CC=8 SCAR=24.3
901 35 my @args = my ( $md5ctx, $posref, $content_length, $read_size ) = @_;
902  
903 35 ▶ if( !defined $content or !ref $content ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $content && ref $content) { } 29 6 35
elsif (ref $content eq 'CODE') { } 6 0 6
elsif (builtin::blessed($content) and $content->isa('Future')) { } 0 0 0
Condition66%2/3
defined $content && ref $content
!ll&&!rl&&r
0296
MC/DC50%1/2
defined $content && ref $content
defined $content ref $content
Truth table66%2/3
A && B
A= defined $contentB= ref $content
ABresult
0X0
100
111
904 29 my $len = $content_length - $$posref;
905 29 ▶ if( defined $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) { } 29 0 29
906 29 ▶ substr( $content, $len ) = "" if length $content > $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $content > $len 0 29 29
907 }
908 else {
909 0 $content = "\0" x $len;
910 }
911  
912 29 $md5ctx->add( $content );
913 29 $$posref += length $content;
914  
915 29 return $content;
916 }
917 elsif( ref $content eq "CODE" ) {
918 return sub {
919 12 ▶ return undef if $$posref >= $content_length;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if $$posref >= $content_length 6 6 12
920  
921 6 my $len = $content_length - $$posref;
922 6 ▶ $len = $read_size if $len > $read_size;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $read_size 0 6 6
923  
924 6 my $chunk = $content->( $$posref, $len );
925 6 return _md5sum_wrap( $chunk, @args );
926 }
927 6 }
928 ▶ elsif( blessed $content and $content->isa( "Future" ) ) {
Condition0%0/3
builtin::blessed($content) and $content->isa("Future")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($content) and $content->isa("Future")
builtin::blessed($content) $content->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($content)B= $content->isa("Future")
ABresult
0X0
100
111
929 return $content->transform(
930 done => sub {
931 0 ▶ my ( $chunk ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
932 0 return _md5sum_wrap( $chunk, @args );
933 },
934 0 );
935 }
936 else {
937 0 die "TODO: md5sum wrap ref $content";
938 }
939 }
940  
941 sub _put_object
942 {
943 29 ▶ my $self = shift;
Subroutine _put_object: called CC=1 SCAR=0.1
944 29 my %args = @_;
945  
946 29 my $md5ctx = Digest::MD5->new;
947 29 my $on_write = $args{on_write};
948 29 my $pos = 0;
949  
950 # Make $content definitely a Future
951 Future->wrap( delete $args{content} )->then( sub {
952 29 ▶ my ( $content ) = @_;
Subroutine __ANON__: called CC=6 SCAR=18.2
953 my $content_length = @_ > 1 ? $_[1] :
954 ref $content ? $args{content_length} :
955 29 ▶ length $content;
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 ? : 2 27 29
ref $content ? : 4 23 27
956 29 ▶ defined $content_length or die "TODO: referential content $content needs length";
Branch: 1 branch
BranchTrueFalseTotal
defined $content_length or die "TODO: referential content $content needs length" 0 29 29
957  
958 29 my $request = $self->_make_request(
959 %args,
960 method => "PUT",
961 content => "", # Doesn't matter, it'll be ignored
962 );
963  
964 29 $request->content_length( $content_length );
965 29 $request->content( "" );
966  
967 $self->_do_request( $request,
968 timeout => $args{timeout},
969 stall_timeout => $args{stall_timeout} // $self->{stall_timeout},
970 expect_continue => 1,
971 29 ▶ request_body => _md5sum_wrap( $content, $md5ctx, \$pos, $content_length, $self->{read_size} ),
Branch: 1 branch
BranchTrueFalseTotal
$on_write ? : 7 22 29
Condition33%1/3
$args{"stall_timeout"} // $self->{"stall_timeout"}
l!l&&r!l&&!r
0029
MC/DC0%0/2
$args{"stall_timeout"} // $self->{"stall_timeout"}
$args{"stall_timeout"} $self->{"stall_timeout"}
Truth table33%1/3
A // B
A= $args{"stall_timeout"}B= $self->{"stall_timeout"}
ABresult
1X1
011
000
972 ( $on_write ? ( on_body_write => $on_write ) : () ),
973 );
974 })->then( sub {
975 27 ▶ my $resp = shift;
Subroutine __ANON__: called CC=4 SCAR=15.0
976  
977 27 ▶ defined( my $etag = $resp->header( "ETag" ) ) or
Branch: 1 branch
BranchTrueFalseTotal
defined(my $etag = $resp->header("ETag")) or return "Future"->new->die("Response did not provide an ETag header") 0 27 27
978 return Future->new->die( "Response did not provide an ETag header" );
979  
980 # Amazon S3 currently documents that the returned ETag header will be
981 # the MD5 hash of the content, surrounded in quote marks. We'd better
982 # hope this continues to be true... :/
983 27 ▶ my ( $got_md5 ) = lc($etag) =~ m/^"([0-9a-f]{32})"$/ or
Branch: 1 branch
BranchTrueFalseTotal
my($got_md5) = lc($etag) =~ /^"([0-9a-f]{32})"$/ or return "Future"->new->die("Returned ETag ($etag) does not look like an MD5 sum", $resp) 0 27 27
984 return Future->new->die( "Returned ETag ($etag) does not look like an MD5 sum", $resp );
985  
986 27 my $expect_md5 = lc($md5ctx->hexdigest);
987  
988 27 ▶ if( $got_md5 ne $expect_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_md5 ne $expect_md5) 0 27 27
989 0 return Future->new->die( "Returned MD5 hash ($got_md5) did not match expected ($expect_md5)", $resp );
990 }
991  
992 27 return Future->wrap( $etag, $pos );
993 29 });
994 }
995  
996 sub _initiate_multipart_upload
997 {
998 9 ▶ my $self = shift;
Subroutine _initiate_multipart_upload: called CC=2 SCAR=7.1
999 9 my %args = @_;
1000  
1001 my $req = $self->_make_request(
1002 method => "POST",
1003 bucket => $args{bucket},
1004 path => "$args{key}?uploads",
1005 content => "",
1006 meta => $args{meta},
1007 9 );
1008  
1009 $self->_do_request_xpc( $req,
1010 timeout => $args{timeout},
1011 stall_timeout => $args{stall_timeout} // $self->{stall_timeout},
1012 )->then( sub {
1013 9 ▶ my $xpc = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
1014  
1015 9 my $id = $xpc->findvalue( ".//s3:InitiateMultipartUploadResult/s3:UploadId" );
1016 9 return Future->wrap( $id );
1017 9 ▶ });
Condition33%1/3
$args{"stall_timeout"} // $self->{"stall_timeout"}
l!l&&r!l&&!r
009
MC/DC0%0/2
$args{"stall_timeout"} // $self->{"stall_timeout"}
$args{"stall_timeout"} $self->{"stall_timeout"}
Truth table33%1/3
A // B
A= $args{"stall_timeout"}B= $self->{"stall_timeout"}
ABresult
1X1
011
000
1018 }
1019  
1020 sub _complete_multipart_upload
1021 {
1022 9 ▶ my $self = shift;
Subroutine _complete_multipart_upload: called CC=2 SCAR=7.1
1023 9 my %args = @_;
1024  
1025 my $req = $self->_make_request(
1026 method => "POST",
1027 bucket => $args{bucket},
1028 path => $args{key},
1029 content => $args{content},
1030 query_params => {
1031 uploadId => $args{id},
1032 },
1033 headers => {
1034 "Content-Type" => "application/xml",
1035 9 "Content-MD5" => encode_base64( md5( $args{content} ), "" ),
1036 },
1037 );
1038  
1039 $self->_do_request_xpc( $req,
1040 timeout => $args{timeout},
1041 stall_timeout => $args{stall_timeout} // $self->{stall_timeout},
1042 )->then( sub {
1043 9 ▶ my $xpc = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
1044  
1045 9 my $etag = $xpc->findvalue( ".//s3:CompleteMultipartUploadResult/s3:ETag" );
1046 9 return Future->wrap( $etag );
1047 9 ▶ });
Condition33%1/3
$args{"stall_timeout"} // $self->{"stall_timeout"}
l!l&&r!l&&!r
009
MC/DC0%0/2
$args{"stall_timeout"} // $self->{"stall_timeout"}
$args{"stall_timeout"} $self->{"stall_timeout"}
Truth table33%1/3
A // B
A= $args{"stall_timeout"}B= $self->{"stall_timeout"}
ABresult
1X1
011
000
1048 }
1049  
1050 sub _put_object_multipart
1051 {
1052 9 ▶ my $self = shift;
Subroutine _put_object_multipart: called CC=2 SCAR=7.0
1053 9 my ( $gen_parts, %args ) = @_;
1054  
1055 9 my $on_write = $args{on_write};
1056  
1057 9 my $id;
1058  
1059 9 my $written_committed = 0; # bytes written in committed parts
1060 9 my %written_tentative; # {$part_num} => bytes written so far in this part
1061  
1062 $self->_retry( "_initiate_multipart_upload",
1063 timeout => $args{meta_timeout} // $self->{timeout},
1064 %args
1065 )->then( sub {
1066 9 ▶ ( $id ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
1067  
1068 9 my $part_num = 0;
1069 ( fmap1 {
1070 20 ▶ my ( $part_num, $content, %moreargs ) = @{$_[0]};
Subroutine __ANON__: called CC=2 SCAR=6.9
1071  
1072 $self->_retry( "_put_object",
1073 bucket => $args{bucket},
1074 path => $args{key},
1075 content => $content,
1076 query_params => {
1077 partNumber => $part_num,
1078 uploadId => $id,
1079 },
1080 timeout => $args{part_timeout},
1081 ( $on_write ?
1082 ( on_write => sub {
1083 6 ▶ $written_tentative{$part_num} = $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.0
1084 6 $on_write->( $written_committed + sum values %written_tentative );
1085 } ) : () ),
1086 %moreargs,
1087 )->then( sub {
1088 20 ▶ my ( $etag, $len ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1089 20 $written_committed += $len;
1090 20 delete $written_tentative{$part_num};
1091 20 return Future->wrap( [ $part_num, $etag ] );
1092 20 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$on_write ? : 6 14 20
1093 } generate => sub {
1094 31 ▶ my @content = $gen_parts->() or return;
Subroutine __ANON__: called CC=6 SCAR=21.4
Branch: 1 branch
BranchTrueFalseTotal
my(@content) = &$gen_parts() or return 11 20 31
1095 20 $part_num++;
1096 20 ▶ return [ $part_num, $content[0] ] if @content == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @content == 1 17 3 20
1097 3 ▶ return [ $part_num, $content[0], content_length => $content[1] ] if @content == 2 and ref $content[0] eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if @content == 2 and ref $content[0] eq "CODE" 3 0 3
Condition33%1/3
@content == 2 and ref $content[0] eq "CODE"
!ll&&!rl&&r
003
MC/DC0%0/2
@content == 2 and ref $content[0] eq "CODE"
@content == 2 ref $content[0] eq "CODE"
Truth table33%1/3
A and B
A= @content == 2B= ref $content[0] eq "CODE"
ABresult
0X0
100
111
1098 # It's possible that $content[0] is a 100MiByte string. Best not to
1099 # interpolate that into $@ or we'll risk OOM
1100 die "Unsure what to do with gen_part result " .
1101 0 ▶ join( ", ", map { ref $_ ? $_ : "<".length($_)." bytes>" } @content );
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
1102 9 ▶ }, concurrent => $args{concurrent} // $self->{put_concurrent} );
Condition66%2/3
$args{"concurrent"} // $self->{"put_concurrent"}
l!l&&r!l&&!r
108
MC/DC50%1/2
$args{"concurrent"} // $self->{"put_concurrent"}
$args{"concurrent"} $self->{"put_concurrent"}
Truth table66%2/3
A // B
A= $args{"concurrent"}B= $self->{"put_concurrent"}
ABresult
1X1
011
000
1103 })->then( sub {
1104 9 ▶ my @etags = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
1105  
1106 9 my $doc = XML::LibXML::Document->new( "1.0", "UTF-8" );
1107 9 $doc->addChild( my $root = $doc->createElement( "CompleteMultipartUpload" ) );
1108  
1109 #add content
1110 9 foreach ( @etags ) {
1111 20 my ( $part_num, $etag ) = @$_;
1112  
1113 20 $root->addChild( my $part = $doc->createElement('Part') );
1114 20 $part->appendTextChild( PartNumber => $part_num );
1115 20 $part->appendTextChild( ETag => $etag );
1116 }
1117  
1118 $self->_retry( "_complete_multipart_upload",
1119 %args,
1120 id => $id,
1121 content => $doc->toString,
1122 timeout => $args{meta_timeout} // $self->{timeout},
1123 )->then( sub {
1124 9 ▶ my ( $etag ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1125 9 return Future->wrap( $etag, $written_committed );
1126 9 ▶ });
Condition100%3/3
$args{"meta_timeout"} // $self->{"timeout"}
l!l&&r!l&&!r
117
MC/DC100%2/2
$args{"meta_timeout"} // $self->{"timeout"}
$args{"meta_timeout"} $self->{"timeout"}
Truth table100%3/3
A // B
A= $args{"meta_timeout"}B= $self->{"timeout"}
ABresult
1X1
011
000
1127 9 ▶ });
Condition100%3/3
$args{"meta_timeout"} // $self->{"timeout"}
l!l&&r!l&&!r
117
MC/DC100%2/2
$args{"meta_timeout"} // $self->{"timeout"}
$args{"meta_timeout"} $self->{"timeout"}
Truth table100%3/3
A // B
A= $args{"meta_timeout"}B= $self->{"timeout"}
ABresult
1X1
011
000
1128 }
1129  
1130 sub put_object
1131 {
1132 18 ▶ my $self = shift;
Subroutine put_object: called CC=5 SCAR=16.1
Pod documented
1133 18 my %args = @_;
1134  
1135 18 my $gen_parts;
1136 18 ▶ if( $gen_parts = delete $args{gen_parts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gen_parts = delete $args{'gen_parts'}) { } 9 9 18
1137 # OK
1138 }
1139 else {
1140 9 ▶ my $content_length = $args{value_length} // length $args{value};
Condition66%2/3
$args{"value_length"} // length $args{"value"}
l!l&&r!l&&!r
180
MC/DC0%0/2
$args{"value_length"} // length $args{"value"}
$args{"value_length"} length $args{"value"}
Truth table66%2/3
A // B
A= $args{"value_length"}B= length $args{"value"}
ABresult
1X1
011
000
1141  
1142 9 my $part_size = $self->{part_size};
1143  
1144 9 ▶ if( $content_length > $part_size * 10_000 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_length > $part_size * 10000) { } 0 9 9
elsif ($content_length > $part_size) { } 1 8 9
1145 0 croak "Cannot upload content in more than 10,000 parts - consider using a larger part_size";
1146 }
1147 elsif( $content_length > $part_size ) {
1148 $gen_parts = sub {
1149 3 ▶ return unless length $args{value};
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless length $args{"value"} 1 2 3
1150 2 return substr( $args{value}, 0, $part_size, "" );
1151 1 };
1152 }
1153 else {
1154 8 my @parts = ( [ delete $args{value}, $content_length ] );
1155 8 ▶ $gen_parts = sub { return unless @parts; @{ shift @parts } };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 8 8 16
1156 }
1157 }
1158  
1159 18 my @parts;
1160 18 push @parts, [ $gen_parts->() ];
1161  
1162 # Ensure first part is a Future then unfuture it
1163 18 Future->wrap( @{$parts[0]} )->then( sub {
1164 18 ▶ @{$parts[0]} = @_ if @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if @_ 17 1 18
1165  
1166 18 push @parts, [ $gen_parts->() ];
1167  
1168 18 ▶ if( @{ $parts[1] } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$parts[1];}) { } 9 9 18
elsif (@{$parts[0];}) { } 8 1 9
1169 # There are at least two parts; we'd better use multipart upload
1170 9 ▶ $self->_put_object_multipart( sub { @parts ? @{ shift @parts } : goto &$gen_parts }, %args );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 18 13 31
1171 }
1172 9 elsif( @{ $parts[0] } ) {
1173 # There is exactly one part
1174 8 my ( $content, $content_length ) = @{ shift @parts };
1175 $self->_retry( "_put_object",
1176 bucket => $args{bucket},
1177 path => $args{key},
1178 content => $content,
1179 content_length => $content_length,
1180 meta => $args{meta},
1181 8 %args,
1182 );
1183 }
1184 else {
1185 # There are no parts at all - create an empty object
1186 $self->_retry( "_put_object",
1187 bucket => $args{bucket},
1188 path => $args{key},
1189 content => "",
1190 meta => $args{meta},
1191 1 %args,
1192 );
1193 }
1194 18 });
1195 }
1196  
1197 =head2 $s3->delete_object( %args ) ==> ()
1198  
1199 Deletes a key from the bucket.
1200  
1201 Takes the following named arguments:
1202  
1203 =over 8
1204  
1205 =item bucket => STRING
1206  
1207 The name of the S3 bucket to put the value in
1208  
1209 =item key => STRING
1210  
1211 The name of the key to put the value in
1212  
1213 =back
1214  
1215 The Future will return nothing.
1216  
1217 =cut
1218  
1219 sub _delete_object
1220 {
1221 3 ▶ my $self = shift;
Subroutine _delete_object: called CC=3 SCAR=11.2
1222 3 my %args = @_;
1223  
1224 my $request = $self->_make_request(
1225 method => "DELETE",
1226 bucket => $args{bucket},
1227 path => $args{key},
1228 3 content => "",
1229 );
1230  
1231 $self->_do_request( $request,
1232 timeout => $args{timeout} // $self->{timeout},
1233 stall_timeout => $args{stall_timeout} // $self->{stall_timeout},
1234 )->then( sub {
1235 1 ▶ return Future->new->done;
Subroutine __ANON__: called CC=1 SCAR=0.0
1236 3 ▶ });
Condition66%4/6
$args{"timeout"} // $self->{"timeout"}
l!l&&r!l&&!r
111
$args{"stall_timeout"} // $self->{"stall_timeout"}
l!l&&r!l&&!r
003
MC/DC100%2/2
$args{"timeout"} // $self->{"timeout"}
$args{"timeout"} $self->{"timeout"}
MC/DC0%0/2
$args{"stall_timeout"} // $self->{"stall_timeout"}
$args{"stall_timeout"} $self->{"stall_timeout"}
Truth table100%3/3
A // B
A= $args{"timeout"}B= $self->{"timeout"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args{"stall_timeout"}B= $self->{"stall_timeout"}
ABresult
1X1
011
000
1237 }
1238  
1239 sub delete_object
1240 {
1241 3 ▶ my $self = shift;
Subroutine delete_object: called CC=1 SCAR=0.0
Pod documented
1242 3 $self->_retry( "_delete_object", @_ );
1243 }
1244  
1245 =head1 SPONSORS
1246  
1247 Parts of this code were paid for by
1248  
1249 =over 2
1250  
1251 =item *
1252  
1253 SocialFlow L<http://www.socialflow.com>
1254  
1255 =item *
1256  
1257 Shadowcat Systems L<http://www.shadow.cat>
1258  
1259 =back
1260  
1261 =head1 AUTHOR
1262  
1263 Paul Evans <leonerd@leonerd.org.uk>
1264  
1265 =cut
1266  
1267 10 0x55AA;
↑ Summary Build »