Net/Amazon/S3/Bucket.pm

filter: Statementstmt 90.2% 176 / 195 Branchbran 62.5% 45 / 72 Conditioncond 35.7% 10 / 28 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 96.4% 27 / 28 Podpod 94.4% 17 / 18 totaltotal 77.3% 276 / 357 CC 49 SCAR 25.1
CC
49
Cov
78%
__ANON__
72.0
_content_sub
19.3
add_key
7.1
CRAP 12.3
« Net/Amazon/S3/Client/Object.pm ↑ Summary Net/Amazon/S3/Utils.pm »
1 package Net::Amazon::S3::Bucket;
2 # ABSTRACT: convenience object for working with Amazon S3 buckets
3 99 $Net::Amazon::S3::Bucket::VERSION = '0.992';
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 99 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 99 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 99 ▶use IO::File 1.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 99 has 'account' => (
11 is => 'ro',
12 isa => 'Net::Amazon::S3',
13 required => 1,
14 handles => [qw[ err errstr ]],
15 );
16 99 has 'bucket' => ( is => 'ro', isa => 'Str', required => 1 );
17 99 has 'creation_date' => ( is => 'ro', isa => 'Maybe[Str]', required => 0 );
18  
19 has 'region' => (
20 is => 'ro',
21 lazy => 1,
22 predicate => 'has_region',
23 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 2 return $_[0]->account->vendor->guess_bucket_region ($_[0]);
25 },
26 99 );
27  
28 99 __PACKAGE__->meta->make_immutable;
29  
30 # returns bool
31 17 ▶sub add_key {
Subroutine add_key: called CC=7 SCAR=19.6
Pod documented
32 17 my $self = shift;
33 17 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_, ['key', 'value']);
34  
35 17 my $key = delete $args{key};
36 17 my $value = delete $args{value};
37  
38 17 ▶ if ( ref($value) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'SCALAR') { } 3 14 17
39 3 ▶ $args{'Content-Length'} ||= -s $$value;
Condition33%1/3
$args{"Content-Length"} ||= -s $$value
l!l&&r!l&&!r
030
MC/DC0%0/2
$args{"Content-Length"} ||= -s $$value
$args{"Content-Length"} -s $$value
Truth table33%1/3
A ||= B
A= $args{"Content-Length"}B= -s $$value
ABresult
1X1
011
000
40 3 $value = _content_sub($$value);
41 } else {
42 14 ▶ $args{'Content-Length'} ||= length $value;
Condition33%1/3
$args{"Content-Length"} ||= length $value
l!l&&r!l&&!r
0140
MC/DC0%0/2
$args{"Content-Length"} ||= length $value
$args{"Content-Length"} length $value
Truth table33%1/3
A ||= B
A= $args{"Content-Length"}B= length $value
ABresult
1X1
011
000
43 }
44  
45 17 my $acl;
46 17 ▶ $acl = delete $args{acl_short} if exists $args{acl_short};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"acl_short"} 1 16 17
47 17 ▶ $acl = delete $args{acl} if exists $args{acl};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"acl"} 10 7 17
48  
49 17 my $encryption = delete $args{encryption};
50 17 my %headers = %args;
51  
52 # we may get a 307 redirect; ask server to signal 100 Continue
53 # before reading the content from CODE reference (_content_sub)
54 17 ▶ $headers{expect} = '100-continue' if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 3 14 17
55  
56 17 my $response = $self->_perform_operation (
57 'Net::Amazon::S3::Operation::Object::Add',
58  
59 key => $key,
60 value => $value,
61 (acl => $acl) x!! defined $acl,
62 ((encryption => $encryption) x!! defined $encryption),
63 headers => \%headers,
64 );
65  
66 10 return $response->is_success;
67 }
68  
69 2 ▶sub add_key_filename {
Subroutine add_key_filename: called CC=1 SCAR=0.0
Pod documented
70 2 my $self = shift;
71 2 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_, ['key', 'value']);
72 2 $args{value} = \ delete $args{value};
73  
74 2 return $self->add_key (%args);
75 }
76  
77 6 ▶sub copy_key {
Subroutine copy_key: called CC=5 SCAR=16.9
Pod documented
78 6 my $self = shift;
79 6 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_, ['key', 'source' ]);
80  
81 6 my $key = delete $args{key};
82 6 my $source = delete $args{source};
83  
84 6 my $acl_short;
85 6 ▶ if (%args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%args) 6 0 6
86 6 ▶ if ( $args{acl_short} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"acl_short"}) 6 0 6
87 6 $acl_short = $args{acl_short};
88 6 delete $args{acl_short};
89 }
90 6 ▶ $args{Net::Amazon::S3::Constants->HEADER_METADATA_DIRECTIVE} ||= 'REPLACE';
Condition50%1/2
$args{"Net::Amazon::S3::Constants"->HEADER_METADATA_DIRECTIVE} ||= "REPLACE"
l!l
06
MC/DC0%0/1
$args{"Net::Amazon::S3::Constants"->HEADER_METADATA_DIRECTIVE} ||= "REPLACE"
$args{"Net::Amazon::S3::Constants"->HEADER_METADATA_DIRECTIVE}
Truth table50%1/2
A
A= $args{"Net::Amazon::S3::Constants"->HEADER_METADATA_DIRECTIVE}
Aresult
11
00
91 }
92  
93 6 $args{Net::Amazon::S3::Constants->HEADER_COPY_SOURCE} = $source;
94  
95 6 my $encryption = delete $args{encryption};
96  
97 6 my $acct = $self->account;
98 6 my $response = $self->_perform_operation (
99 'Net::Amazon::S3::Operation::Object::Add',
100  
101 value => '',
102 key => $key,
103 acl_short => $acl_short,
104 (encryption => $encryption) x!! defined $encryption,
105 headers => \%args,
106 );
107  
108 0 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
109 }
110  
111 2 ▶sub edit_metadata {
Subroutine edit_metadata: called CC=2 SCAR=7.0
Pod documented
112 2 my $self = shift;
113 2 my %args = Net::Amazon::S3::Utils->parse_arguments_with_object (\@_);
114  
115 2 my $key = delete $args{key};
116 2 ▶ croak "Need some metadata to change" unless %args;
Branch: 1 branch
BranchTrueFalseTotal
unless %args 0 2 2
117  
118 2 return $self->copy_key (
119 key => $key,
120 source => "/" . $self->bucket . "/" . $key,
121 %args,
122 );
123 }
124  
125 9 ▶sub head_key {
Subroutine head_key: called CC=1 SCAR=0.0
Pod documented
126 9 my $self = shift;
127 9 my %args = Net::Amazon::S3::Utils->parse_arguments_with_object (\@_);
128  
129 9 return $self->get_key (%args, method => 'HEAD', filename => undef);
130 }
131  
132 4 ▶sub query_string_authentication_uri {
Subroutine query_string_authentication_uri: called CC=2 SCAR=6.9
Pod documented
133 4 my $self = shift;
134 4 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_, ['key', 'expires_at']);
135  
136 4 ▶ $args{method} = 'GET' unless exists $args{method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"method"} 3 1 4
137  
138 my $request = Net::Amazon::S3::Operation::Object::Fetch::Request->new (
139 s3 => $self->account,
140 bucket => $self,
141 key => $args{key},
142 method => $args{method},
143 4 );
144  
145 4 return $request->query_string_authentication_uri ($args{expires_at});
146 }
147  
148 23 ▶sub get_key {
Subroutine get_key: called CC=6 SCAR=17.9
Pod documented
149 23 my $self = shift;
150 23 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_, ['key', 'method', 'filename']);
151  
152 3 $args{filename} = ${ delete $args{filename} }
153 23 ▶ if ref $args{filename};
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"filename"} 3 20 23
154  
155 23 ▶ $args{method} ||= 'GET';
Condition50%1/2
$args{"method"} ||= "GET"
l!l
230
MC/DC0%0/1
$args{"method"} ||= "GET"
$args{"method"}
Truth table50%1/2
A
A= $args{"method"}
Aresult
11
00
156  
157 my $response = $self->_perform_operation (
158 'Net::Amazon::S3::Operation::Object::Fetch',
159  
160 filename => $args{filename},
161 (range => $args{range}) x defined $args{range},
162  
163 key => $args{key},
164 method => $args{method},
165 23 );
166  
167 7 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 4 3 7
168 3 my $etag = $response->etag;
169  
170 3 my $return;
171 3 foreach my $header ($response->headers->header_field_names) {
172 15 $return->{ lc $header } = $response->header ($header);
173 }
174 3 ▶ $return->{content_length} = $response->content_length || 0;
Condition100%2/2
$response->content_length || 0
l!l
21
MC/DC100%1/1
$response->content_length || 0
$response->content_length
Truth table100%2/2
A
A= $response->content_length
Aresult
11
00
175 3 $return->{content_type} = $response->content_type;
176 3 $return->{etag} = $etag;
177 3 $return->{value} = $response->content;
178  
179 3 return $return;
180  
181 }
182  
183 1 ▶sub get_key_filename {
Subroutine get_key_filename: called CC=1 SCAR=0.0
Pod documented
184 1 my $self = shift;
185 1 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_, ['key', 'method', 'filename']);
186  
187 1 $args{filename} = \ delete $args{filename};
188 1 return $self->get_key (%args);
189 }
190  
191 # returns bool
192 5 ▶sub delete_multi_object {
Subroutine delete_multi_object: called CC=6 SCAR=18.2
Pod undocumented
193 5 my $self = shift;
194 5 my @objects = @_;
195 5 ▶ return unless( scalar(@objects) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @objects 0 5 5
196  
197 # Since delete can handle up to 1000 requests, be a little bit nicer
198 # and slice up requests and also allow keys to be strings
199 # rather than only objects.
200 5 my $last_result;
201 5 ▶ while (scalar(@objects) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @objects > 0) 5 1 6
202 my $response = $self->_perform_operation (
203 'Net::Amazon::S3::Operation::Objects::Delete',
204  
205 keys => [
206 5 ▶ map { ref ($_) ? $_->key : $_ }
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 0 11 11
scalar @objects > 1000 ? : 0 5 5
207 splice @objects, 0, ((scalar(@objects) > 1000) ? 1000 : scalar(@objects))
208 ]
209 );
210  
211 4 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 3 1 4
212 }
213  
214 1 return 1;
215 }
216  
217 9 ▶sub delete_key {
Subroutine delete_key: called CC=3 SCAR=11.8
Pod documented
218 9 my $self = shift;
219 9 my %args = Net::Amazon::S3::Utils->parse_arguments_with_object (\@_);
220  
221 9 ▶ croak 'must specify key' unless defined $args{key} && length $args{key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"key"} and length $args{"key"} 0 9 9
Condition33%1/3
defined $args{"key"} and length $args{"key"}
!ll&&!rl&&r
009
MC/DC0%0/2
defined $args{"key"} and length $args{"key"}
defined $args{"key"} length $args{"key"}
Truth table33%1/3
A and B
A= defined $args{"key"}B= length $args{"key"}
ABresult
0X0
100
111
222  
223 9 my $response = $self->_perform_operation (
224 'Net::Amazon::S3::Operation::Object::Delete',
225 %args,
226 );
227  
228 5 return $response->is_success;
229 }
230  
231 1 ▶sub delete_bucket {
Subroutine delete_bucket: called CC=1 SCAR=0.0
Pod documented
232 1 my $self = shift;
233 1 return $self->account->delete_bucket (bucket => $self, @_);
234 }
235  
236 2 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
237 2 my $self = shift;
238 2 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_);
239  
240 2 return $self->account->list_bucket ($self, %args);
241 }
242  
243 2 ▶sub list_all {
Subroutine list_all: called CC=1 SCAR=0.0
Pod documented
244 2 my $self = shift;
245 2 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_);
246  
247 2 return $self->account->list_bucket_all ($self, %args);
248 }
249  
250 14 ▶sub get_acl {
Subroutine get_acl: called CC=3 SCAR=11.0
Pod documented
251 14 my $self = shift;
252 14 my %args = Net::Amazon::S3::Utils->parse_arguments_with_object (\@_);
253  
254 14 my $response;
255 14 ▶ if (defined $args{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'key'}) { } 8 6 14
256 8 $response = $self->_perform_operation (
257 'Net::Amazon::S3::Operation::Object::Acl::Fetch',
258 %args,
259 );
260 } else {
261 6 delete $args{key};
262 6 $response = $self->_perform_operation (
263 'Net::Amazon::S3::Operation::Bucket::Acl::Fetch',
264 %args,
265 );
266 }
267  
268 5 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 3 2 5
269 2 return $response->content;
270 }
271  
272 24 ▶sub set_acl {
Subroutine set_acl: called CC=3 SCAR=11.0
Pod documented
273 24 my $self = shift;
274 24 my %args = Net::Amazon::S3::Utils->parse_arguments_with_object (\@_);
275  
276 24 my $response;
277 24 ▶ if (defined $args{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'key'}) { } 13 11 24
278 13 $response = $self->_perform_operation (
279 'Net::Amazon::S3::Operation::Object::Acl::Set',
280 %args,
281 );
282 } else {
283 11 delete $args{key};
284 11 $response = $self->_perform_operation (
285 'Net::Amazon::S3::Operation::Bucket::Acl::Set',
286 %args,
287 );
288 }
289  
290 18 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 7 11 18
291 11 return 1;
292 }
293  
294 2 ▶sub get_location_constraint {
Subroutine get_location_constraint: called CC=2 SCAR=10.1
Pod documented
295 2 my $self = shift;
296 2 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_);
297  
298 2 my $response = $self->_perform_operation (
299 'Net::Amazon::S3::Operation::Bucket::Location',
300 %args,
301 );
302  
303 0 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
304 0 return $response->location;
305 }
306  
307 14 ▶sub add_tags {
Subroutine add_tags: called CC=3 SCAR=11.0
Pod documented
308 14 my $self = shift;
309 14 ▶ my %args = @_ == 1 ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 11 3 14
310  
311 14 my $response;
312 14 ▶ if (defined $args{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'key'}) { } 8 6 14
313 8 $response = $self->_perform_operation (
314 'Net::Amazon::S3::Operation::Object::Tags::Add',
315 %args,
316 );
317 } else {
318 6 delete $args{key};
319 6 $response = $self->_perform_operation (
320 'Net::Amazon::S3::Operation::Bucket::Tags::Add',
321 %args,
322 );
323 }
324  
325 8 return $response->is_success;
326 }
327  
328 15 ▶sub delete_tags {
Subroutine delete_tags: called CC=2 SCAR=6.9
Pod documented
329 15 my $self = shift;
330 15 my %args = Net::Amazon::S3::Utils->parse_arguments_with_object (\@_);
331  
332 15 my $response;
333 15 ▶ if (defined $args{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'key'}) { } 9 6 15
334 9 $response = $self->_perform_operation (
335 'Net::Amazon::S3::Operation::Object::Tags::Delete',
336 %args,
337 );
338 } else {
339 6 delete $args{key};
340 6 $response = $self->_perform_operation (
341 'Net::Amazon::S3::Operation::Bucket::Tags::Delete',
342 %args,
343 );
344 }
345  
346 9 return $response->is_success;
347 }
348  
349 3 ▶sub _content_sub {
Subroutine _content_sub: called CC=6 SCAR=29.6
350 3 my $filename = shift;
351 3 my $stat = stat($filename);
352 3 my $remaining = $stat->size;
353 3 ▶ my $blksize = $stat->blksize || 4096;
Condition50%1/2
$stat->blksize || 4096
l!l
30
MC/DC0%0/1
$stat->blksize || 4096
$stat->blksize
Truth table50%1/2
A
A= $stat->blksize
Aresult
11
00
354  
355 3 ▶ croak "$filename not a readable file with fixed size"
Branch: 1 branch
BranchTrueFalseTotal
unless -r $filename and -f _ || $remaining 0 3 3
Condition33%2/6
-r $filename and -f _ || $remaining
!ll&&!rl&&r
003
-f _ or $remaining
l!l&&r!l&&!r
300
MC/DC0%0/3
-r $filename and -f _ || $remaining
-r $filename -f _ $remaining
Truth table25%1/4
A and B or C
A= -r $filenameB= -f _C= $remaining
ABCresult
0XX0
1000
11X1
1011
356 unless -r $filename and ( -f _ || $remaining );
357 3 ▶ my $fh = IO::File->new( $filename, 'r' )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($filename, "r") or croak("Could not open ${filename}: $!") 3 0 3
358 or croak "Could not open $filename: $!";
359 3 $fh->binmode;
360  
361 0 ▶ return sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
362 0 my $buffer;
363  
364 # upon retries the file is closed and we must reopen it
365 0 ▶ unless ( $fh->opened ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->opened) 0 0 0
366 0 ▶ $fh = IO::File->new( $filename, 'r' )
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($filename, "r") or croak("Could not open ${filename}: $!") 0 0 0
367 or croak "Could not open $filename: $!";
368 0 $fh->binmode;
369 0 $remaining = $stat->size;
370 }
371  
372 # warn "read remaining $remaining";
373 0 ▶ unless ( my $read = $fh->read( $buffer, $blksize ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (my $read = $fh->read($buffer, $blksize)) 0 0 0
374  
375 # warn "read $read buffer $buffer remaining $remaining";
376 0 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $! and $remaining 0 0 0
Condition0%0/3
$! and $remaining
!ll&&!rl&&r
000
MC/DC0%0/2
$! and $remaining
$! $remaining
Truth table0%0/3
A and B
A= $!B= $remaining
ABresult
0X0
100
111
377 "Error while reading upload content $filename ($remaining remaining) $!"
378 if $! and $remaining;
379  
380 # otherwise, we found EOF
381 0 ▶ $fh->close
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak("close of upload content $filename failed: $!") 0 0 0
382 or croak "close of upload content $filename failed: $!";
383 0 ▶ $buffer ||= ''
Condition0%0/2
$buffer ||= ""
l!l
00
MC/DC0%0/1
$buffer ||= ""
$buffer
Truth table0%0/2
A
A= $buffer
Aresult
11
00
384 ; # LWP expects an emptry string on finish, read returns 0
385 }
386 0 $remaining -= length($buffer);
387 0 return $buffer;
388 3 };
389 }
390  
391 1 ▶sub _head_region {
Subroutine _head_region: called CC=5 SCAR=17.5
392 1 my ($self) = @_;
393  
394 1 ▶ my $protocol = $self->account->secure ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
$self->account->secure ? : 1 0 1
395 1 my $host = $self->account->host;
396 1 my $path = $self->bucket;
397 1 my @retry = (1, 2, (4) x 8);
398  
399 1 ▶ if ($self->account->use_virtual_host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->account->use_virtual_host) 1 0 1
400 1 $host = "$path.$host";
401 1 $path = '';
402 }
403  
404 1 my $request_uri = "${protocol}://${host}/$path";
405 1 ▶ while (@retry) {
Branch: 1 branch
BranchTrueFalseTotal
if (@retry) 1 0 1
406 1 my $request = HTTP::Request->new (HEAD => $request_uri);
407  
408 # Disable redirects
409 1 my $requests_redirectable = $self->account->ua->requests_redirectable;
410 1 $self->account->ua->requests_redirectable( [] );
411  
412 1 my $response = $self->account->ua->request ($request);
413  
414 1 $self->account->ua->requests_redirectable( $requests_redirectable );
415  
416 1 ▶ return $response->header (Net::Amazon::S3::Constants->HEADER_BUCKET_REGION)
Branch: 1 branch
BranchTrueFalseTotal
if $response->header("Net::Amazon::S3::Constants"->HEADER_BUCKET_REGION) 1 0 1
417 if $response->header (Net::Amazon::S3::Constants->HEADER_BUCKET_REGION);
418  
419 0 print STDERR "Invalid bucket head response; $request_uri\n";
420 0 print STDERR $response->as_string;
421  
422 0 sleep shift @retry;
423 }
424  
425 0 die "Cannot determine bucket region; bucket=${\ $self->bucket }";
426 }
427  
428 129 ▶sub _perform_operation {
Subroutine _perform_operation: called CC=1 SCAR=0.0
429 129 my ($self, $operation, %params) = @_;
430  
431 129 $self->account->_perform_operation ($operation => (
432 bucket => $self,
433 %params,
434 ));
435 }
436  
437 99 1;
438  
439 __END__
« Net/Amazon/S3/Client/Object.pm ↑ Summary Net/Amazon/S3/Utils.pm »