| 1 | package Net::AmazonS3::Simple; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '0.1.3'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use AWS::Signature4; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use Net::AmazonS3::Simple::HTTP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Net::AmazonS3::Simple::Object::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Net::AmazonS3::Simple::Object::Memory; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | use Class::Tiny qw( | ||||||||||
| 16 | aws_access_key_id | ||||||||||
| 17 | aws_secret_access_key | ||||||||||
| 18 | ), { | ||||||||||
| 19 | region => 'us-west-1', | ||||||||||
| 20 | validate => 1, | ||||||||||
| 21 | auto_region => 1, | ||||||||||
| 22 | secure => 1, | ||||||||||
| 23 | host => 's3.amazonaws.com', | ||||||||||
| 24 | signer => sub { | ||||||||||
| 25 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 1 | return AWS::Signature4->new( | |||||||||
| 28 | -access_key => $self->aws_access_key_id, | ||||||||||
| 29 | -secret_key => $self->aws_secret_access_key, | ||||||||||
| 30 | ); | ||||||||||
| 31 | }, | ||||||||||
| 32 | http_client => sub { | ||||||||||
| 33 | 1 | ▶ | return LWP::UserAgent->new(); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 34 | }, | ||||||||||
| 35 | requestator => sub { | ||||||||||
| 36 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | 1 | return Net::AmazonS3::Simple::HTTP->new( | |||||||||
| 39 | http_client => $self->http_client, | ||||||||||
| 40 | signer => $self->signer, | ||||||||||
| 41 | auto_region => $self->auto_region, | ||||||||||
| 42 | region => $self->region, | ||||||||||
| 43 | secure => $self->secure, | ||||||||||
| 44 | host => $self->host, | ||||||||||
| 45 | ); | ||||||||||
| 46 | } | ||||||||||
| 47 | 1 | ▶ | }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | |||||||||||
| 49 | =head1 NAME | ||||||||||
| 50 | |||||||||||
| 51 | Net::AmazonS3::Simple - simple S3 client support signature v4 | ||||||||||
| 52 | |||||||||||
| 53 | =head1 SYNOPSIS | ||||||||||
| 54 | |||||||||||
| 55 | my $s3 = Net::AmazonS3::Simple->new( | ||||||||||
| 56 | aws_access_key_id => 'XXX', | ||||||||||
| 57 | aws_secret_access_key => 'YYY', | ||||||||||
| 58 | ); | ||||||||||
| 59 | |||||||||||
| 60 | $s3->get_object($bucket, $key); | ||||||||||
| 61 | |||||||||||
| 62 | #or for big file is better | ||||||||||
| 63 | |||||||||||
| 64 | $s3->save_object_to_file($bucket, $key, $file); | ||||||||||
| 65 | |||||||||||
| 66 | =head1 DESCRIPTION | ||||||||||
| 67 | |||||||||||
| 68 | This S3 client have really simple interface and support only get object (yet). | ||||||||||
| 69 | |||||||||||
| 70 | This S3 client use L<AWS::Signature4>. Signature v4 is L<needed|http://stackoverflow.com/questions/26533245/the-authorization-mechanism-you-have-provided-is-not-supported-please-use-aws4> for EU AWS region (for other regions is optionable). | ||||||||||
| 71 | If you need other region, I recommend some other S3 client (L<SEE_ALSO|/SEE_ALSO>). | ||||||||||
| 72 | |||||||||||
| 73 | =head1 METHODS | ||||||||||
| 74 | |||||||||||
| 75 | =head2 new(%attributes) | ||||||||||
| 76 | |||||||||||
| 77 | =head3 %attributes | ||||||||||
| 78 | |||||||||||
| 79 | =head4 aws_access_key_id | ||||||||||
| 80 | |||||||||||
| 81 | =head4 aws_secret_access_key | ||||||||||
| 82 | |||||||||||
| 83 | =head4 region | ||||||||||
| 84 | |||||||||||
| 85 | default I<us-west-1> | ||||||||||
| 86 | |||||||||||
| 87 | =head4 auto_region | ||||||||||
| 88 | |||||||||||
| 89 | is is set I<wrong> C<region>, is automaticaly changed to I<expecting> region | ||||||||||
| 90 | |||||||||||
| 91 | default I<1> | ||||||||||
| 92 | |||||||||||
| 93 | =head4 validate | ||||||||||
| 94 | |||||||||||
| 95 | object after get is validate (recalculate MD5 checksum) | ||||||||||
| 96 | |||||||||||
| 97 | default I<1> | ||||||||||
| 98 | |||||||||||
| 99 | =head4 secure | ||||||||||
| 100 | |||||||||||
| 101 | is is set, then use I<https> protocol | ||||||||||
| 102 | |||||||||||
| 103 | default I<1> | ||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | =head4 host | ||||||||||
| 107 | |||||||||||
| 108 | default I<s3.amazonaws.com> | ||||||||||
| 109 | |||||||||||
| 110 | =cut | ||||||||||
| 111 | |||||||||||
| 112 | sub BUILD { | ||||||||||
| 113 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||
| 114 | |||||||||||
| 115 | 1 | foreach my $req (qw/aws_access_key_id aws_secret_access_key/) { | |||||||||
| 116 | 2 | ▶ | die "$req attribute required" unless defined $self->$req; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | =head2 get_object($bucket, $key) | ||||||||||
| 121 | |||||||||||
| 122 | C<$bucket> - bucket name | ||||||||||
| 123 | |||||||||||
| 124 | C<$key> - object key | ||||||||||
| 125 | |||||||||||
| 126 | return L<Net::AmazonS3::Simple::Object::Memory> | ||||||||||
| 127 | |||||||||||
| 128 | =cut | ||||||||||
| 129 | |||||||||||
| 130 | sub get_object { | ||||||||||
| 131 | 1 | ▶ | my ($self, $bucket, $key) = @_; | ||||||||
|
Subroutine
get_object: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 132 | |||||||||||
| 133 | 1 | my $response = $self->requestator->request( | |||||||||
| 134 | bucket => $bucket, | ||||||||||
| 135 | path => $key, | ||||||||||
| 136 | ); | ||||||||||
| 137 | |||||||||||
| 138 | 1 | return Net::AmazonS3::Simple::Object::Memory->create_from_response( | |||||||||
| 139 | validate => $self->validate, | ||||||||||
| 140 | response => $response | ||||||||||
| 141 | ); | ||||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | =head2 save_object_to_file($bucket, $key, $file) | ||||||||||
| 145 | |||||||||||
| 146 | C<$bucket> - bucket name | ||||||||||
| 147 | |||||||||||
| 148 | C<$key> - object key | ||||||||||
| 149 | |||||||||||
| 150 | C<$file> - file to save, optional, default is C<tempfile> | ||||||||||
| 151 | |||||||||||
| 152 | |||||||||||
| 153 | return L<Net::AmazonS3::Simple::Object::File> | ||||||||||
| 154 | |||||||||||
| 155 | =cut | ||||||||||
| 156 | |||||||||||
| 157 | sub save_object_to_file { | ||||||||||
| 158 | 1 | ▶ | my ($self, $bucket, $key, $file) = @_; | ||||||||
|
Subroutine
save_object_to_file: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 159 | |||||||||||
| 160 | 1 | ▶ | $file = Path::Tiny->tempfile() if !defined $file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | |||||||||||
| 162 | 1 | my $response = $self->requestator->request( | |||||||||
| 163 | bucket => $bucket, | ||||||||||
| 164 | path => $key, | ||||||||||
| 165 | content_to_file => $file, | ||||||||||
| 166 | ); | ||||||||||
| 167 | |||||||||||
| 168 | 1 | return Net::AmazonS3::Simple::Object::File->create_from_response( | |||||||||
| 169 | validate => $self->validate, | ||||||||||
| 170 | response => $response, | ||||||||||
| 171 | file_path => path($file), | ||||||||||
| 172 | ); | ||||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | =head1 SEE_ALSO | ||||||||||
| 176 | |||||||||||
| 177 | L<Paws::S3> - support version 4 signature too, | ||||||||||
| 178 | L<Paws> support more AWS services, | ||||||||||
| 179 | some dependency of this module don't work on windows | ||||||||||
| 180 | |||||||||||
| 181 | L<Net::Amazon::S3> - don't support version 4 signature, | ||||||||||
| 182 | some dependency of this module don't work on windows | ||||||||||
| 183 | |||||||||||
| 184 | L<AWS::S3> - don't support version 4 signature, | ||||||||||
| 185 | object is get to memory only (no direct to file - it's not good for downloading big files), | ||||||||||
| 186 | similar interface like L<Net::Amazon::S3> | ||||||||||
| 187 | |||||||||||
| 188 | L<Amazon::S3> - don't support version 4 signature, | ||||||||||
| 189 | similar interface like L<Net::Amazon::S3>, | ||||||||||
| 190 | last update Aug 15, 2009 | ||||||||||
| 191 | |||||||||||
| 192 | L<Amazon::S3::Thin> - don't support version 4 signature, | ||||||||||
| 193 | simple interface | ||||||||||
| 194 | |||||||||||
| 195 | L<Furl::S3> - don't support version 4 signature, | ||||||||||
| 196 | simple interface (similar like L<Amazon::S3::Thin>), | ||||||||||
| 197 | last update May 16, 2012 | ||||||||||
| 198 | |||||||||||
| 199 | =head1 LICENSE | ||||||||||
| 200 | |||||||||||
| 201 | Copyright (C) Avast Software. | ||||||||||
| 202 | |||||||||||
| 203 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 204 | it under the same terms as Perl itself. | ||||||||||
| 205 | |||||||||||
| 206 | =head1 AUTHOR | ||||||||||
| 207 | |||||||||||
| 208 | Jan Seidl E<lt>seidl@avast.comE<gt> | ||||||||||
| 209 | |||||||||||
| 210 | =cut | ||||||||||
| 211 | |||||||||||
| 212 | 1 | 1; | |||||||||