S3.pm

filter: Statementstmt 95.1% 78 / 82 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.2% 20 / 21 Podpod 80.0% 4 / 5 totaltotal 92.3% 109 / 118 CC 7 SCAR 6.8
CC
7
Cov
92%
__ANON__
6.0
add_bucket
4.0
buckets
2.0
CRAP 2.0
« S3/Request/CreateBucket.pm ↑ Summary S3/HTTPRequest.pm »
1  
2 package AWS::S3;
3  
4 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use LWP::UserAgent::Determined;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Class::Load 'load_class';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Log::Any qw( $LOG );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use AWS::S3::ResponseParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use AWS::S3::Owner;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use AWS::S3::Bucket;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 our $VERSION = '3.00';
18  
19 10 has [qw/access_key_id secret_access_key/] => ( is => 'ro', isa => 'Str' );
20  
21 has 'session_token' => (
22 is => 'ro',
23 isa => 'Maybe[Str]',
24 lazy => 1,
25 10 ▶ default => sub { $ENV{AWS_SESSION_TOKEN} },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 10 );
27  
28 has 'region' => (
29 is => 'ro',
30 isa => 'Maybe[Str]',
31 lazy => 1,
32 11 ▶ default => sub { $ENV{AWS_REGION} },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 10 );
34  
35 # use https unless told otherwise, over http the request can be read and
36 # modified on the network, including the session token and the contents
37 10 has 'secure' => (
38 is => 'ro',
39 isa => 'Bool',
40 lazy => 1,
41 default => 1
42 );
43  
44 has 'endpoint' => (
45 is => 'ro',
46 isa => 'Str',
47 lazy => 1,
48 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
49 0 my ( $s ) = @_;
50  
51 0 ▶ if ( my $region = $s->region ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $region = $s->region) { } 0 0 0
52 0 return "s3.$region.amazonaws.com"
53 } else {
54 0 return "s3.amazonaws.com"
55 }
56 },
57 10 );
58  
59 has 'ua' => (
60 is => 'ro',
61 isa => 'LWP::UserAgent',
62 6 ▶ default => sub { LWP::UserAgent::Determined->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
63 10 );
64  
65 has 'honor_leading_slashes' => (
66 is => 'ro',
67 isa => 'Bool',
68 57 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 10 );
70  
71 477 ▶sub request {
Subroutine request: called CC=1 SCAR=0.0
Pod undocumented
72 477 my ( $s, $type, %args ) = @_;
73  
74 477 my $class = "AWS::S3::Request::$type";
75  
76 477 load_class( $class );
77 477 return $class->new( %args, s3 => $s, type => $type );
78 } # end request()
79  
80 2 ▶sub owner {
Subroutine owner: called CC=1 SCAR=0.0
Pod documented
81 2 my $s = shift;
82  
83 2 my $type = 'ListAllMyBuckets';
84 2 my $request = $s->request( $type );
85 2 my $response = $request->request();
86  
87 # Linode/Akamai E3 endpoints do not include the `xmlns` in
88 # ListAllMyBuckets, so use the localname to work with or
89 # without a declared XML namespace.
90 2 my $xml = $response->xml;
91 2 my ($node) = $xml->getElementsByLocalName('Owner');
92 2 return AWS::S3::Owner->new(
93 id => $node->getElementsByLocalName('ID')->string_value,
94 display_name => $node->getElementsByLocalName('DisplayName')->string_value,
95 );
96 } # end owner()
97  
98 7 ▶sub buckets {
Subroutine buckets: called CC=2 SCAR=6.9
Pod documented
99 7 my ( $s ) = @_;
100  
101 7 my $type = 'ListAllMyBuckets';
102 7 my $request = $s->request( $type );
103 7 my $response = $request->request();
104  
105 # Linode/Akamai E3 endpoints do not include the `xmlns` in
106 # ListAllMyBuckets, so use the localname to work with or
107 # without a declared XML namespace.
108 7 my $xml = $response->xml;
109 7 my @buckets = ();
110 7 foreach my $node ( $xml->getElementsByLocalName( 'Bucket' ) ) {
111 14 push @buckets,
112 AWS::S3::Bucket->new(
113 name => $node->getElementsByLocalName('Name')->string_value,
114 creation_date => $node->getElementsByLocalName('CreationDate')->string_value,
115 s3 => $s,
116 );
117 } # end foreach()
118  
119 7 $LOG->debug('Listed AWS buckets', { buckets => [map $_->name, @buckets] });
120 7 return @buckets;
121 } # end buckets()
122  
123 5 ▶sub bucket {
Subroutine bucket: called CC=2 SCAR=6.9
Pod documented
124 5 my ( $s, $name ) = @_;
125  
126 5 ▶ my ( $bucket ) = grep { $_->name eq $name } $s->buckets
Branch: 1 branch
BranchTrueFalseTotal
my($bucket) = grep({$_->name eq $name;} $s->buckets) or return 3 2 5
127 or return;
128 3 $bucket;
129 } # end bucket()
130  
131 4 ▶sub add_bucket {
Subroutine add_bucket: called CC=4 SCAR=13.9
Pod documented
132 4 my ( $s, %args ) = @_;
133  
134 4 my $type = 'CreateBucket';
135 my $request = $s->request(
136 $type,
137 bucket => $args{name},
138 (
139 $args{location} ? ( location => $args{location} )
140 4 ▶ : $s->region ? ( location => $s->region )
Branch: 2 branches
BranchTrueFalseTotal
$args{'location'} ? : 2 2 4
$s->region ? : 0 2 2
141 : ()
142 ),
143 );
144 3 my $response = $request->request();
145  
146 3 ▶ if ( my $msg = $response->friendly_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $response->friendly_error) 2 1 3
147 2 die $msg;
148 } # end if()
149  
150 1 return $s->bucket( $args{name} );
151 } # end add_bucket()
152  
153 10 __PACKAGE__->meta->make_immutable;
154  
155 __END__
« S3/Request/CreateBucket.pm ↑ Summary S3/HTTPRequest.pm »