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 SCAR 43.0
CC
49
Cov
78%
__ANON__
72.0
_content_sub
19.3
add_key
7.1
CRAP 73.5
« Net/Amazon/S3/Client/Object.pm ↑ Summary Shared/Examples/Net/Amazon/S3.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.99';
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 default => sub {
24 2 ▶ return $_[0]->account->vendor->guess_bucket_region ($_[0]);
Subroutine __ANON__: called CC=1 SCAR=0.0
25 },
26 99 );
27  
28 99 __PACKAGE__->meta->make_immutable;
29  
30 # returns bool
31 sub add_key {
32 17 ▶ my $self = shift;
Subroutine add_key: called CC=7 SCAR=19.6
Pod documented
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 sub add_key_filename {
70 2 ▶ my $self = shift;
Subroutine add_key_filename: called CC=1 SCAR=0.0
Pod documented
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 sub copy_key {
78 6 ▶ my $self = shift;
Subroutine copy_key: called CC=5 SCAR=16.9
Pod documented
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 sub edit_metadata {
112 2 ▶ my $self = shift;
Subroutine edit_metadata: called CC=2 SCAR=7.0
Pod documented
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 sub head_key {
126 9 ▶ my $self = shift;
Subroutine head_key: called CC=1 SCAR=0.0
Pod documented
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 sub query_string_authentication_uri {
133 4 ▶ my $self = shift;
Subroutine query_string_authentication_uri: called CC=2 SCAR=6.9
Pod documented
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 sub get_key {
149 23 ▶ my $self = shift;
Subroutine get_key: called CC=6 SCAR=17.9
Pod documented
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 sub get_key_filename {
184 1 ▶ my $self = shift;
Subroutine get_key_filename: called CC=1 SCAR=0.0
Pod documented
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 sub delete_multi_object {
193 5 ▶ my $self = shift;
Subroutine delete_multi_object: called CC=6 SCAR=18.2
Pod undocumented
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 sub delete_key {
218 9 ▶ my $self = shift;
Subroutine delete_key: called CC=3 SCAR=11.8
Pod documented
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 sub delete_bucket {
232 1 ▶ my $self = shift;
Subroutine delete_bucket: called CC=1 SCAR=0.0
Pod documented
233 1 return $self->account->delete_bucket (bucket => $self, @_);
234 }
235  
236 sub list {
237 2 ▶ my $self = shift;
Subroutine list: called CC=1 SCAR=0.0
Pod documented
238 2 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_);
239  
240 2 return $self->account->list_bucket ($self, %args);
241 }
242  
243 sub list_all {
244 2 ▶ my $self = shift;
Subroutine list_all: called CC=1 SCAR=0.0
Pod documented
245 2 my %args = Net::Amazon::S3::Utils->parse_arguments (\@_);
246  
247 2 return $self->account->list_bucket_all ($self, %args);
248 }
249  
250 sub get_acl {
251 14 ▶ my $self = shift;
Subroutine get_acl: called CC=3 SCAR=11.0
Pod documented
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 sub set_acl {
273 24 ▶ my $self = shift;
Subroutine set_acl: called CC=3 SCAR=11.0
Pod documented
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 sub get_location_constraint {
295 2 ▶ my $self = shift;
Subroutine get_location_constraint: called CC=2 SCAR=10.1
Pod documented
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 sub add_tags {
308 14 ▶ my $self = shift;
Subroutine add_tags: called CC=3 SCAR=11.0
Pod documented
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 sub delete_tags {
329 15 ▶ my $self = shift;
Subroutine delete_tags: called CC=2 SCAR=6.9
Pod documented
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 sub _content_sub {
350 3 ▶ my $filename = shift;
Subroutine _content_sub: called CC=6 SCAR=29.6
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
030
-f _ or $remaining
l!l&&r!l&&!r
300
MC/DC0%0/3
-r $filename and -f _ || $remaining
-r $filename -f _ $remaining
Truth table0%0/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}: $!") 0 3 3
358 or croak "Could not open $filename: $!";
359 3 $fh->binmode;
360  
361 return sub {
362 0 ▶ my $buffer;
Subroutine __ANON__: not called CC=8 SCAR=42.8
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 sub _head_region {
392 1 ▶ my ($self) = @_;
Subroutine _head_region: called CC=5 SCAR=17.5
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 sub _perform_operation {
429 129 ▶ my ($self, $operation, %params) = @_;
Subroutine _perform_operation: called CC=1 SCAR=0.0
430  
431 129 $self->account->_perform_operation ($operation => (
432 bucket => $self,
433 %params,
434 ));
435 }
436  
437 99 1;
438  
439 __END__
440  
441 =pod
442  
443 =encoding UTF-8
444  
445 =head1 NAME
446  
447 Net::Amazon::S3::Bucket - convenience object for working with Amazon S3 buckets
448  
449 =head1 VERSION
450  
451 version 0.99
452  
453 =head1 SYNOPSIS
454  
455 use Net::Amazon::S3;
456  
457 my $bucket = $s3->bucket("foo");
458  
459 ok($bucket->add_key("key", "data"));
460 ok($bucket->add_key("key", "data", {
461 content_type => "text/html",
462 'x-amz-meta-colour' => 'orange',
463 }));
464  
465 # Enable server-side encryption
466 ok($bucket->add_key("key", "data", {
467 encryption => 'AES256',
468 }));
469  
470 # the err and errstr methods just proxy up to the Net::Amazon::S3's
471 # objects err/errstr methods.
472 $bucket->add_key("bar", "baz") or
473 die $bucket->err . $bucket->errstr;
474  
475 # fetch a key
476 $val = $bucket->get_key("key");
477 is( $val->{value}, 'data' );
478 is( $val->{content_type}, 'text/html' );
479 is( $val->{etag}, 'b9ece18c950afbfa6b0fdbfa4ff731d3' );
480 is( $val->{'x-amz-meta-colour'}, 'orange' );
481  
482 # fetch a part of the key
483 $val = $bucket->get_key("key", { range => "bytes=1024-10240" });
484  
485 # returns undef on missing or on error (check $bucket->err)
486 is(undef, $bucket->get_key("non-existing-key"));
487 die $bucket->errstr if $bucket->err;
488  
489 # fetch a key's metadata
490 $val = $bucket->head_key("key");
491 is( $val->{value}, '' );
492 is( $val->{content_type}, 'text/html' );
493 is( $val->{etag}, 'b9ece18c950afbfa6b0fdbfa4ff731d3' );
494 is( $val->{'x-amz-meta-colour'}, 'orange' );
495  
496 # delete a key
497 ok($bucket->delete_key($key_name));
498 ok(! $bucket->delete_key("non-exist-key"));
499  
500 # delete the entire bucket (Amazon requires it first be empty)
501 $bucket->delete_bucket;
502  
503 =head1 DESCRIPTION
504  
505 This module represents an S3 bucket. You get a bucket object
506 from the Net::Amazon::S3 object.
507  
508 =head1 METHODS
509  
510 =head2 new
511  
512 Create a new bucket object. Expects a hash containing these two arguments:
513  
514 =over
515  
516 =item bucket
517  
518 =item account
519  
520 =back
521  
522 =head2 add_key
523  
524 Takes three positional parameters:
525  
526 =over
527  
528 =item key
529  
530 =item value
531  
532 =item configuration
533  
534 A hash of configuration data for this key.
535  
536 =over
537  
538 =item acl
539  
540 =item encryption
541  
542 =item any additional HTTP header
543  
544 =back
545  
546 See L<Net::Amazon::S3::Operation::Object::Add::Request> for details
547  
548 =back
549  
550 Returns a boolean.
551  
552 =head2 add_key_filename
553  
554 Use this to upload a large file to S3. Takes three positional parameters:
555  
556 =over
557  
558 =item key
559  
560 =item filename
561  
562 =item configuration
563  
564 A hash of configuration data for this key. (See synopsis);
565  
566 =back
567  
568 Returns a boolean.
569  
570 =head2 copy_key
571  
572 Creates (or replaces) a key, copying its contents from another key elsewhere in S3.
573 Takes the following parameters:
574  
575 =over
576  
577 =item key
578  
579 The key to (over)write
580  
581 =item source
582  
583 Where to copy the key from. Should be in the form C</I<bucketname>/I<keyname>>/.
584  
585 =item conf
586  
587 Optional configuration hash. If present and defined, the configuration (ACL
588 and headers) there will be used for the new key; otherwise it will be copied
589 from the source key.
590  
591 =back
592  
593 =head2 edit_metadata
594  
595 Changes the metadata associated with an existing key. Arguments:
596  
597 =over
598  
599 =item key
600  
601 The key to edit
602  
603 =item conf
604  
605 The new configuration hash to use
606  
607 =back
608  
609 =head2 head_key KEY
610  
611 Takes the name of a key in this bucket and returns its configuration hash
612  
613 =head2 query_string_authentication_uri KEY, EXPIRES_AT
614  
615 my $uri = $bucket->query_string_authentication_uri (
616 key => 'foo',
617 expires_at => time + 3_600, # valid for one hour
618 );
619  
620 my $uri = $bucket->query_string_authentication_uri (
621 key => 'foo',
622 expires_at => time + 3_600,
623 method => 'PUT',
624 );
625  
626 Returns uri presigned with your credentials.
627  
628 When used with Signature V4 you have to specify also HTTP method this
629 presigned uri will be used for (default: C<GET>)
630  
631 Method provides authenticated uri only for direct object operations.
632  
633 Method follows API's L</"CALLING CONVENTION">.
634  
635 Recognized positional arguments (mandatory).
636  
637 =over
638  
639 =item key
640  
641 =item expires_at
642  
643 Expiration time (epoch time).
644  
645 =back
646  
647 Optional arguments
648  
649 =over
650  
651 =item method
652  
653 Default: C<GET>
654  
655 Intended HTTP method this uri will be presigned for.
656  
657 Signature V2 doesn't use it but Signature V4 does.
658  
659 See L<https://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObject.html>
660  
661 =back
662  
663 =head2 get_key $key_name [$method]
664  
665 Takes a key name and an optional HTTP method (which defaults to C<GET>.
666 Fetches the key from AWS.
667  
668 On failure:
669  
670 Returns undef on missing content, throws an exception (dies) on server errors.
671  
672 On success:
673  
674 Returns a hashref of { content_type, etag, value, @meta } on success. Other
675 values from the server are there too, with the key being lowercased.
676  
677 =head2 get_key_filename $key_name $method $filename
678  
679 Use this to download large files from S3. Takes a key name and an optional
680 HTTP method (which defaults to C<GET>. Fetches the key from AWS and writes
681 it to the filename. THe value returned will be empty.
682  
683 On failure:
684  
685 Returns undef on missing content, throws an exception (dies) on server errors.
686  
687 On success:
688  
689 Returns a hashref of { content_type, etag, value, @meta } on success
690  
691 =head2 delete_key $key_name
692  
693 Removes C<$key> from the bucket. Forever. It's gone after this.
694  
695 Returns true on success and false on failure
696  
697 =head2 delete_bucket
698  
699 Delete the current bucket object from the server. Takes no arguments.
700  
701 Fails if the bucket has anything in it.
702  
703 This is an alias for C<< $s3->delete_bucket($bucket) >>
704  
705 =head2 list
706  
707 List all keys in this bucket.
708  
709 see L<Net::Amazon::S3/list_bucket> for documentation of this method.
710  
711 =head2 list_all
712  
713 List all keys in this bucket without having to worry about
714 'marker'. This may make multiple requests to S3 under the hood.
715  
716 see L<Net::Amazon::S3/list_bucket_all> for documentation of this method.
717  
718 =head2 get_acl
719  
720 Takes one optional positional parameter
721  
722 =over
723  
724 =item key (optional)
725  
726 If no key is specified, it returns the acl for the bucket.
727  
728 =back
729  
730 Returns an acl in XML format.
731  
732 =head2 set_acl
733  
734 Takes a configuration hash_ref containing:
735  
736 =over
737  
738 =item acl_xml (cannot be used in conjunction with acl_short)
739  
740 An XML string which contains access control information which matches
741 Amazon's published schema. There is an example of one of these XML strings
742 in the tests for this module.
743  
744 =item acl_short (cannot be used in conjunction with acl_xml)
745  
746 You can use the shorthand notation instead of specifying XML for
747 certain 'canned' types of acls.
748  
749 (from the Amazon API documentation)
750  
751 private: Owner gets FULL_CONTROL. No one else has any access rights.
752 This is the default.
753  
754 public-read:Owner gets FULL_CONTROL and the anonymous principal is granted
755 READ access. If this policy is used on an object, it can be read from a
756 browser with no authentication.
757  
758 public-read-write:Owner gets FULL_CONTROL, the anonymous principal is
759 granted READ and WRITE access. This is a useful policy to apply to a bucket,
760 if you intend for any anonymous user to PUT objects into the bucket.
761  
762 authenticated-read:Owner gets FULL_CONTROL, and any principal authenticated
763 as a registered Amazon S3 user is granted READ access.
764  
765 =item key (optional)
766  
767 If the key is not set, it will apply the acl to the bucket.
768  
769 =back
770  
771 Returns a boolean.
772  
773 =head2 get_location_constraint
774  
775 Retrieves the location constraint set when the bucket was created. Returns a
776 string (eg, 'EU'), or undef if no location constraint was set.
777  
778 =head2 err
779  
780 The S3 error code for the last error the object ran into
781  
782 =head2 errstr
783  
784 A human readable error string for the last error the object ran into
785  
786 =head2 add_tags
787  
788 # Add tags for a bucket
789 $s3->add_tags ({
790 bucket => 'bucket-name',
791 tags => { tag1 => 'value-1', tag2 => 'value-2' },
792 });
793  
794 # Add tags for an object
795 $s3->add_tags ({
796 bucket => 'bucket-name',
797 key => 'key',
798 tags => { tag1 => 'value-1', tag2 => 'value-2' },
799 });
800  
801 Takes configuration parameters
802  
803 =over
804  
805 =item key (optional, scalar)
806  
807 If key is specified, add tag(s) to object, otherwise on bucket.
808  
809 =item tags (mandatory, hashref)
810  
811 Set specified tags and their respective values.
812  
813 =item version_id (optional)
814  
815 Is specified (in conjunction with C<key>) add tag(s) to versioned object.
816  
817 =back
818  
819 Returns C<true> on success.
820  
821 Returns C<false> and sets C<err>/C<errstr> otherwise.
822  
823 =head2 delete_tags
824  
825 # Add tags for a bucket
826 $s3->delete_tags ({
827 bucket => 'bucket-name',
828 });
829  
830 # Add tags for an object
831 $s3->delete_tags ({
832 bucket => 'bucket-name',
833 key => 'key',
834 version_id => $version_id,
835 });
836  
837 Takes configuration parameters
838  
839 =over
840  
841 =item key (optional, scalar)
842  
843 If key is specified, add tag(s) to object, otherwise on bucket.
844  
845 =item version_id (optional)
846  
847 Is specified (in conjunction with C<key>) add tag(s) to versioned object.
848  
849 =back
850  
851 Returns C<true> on success.
852  
853 Returns C<false> and sets C<err>/C<errstr> otherwise.
854  
855 =head1 SEE ALSO
856  
857 L<Net::Amazon::S3>
858  
859 =head1 AUTHOR
860  
861 Branislav Zahradník <barney@cpan.org>
862  
863 =head1 COPYRIGHT AND LICENSE
864  
865 This software is copyright (c) 2021 by Amazon Digital Services, Leon Brocard, Brad Fitzpatrick, Pedro Figueiredo, Rusty Conover, Branislav Zahradník.
866  
867 This is free software; you can redistribute it and/or modify it under
868 the same terms as the Perl 5 programming language system itself.
869  
870 =cut
« Net/Amazon/S3/Client/Object.pm ↑ Summary Shared/Examples/Net/Amazon/S3.pm »