| 1 | package Net::Amazon::S3::Client; | ||||||||||
| 2 | # ABSTRACT: An easy-to-use Amazon S3 client | ||||||||||
| 3 | 99 | $Net::Amazon::S3::Client::VERSION = '0.99'; | |||||||||
| 4 | 99 | ▶ | use Moose 0.85; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 99 | ▶ | use HTTP::Status qw(status_message); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 99 | ▶ | use MooseX::StrictConstructor 0.16; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 99 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 99 | ▶ | use Net::Amazon::S3; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 99 | ▶ | use Net::Amazon::S3::Constraint::Etag; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 99 | ▶ | use Net::Amazon::S3::Error::Handler::Confess; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 99 | has 's3' => ( | |||||||||
| 14 | is => 'ro', | ||||||||||
| 15 | isa => 'Net::Amazon::S3', | ||||||||||
| 16 | required => 1, | ||||||||||
| 17 | handles => [ | ||||||||||
| 18 | 'ua', | ||||||||||
| 19 | ], | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 99 | has error_handler_class => ( | |||||||||
| 23 | is => 'ro', | ||||||||||
| 24 | lazy => 1, | ||||||||||
| 25 | default => 'Net::Amazon::S3::Error::Handler::Confess', | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | has error_handler => ( | ||||||||||
| 29 | is => 'ro', | ||||||||||
| 30 | lazy => 1, | ||||||||||
| 31 | 144 | ▶ | default => sub { $_[0]->error_handler_class->new (s3 => $_[0]->s3) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 99 | ); | |||||||||
| 33 | |||||||||||
| 34 | 99 | has bucket_class => ( | |||||||||
| 35 | is => 'ro', | ||||||||||
| 36 | init_arg => undef, | ||||||||||
| 37 | lazy => 1, | ||||||||||
| 38 | default => 'Net::Amazon::S3::Client::Bucket', | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | around BUILDARGS => sub { | ||||||||||
| 42 | 152 | ▶ | my ($orig, $class) = (shift, shift); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 43 | 152 | my $args = $class->$orig (@_); | |||||||||
| 44 | |||||||||||
| 45 | 152 | ▶ | unless (exists $args->{s3}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 2 | my $error_handler_class = delete $args->{error_handler_class}; | |||||||||
| 47 | 2 | my $error_handler = delete $args->{error_handler}; | |||||||||
| 48 | 2 | $args = { | |||||||||
| 49 | (error_handler_class => $error_handler_class) x!! defined $error_handler_class, | ||||||||||
| 50 | (error_handler => $error_handler ) x!! defined $error_handler, | ||||||||||
| 51 | s3 => Net::Amazon::S3->new ($args), | ||||||||||
| 52 | } | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 152 | $args; | |||||||||
| 56 | 99 | }; | |||||||||
| 57 | |||||||||||
| 58 | 99 | __PACKAGE__->meta->make_immutable; | |||||||||
| 59 | |||||||||||
| 60 | sub buckets { | ||||||||||
| 61 | 5 | ▶ | my $self = shift; | ||||||||
|
Subroutine
buckets: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 62 | 5 | my $s3 = $self->s3; | |||||||||
| 63 | |||||||||||
| 64 | 5 | my $response = $self->_perform_operation ( | |||||||||
| 65 | 'Net::Amazon::S3::Operation::Buckets::List', | ||||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | 2 | ▶ | return unless $response->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | |||||||||||
| 70 | 2 | my $owner_id = $response->owner_id; | |||||||||
| 71 | 2 | my $owner_display_name = $response->owner_displayname; | |||||||||
| 72 | |||||||||||
| 73 | 2 | my @buckets; | |||||||||
| 74 | 2 | foreach my $bucket ($response->buckets) { | |||||||||
| 75 | push @buckets, $self->bucket_class->new ( | ||||||||||
| 76 | client => $self, | ||||||||||
| 77 | name => $bucket->{name}, | ||||||||||
| 78 | creation_date => $bucket->{creation_date}, | ||||||||||
| 79 | 4 | owner_id => $owner_id, | |||||||||
| 80 | owner_display_name => $owner_display_name, | ||||||||||
| 81 | ); | ||||||||||
| 82 | |||||||||||
| 83 | } | ||||||||||
| 84 | 2 | return @buckets; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | sub create_bucket { | ||||||||||
| 88 | 17 | ▶ | my ( $self, %conf ) = @_; | ||||||||
|
Subroutine
create_bucket: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 89 | |||||||||||
| 90 | my $bucket = $self->bucket_class->new( | ||||||||||
| 91 | client => $self, | ||||||||||
| 92 | name => $conf{name}, | ||||||||||
| 93 | 17 | ); | |||||||||
| 94 | 17 | $bucket->_create(%conf); | |||||||||
| 95 | 6 | return $bucket; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | sub bucket { | ||||||||||
| 99 | 126 | ▶ | my ( $self, %conf ) = @_; | ||||||||
|
Subroutine
bucket: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 100 | 126 | return $self->bucket_class->new( | |||||||||
| 101 | client => $self, | ||||||||||
| 102 | %conf, | ||||||||||
| 103 | ); | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | sub _perform_operation { | ||||||||||
| 107 | 144 | ▶ | my ($self, $operation, %params) = @_; | ||||||||
|
Subroutine
_perform_operation: called CC=1 SCAR=0.0
| |||||||||||
| 108 | |||||||||||
| 109 | 144 | return $self->s3->_perform_operation ( | |||||||||
| 110 | $operation, | ||||||||||
| 111 | error_handler => $self->error_handler, | ||||||||||
| 112 | %params | ||||||||||
| 113 | ); | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 99 | 1; | |||||||||
| 117 | |||||||||||
| 118 | __END__ | ||||||||||
| 119 | |||||||||||
| 120 | =pod | ||||||||||
| 121 | |||||||||||
| 122 | =encoding UTF-8 | ||||||||||
| 123 | |||||||||||
| 124 | =head1 NAME | ||||||||||
| 125 | |||||||||||
| 126 | Net::Amazon::S3::Client - An easy-to-use Amazon S3 client | ||||||||||
| 127 | |||||||||||
| 128 | =head1 VERSION | ||||||||||
| 129 | |||||||||||
| 130 | version 0.99 | ||||||||||
| 131 | |||||||||||
| 132 | =head1 SYNOPSIS | ||||||||||
| 133 | |||||||||||
| 134 | # Build Client instance | ||||||||||
| 135 | my $client = Net::Amazon::S3::Client->new ( | ||||||||||
| 136 | # accepts all Net::Amazon::S3's arguments | ||||||||||
| 137 | aws_access_key_id => $aws_access_key_id, | ||||||||||
| 138 | aws_secret_access_key => $aws_secret_access_key, | ||||||||||
| 139 | retry => 1, | ||||||||||
| 140 | ); | ||||||||||
| 141 | |||||||||||
| 142 | # or reuse an existing S3 connection | ||||||||||
| 143 | my $client = Net::Amazon::S3::Client->new (s3 => $s3); | ||||||||||
| 144 | |||||||||||
| 145 | # list all my buckets | ||||||||||
| 146 | # returns a list of L<Net::Amazon::S3::Client::Bucket> objects | ||||||||||
| 147 | my @buckets = $client->buckets; | ||||||||||
| 148 | foreach my $bucket (@buckets) { | ||||||||||
| 149 | print $bucket->name . "\n"; | ||||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | # create a new bucket | ||||||||||
| 153 | # returns a L<Net::Amazon::S3::Client::Bucket> object | ||||||||||
| 154 | my $bucket = $client->create_bucket( | ||||||||||
| 155 | name => $bucket_name, | ||||||||||
| 156 | acl_short => 'private', | ||||||||||
| 157 | location_constraint => 'us-east-1', | ||||||||||
| 158 | ); | ||||||||||
| 159 | |||||||||||
| 160 | # or use an existing bucket | ||||||||||
| 161 | # returns a L<Net::Amazon::S3::Client::Bucket> object | ||||||||||
| 162 | my $bucket = $client->bucket( name => $bucket_name ); | ||||||||||
| 163 | |||||||||||
| 164 | =head1 DESCRIPTION | ||||||||||
| 165 | |||||||||||
| 166 | The L<Net::Amazon::S3> module was written when the Amazon S3 service | ||||||||||
| 167 | had just come out and it is a light wrapper around the APIs. Some | ||||||||||
| 168 | bad API decisions were also made. The | ||||||||||
| 169 | L<Net::Amazon::S3::Client>, L<Net::Amazon::S3::Client::Bucket> and | ||||||||||
| 170 | L<Net::Amazon::S3::Client::Object> classes are designed after years | ||||||||||
| 171 | of usage to be easy to use for common tasks. | ||||||||||
| 172 | |||||||||||
| 173 | These classes throw an exception when a fatal error occurs. It | ||||||||||
| 174 | also is very careful to pass an MD5 of the content when uploaded | ||||||||||
| 175 | to S3 and check the resultant ETag. | ||||||||||
| 176 | |||||||||||
| 177 | WARNING: This is an early release of the Client classes, the APIs | ||||||||||
| 178 | may change. | ||||||||||
| 179 | |||||||||||
| 180 | =for test_synopsis no strict 'vars' | ||||||||||
| 181 | |||||||||||
| 182 | =head1 CONSTRUCTOR | ||||||||||
| 183 | |||||||||||
| 184 | =over | ||||||||||
| 185 | |||||||||||
| 186 | =item s3 | ||||||||||
| 187 | |||||||||||
| 188 | L<< Net::Amazon::S3 >> instance | ||||||||||
| 189 | |||||||||||
| 190 | =item error_handler_class | ||||||||||
| 191 | |||||||||||
| 192 | Error handler class name (package name), see L<< Net::Amazon::S3::Error::Handler >> | ||||||||||
| 193 | for more. Overrides one available in C<s3>. | ||||||||||
| 194 | |||||||||||
| 195 | Default: L<< Net::Amazon::S3::Error::Handler::Confess >> | ||||||||||
| 196 | |||||||||||
| 197 | =item error_handler | ||||||||||
| 198 | |||||||||||
| 199 | Instance of error handler class. | ||||||||||
| 200 | |||||||||||
| 201 | =back | ||||||||||
| 202 | |||||||||||
| 203 | =head1 METHODS | ||||||||||
| 204 | |||||||||||
| 205 | =head2 new | ||||||||||
| 206 | |||||||||||
| 207 | L<Net::Amazon::S3::Client> can be constructed two ways. | ||||||||||
| 208 | |||||||||||
| 209 | Historically it wraps S3 API instance | ||||||||||
| 210 | |||||||||||
| 211 | use Net::Amazon::S3::Client; | ||||||||||
| 212 | |||||||||||
| 213 | my $client = Net::Amazon::S3::Client->new ( | ||||||||||
| 214 | s3 => .... # Net::Amazon::S3 instance | ||||||||||
| 215 | ); | ||||||||||
| 216 | |||||||||||
| 217 | =head2 new (since v0.92) | ||||||||||
| 218 | |||||||||||
| 219 | Since v0.92 explicit creation of S3 API instance is no longer necessary. | ||||||||||
| 220 | L<Net::Amazon::S3::Client>'s constructor accepts same parameters as L<Net::Amazon::S3> | ||||||||||
| 221 | |||||||||||
| 222 | use Net::Amazon::S3::Client v0.92; | ||||||||||
| 223 | |||||||||||
| 224 | my $client = Net::Amazon::S3::Client->new ( | ||||||||||
| 225 | aws_access_key_id => ..., | ||||||||||
| 226 | aws_secret_access_key => ..., | ||||||||||
| 227 | ..., | ||||||||||
| 228 | ); | ||||||||||
| 229 | |||||||||||
| 230 | =head2 buckets | ||||||||||
| 231 | |||||||||||
| 232 | # list all my buckets | ||||||||||
| 233 | # returns a list of L<Net::Amazon::S3::Client::Bucket> objects | ||||||||||
| 234 | my @buckets = $client->buckets; | ||||||||||
| 235 | foreach my $bucket (@buckets) { | ||||||||||
| 236 | print $bucket->name . "\n"; | ||||||||||
| 237 | } | ||||||||||
| 238 | |||||||||||
| 239 | =head2 create_bucket | ||||||||||
| 240 | |||||||||||
| 241 | # create a new bucket | ||||||||||
| 242 | # returns a L<Net::Amazon::S3::Client::Bucket> object | ||||||||||
| 243 | my $bucket = $client->create_bucket( | ||||||||||
| 244 | name => $bucket_name, | ||||||||||
| 245 | acl_short => 'private', | ||||||||||
| 246 | location_constraint => 'us-east-1', | ||||||||||
| 247 | ); | ||||||||||
| 248 | |||||||||||
| 249 | =head2 bucket | ||||||||||
| 250 | |||||||||||
| 251 | # or use an existing bucket | ||||||||||
| 252 | # returns a L<Net::Amazon::S3::Client::Bucket> object | ||||||||||
| 253 | my $bucket = $client->bucket( name => $bucket_name ); | ||||||||||
| 254 | |||||||||||
| 255 | =head2 bucket_class | ||||||||||
| 256 | |||||||||||
| 257 | # returns string "Net::Amazon::S3::Client::Bucket" | ||||||||||
| 258 | # subclasses will want to override this. | ||||||||||
| 259 | my $bucket_class = $client->bucket_class | ||||||||||
| 260 | |||||||||||
| 261 | =head1 AUTHOR | ||||||||||
| 262 | |||||||||||
| 263 | Branislav Zahradník <barney@cpan.org> | ||||||||||
| 264 | |||||||||||
| 265 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 266 | |||||||||||
| 267 | This software is copyright (c) 2021 by Amazon Digital Services, Leon Brocard, Brad Fitzpatrick, Pedro Figueiredo, Rusty Conover, Branislav Zahradník. | ||||||||||
| 268 | |||||||||||
| 269 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 270 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 271 | |||||||||||
| 272 | =cut | ||||||||||