S3/Bucket.pm

filter: Statementstmt 53.7% 58 / 108 Branchbran 26.4% 9 / 34 Conditioncond 40.0% 6 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 72.2% 13 / 18 Podpod 50.0% 3 / 6 totaltotal 47.0% 89 / 189 CC 25 SCAR 23.2
CC
25
Cov
46%
__ANON__
30.0
file
15.3
__ANON__
12.0
CRAP 10.1
« S3/Signer/V4.pm ↑ Summary S3/Roles/Request.pm »
1  
2 package AWS::S3::Bucket;
3  
4 10 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use AWS::S3::ResponseParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use AWS::S3::FileIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 has 's3' => (
11 is => 'ro',
12 isa => 'AWS::S3',
13 required => 1,
14 );
15  
16 10 has 'name' => (
17 is => 'ro',
18 isa => 'Str',
19 required => 1,
20 );
21  
22 10 has 'creation_date' => (
23 is => 'ro',
24 isa => 'Str',
25 required => 0,
26 );
27  
28 10 ▶use MooseX::Types -declare => [qw/ACLShorts/];
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 has 'acl' => (
31 is => 'rw',
32 isa => 'Str',
33 required => 0,
34 lazy => 1,
35 clearer => '_clear_acl',
36 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 my $self = shift;
38 0 my $type = 'GetBucketAccessControl';
39 0 return $self->_get_property( $type )->response->decoded_content();
40 },
41 0 ▶ trigger => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
42 0 my ( $self, $new_val, $old_val ) = @_;
43  
44 0 my %shorts = map { $_ => undef } qw(
45 private public-read public-read-write authenticated-read
46 );
47  
48 0 my %acl = ();
49 0 ▶ if ( $new_val =~ m{<} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_val =~ /</) { } 0 0 0
elsif (exists $shorts{$new_val}) { } 0 0 0
50 0 $acl{acl_xml} = $new_val;
51 }
52 elsif ( exists $shorts{$new_val} ) {
53 0 $acl{acl_short} = $new_val;
54 }
55 else {
56 0 die "Attempt to set an invalid value for acl: '$new_val'";
57 }
58  
59 0 my $type = 'SetBucketAccessControl';
60 0 my $req = $self->s3->request( $type, %acl, bucket => $self->name, );
61 0 my $response = $req->request();
62  
63 0 ▶ return if $response->response->code == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $response->response->code == 404 0 0 0
64  
65 0 ▶ if ( my $msg = $response->friendly_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $response->friendly_error) 0 0 0
66 0 die $msg;
67 } # end if()
68  
69 0 $self->_clear_acl;
70 }
71 10 );
72  
73 has 'location_constraint' => (
74 is => 'ro',
75 isa => 'Str',
76 required => 0,
77 lazy => 1,
78 0 ▶ default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
79 0 my $self = shift;
80  
81 0 my $type = 'GetBucketLocationConstraint';
82 0 my $response = $self->_get_property( $type );
83  
84 0 my $constraint = $response->xpc->findvalue( '//s3:LocationConstraint' );
85 0 ▶ if ( defined $constraint && $constraint eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $constraint and $constraint eq '') { } 0 0 0
Condition0%0/3
defined $constraint and $constraint eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $constraint and $constraint eq ""
defined $constraint $constraint eq ""
Truth table0%0/3
A and B
A= defined $constraintB= $constraint eq ""
ABresult
0X0
100
111
86 0 return;
87 } else {
88 0 return $constraint;
89 }
90 }
91 10 );
92  
93 has 'policy' => (
94 is => 'rw',
95 isa => 'Str',
96 required => 0,
97 lazy => 1,
98 clearer => '_clear_policy',
99 0 ▶ default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
100 0 my $self = shift;
101  
102 0 my $type = 'GetBucketPolicy';
103 0 my $req = $self->s3->request( $type, bucket => $self->name, );
104 0 my $response = $req->request();
105  
106 0 eval { $response->_parse_errors };
107 0 ▶ if ( my $msg = $response->friendly_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $response->friendly_error) 0 0 0
108 0 ▶ if ( $response->error_code eq 'NoSuchBucketPolicy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->error_code eq 'NoSuchBucketPolicy') { } 0 0 0
109 0 return '';
110 } else {
111 0 die $msg;
112 } # end if()
113 } # end if()
114  
115 0 return $response->response->decoded_content();
116 },
117 0 ▶ trigger => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
118 0 my ( $self, $policy ) = @_;
119  
120 0 my $type = 'SetBucketPolicy';
121 0 my $req = $self->s3->request(
122 $type,
123 bucket => $self->name,
124 policy => $policy,
125 );
126 0 my $response = $req->request();
127  
128 #warn "NewPolicy:($policy).......\n";
129 #warn $response->response->as_string;
130 0 ▶ if ( my $msg = $response->friendly_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $response->friendly_error) 0 0 0
131 0 die $msg;
132 } # end if()
133  
134 0 $self->_clear_policy;
135  
136 }
137 10 );
138  
139 # XXX: Not tested.
140 1 ▶sub enable_cloudfront_distribution {
Subroutine enable_cloudfront_distribution: called CC=2 SCAR=10.9
Pod undocumented
141 1 my ( $s, $cloudfront_dist ) = @_;
142  
143 1 ▶ $cloudfront_dist->isa( 'AWS::CloudFront::Distribution' )
Branch: 1 branch
BranchTrueFalseTotal
$cloudfront_dist->isa("AWS::CloudFront::Distribution") or die "Usage: enable_cloudfront_distribution( <AWS::CloudFront::Distribution object> )" 0 1 1
144 or die "Usage: enable_cloudfront_distribution( <AWS::CloudFront::Distribution object> )";
145  
146 0 my $ident = $cloudfront_dist->cf->create_origin_access_identity( Comment => "Access to s3://" . $s->name, );
147 0 $s->policy( <<"JSON");
148 {
149 "Version":"2008-10-17",
150 "Id":"PolicyForCloudFrontPrivateContent",
151 "Statement":[{
152 "Sid": "Grant a CloudFront Origin Identity access to support private content",
153 "Effect":"Allow",
154 "Principal": {
155 0 "CanonicalUser":"@{[ $ident->S3CanonicalUserId ]}"
156 },
157 "Action": "s3:GetObject",
158 0 "Resource": "arn:aws:s3:::@{[ $s->name ]}/*"
159 }
160 ]
161 }
162 JSON
163 } # end enable_cloudfront_distribution()
164  
165 1 ▶sub files {
Subroutine files: called CC=1 SCAR=0.0
Pod documented
166 1 my ( $s, %args ) = @_;
167  
168 1 return AWS::S3::FileIterator->new( %args, bucket => $s, );
169 } # end files()
170  
171 1 ▶sub file {
Subroutine file: called CC=10 SCAR=27.3
Pod documented
172 1 my ( $s, $key ) = @_;
173  
174 1 my $type = 'GetFileInfo';
175  
176 1 ▶ my $parser = $s->_get_property( $type, key => $key )
Branch: 1 branch
BranchTrueFalseTotal
my $parser = $s->_get_property($type, "key", $key) or return 1 0 1
177 or return;
178  
179 1 my $res = $parser->response;
180 1 ▶ confess "Cannot get file: ", $res->as_string, " " unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 1 1
181 1 ▶ return AWS::S3::File->new(
Branch: 1 branch
BranchTrueFalseTotal
($res->header('x-amz-server-side-encryption') || '') eq 'AES256' ? : 0 1 1
Condition50%6/12
$key || undef
l!l
10
$res->header('content-length') || 0
l!l
10
$res->header('content-type') || 'application/octet-stream'
l!l
10
$res->header('etag') || undef
l!l
10
$res->header('last-modified') || undef
l!l
10
$res->header('x-amz-server-side-encryption') || ''
l!l
10
MC/DC0%0/1
$key || undef
$key
MC/DC0%0/1
$res->header('content-length') || 0
$res->header('content-length')
MC/DC0%0/1
$res->header('content-type') || 'application/octet-stream'
$res->header('content-type')
MC/DC0%0/1
$res->header('etag') || undef
$res->header('etag')
MC/DC0%0/1
$res->header('last-modified') || undef
$res->header('last-modified')
MC/DC0%0/1
$res->header('x-amz-server-side-encryption') || ''
$res->header('x-amz-server-side-encryption')
Truth table50%1/2
A
A= $key
Aresult
11
00
Truth table50%1/2
A
A= $res->header('content-length')
Aresult
11
00
Truth table50%1/2
A
A= $res->header('content-type')
Aresult
11
00
Truth table50%1/2
A
A= $res->header('etag')
Aresult
11
00
Truth table50%1/2
A
A= $res->header('last-modified')
Aresult
11
00
Truth table50%1/2
A
A= $res->header('x-amz-server-side-encryption')
Aresult
11
00
182 bucket => $s,
183 key => $key || undef,
184 size => $res->header( 'content-length' ) || 0,
185 contenttype => $res->header( 'content-type' ) || 'application/octet-stream',
186 etag => $res->header( 'etag' ) || undef,
187 lastmodified => $res->header( 'last-modified' ) || undef,
188 is_encrypted => ( $res->header( 'x-amz-server-side-encryption' ) || '' ) eq 'AES256' ? 1 : 0,
189 );
190 } # end file()
191  
192 10 ▶sub add_file {
Subroutine add_file: called CC=1 SCAR=0.0
Pod undocumented
193 10 my ( $s, %args ) = @_;
194  
195 10 my $file = AWS::S3::File->new(
196 %args,
197 bucket => $s
198 );
199 10 $file->contents( $args{contents} );
200 10 return $file;
201 } # end add_file()
202  
203 1 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.1
Pod undocumented
204 1 my ( $s ) = @_;
205  
206 1 my $type = 'DeleteBucket';
207  
208 1 my $req = $s->s3->request( $type, bucket => $s->name, );
209 1 my $response = $req->request();
210  
211 1 ▶ if ( my $msg = $response->friendly_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $response->friendly_error) 0 1 1
212 0 die $msg;
213 } # end if()
214  
215 1 return 1;
216 } # end delete()
217  
218 # Working as of v0.023
219 1 ▶sub delete_multi {
Subroutine delete_multi: called CC=3 SCAR=11.4
Pod documented
220 1 my ( $s, @keys ) = @_;
221  
222 1 ▶ die "You can only delete up to 1000 keys at once"
Branch: 1 branch
BranchTrueFalseTotal
if @keys > 1000 0 1 1
223 if @keys > 1000;
224 1 my $type = 'DeleteMulti';
225  
226 1 my $req = $s->s3->request(
227 $type,
228 bucket => $s->name,
229 keys => \@keys,
230 );
231 1 my $response = $req->request();
232  
233 1 ▶ if ( my $msg = $response->friendly_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $response->friendly_error) 0 1 1
234 0 die $msg;
235 } # end if()
236  
237 1 return 1;
238 } # end delete_multi()
239  
240 1 ▶sub _get_property {
Subroutine _get_property: called CC=3 SCAR=11.6
241 1 my ( $s, $type, %args ) = @_;
242  
243 1 my $req = $s->s3->request( $type, bucket => $s->name, %args );
244 1 my $response = $req->request();
245  
246 1 ▶ return if $response->response->code == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $response->response->code == 404 0 1 1
247  
248 1 ▶ if ( my $msg = $response->friendly_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $response->friendly_error) 0 1 1
249 0 die $msg;
250 } # end if()
251  
252 1 return $response;
253 } # end _get_property()
254  
255 10 __PACKAGE__->meta->make_immutable;
256  
257 __END__
« S3/Signer/V4.pm ↑ Summary S3/Roles/Request.pm »