File Coverage

blib/lib/Net/AmazonS3/Simple.pm
Criterion Covered Total %
statement 42 42 100.0
branch 2 4 50.0
condition n/a
subroutine 12 12 100.0
pod 2 3 66.6
total 58 61 95.0


line stmt bran cond sub pod time code
1             package Net::AmazonS3::Simple;
2 1     1   863 use strict;
  1         1  
  1         27  
3 1     1   4 use warnings;
  1         1  
  1         43  
4              
5             our $VERSION = '0.1.3';
6              
7 1     1   430 use AWS::Signature4;
  1         21509  
  1         28  
8 1     1   566 use LWP::UserAgent;
  1         22718  
  1         27  
9 1     1   700 use Path::Tiny;
  1         7798  
  1         58  
10              
11 1     1   441 use Net::AmazonS3::Simple::HTTP;
  1         2  
  1         21  
12 1     1   4 use Net::AmazonS3::Simple::Object::File;
  1         1  
  1         19  
13 1     1   4 use Net::AmazonS3::Simple::Object::Memory;
  1         1  
  1         113  
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         1946 my ($self) = @_;
26              
27 1         16 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         9 return LWP::UserAgent->new();
34             },
35             requestator => sub {
36 1         6 my ($self) = @_;
37              
38 1         13 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     1   3 };
  1         2  
  1         10  
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. Signature v4 is L for EU AWS region (for other regions is optionable).
71             If you need other region, I recommend some other S3 client (L).
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
86              
87             =head4 auto_region
88              
89             is is set I C, is automaticaly changed to I 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 protocol
102              
103             default I<1>
104              
105              
106             =head4 host
107              
108             default I
109              
110             =cut
111              
112             sub BUILD {
113 1     1 0 786 my ($self) = @_;
114              
115 1         2 foreach my $req (qw/aws_access_key_id aws_secret_access_key/) {
116 2 50       43 die "$req attribute required" unless defined $self->$req;
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
127              
128             =cut
129              
130             sub get_object {
131 1     1 1 650 my ($self, $bucket, $key) = @_;
132              
133 1         21 my $response = $self->requestator->request(
134             bucket => $bucket,
135             path => $key,
136             );
137              
138 1         79 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
151              
152              
153             return L
154              
155             =cut
156              
157             sub save_object_to_file {
158 1     1 1 2029 my ($self, $bucket, $key, $file) = @_;
159              
160 1 50       11 $file = Path::Tiny->tempfile() if !defined $file;
161              
162 1         13718 my $response = $self->requestator->request(
163             bucket => $bucket,
164             path => $key,
165             content_to_file => $file,
166             );
167              
168 1         59 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 - support version 4 signature too,
178             L support more AWS services,
179             some dependency of this module don't work on windows
180              
181             L - don't support version 4 signature,
182             some dependency of this module don't work on windows
183              
184             L - 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
187              
188             L - don't support version 4 signature,
189             similar interface like L,
190             last update Aug 15, 2009
191              
192             L - don't support version 4 signature,
193             simple interface
194              
195             L - don't support version 4 signature,
196             simple interface (similar like L),
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 Eseidl@avast.comE
209              
210             =cut
211              
212             1;