File Coverage

lib/AnyEvent/Net/Amazon/S3.pm
Criterion Covered Total %
statement 18 44 40.9
branch 0 6 0.0
condition 0 5 0.0
subroutine 6 8 75.0
pod n/a
total 24 63 38.1


line stmt bran cond sub pod time code
1             package AnyEvent::Net::Amazon::S3;
2              
3             # ABSTRACT: Use the Amazon S3 - Simple Storage Service
4             our $VERSION = 'v0.04.0.80'; # VERSION
5              
6 1     1   460 use strict;
  1         2  
  1         31  
7 1     1   4 use warnings;
  1         1  
  1         28  
8              
9 1     1   3 use Carp;
  1         2  
  1         61  
10 1     1   424 use Module::AnyEvent::Helper;
  1         8740  
  1         42  
11 1     1   7 use AnyEvent;
  1         1  
  1         224  
12              
13             sub list_bucket_all_async {
14 0     0     my ( $self, $conf ) = @_;
15 0   0       $conf ||= {};
16 0           my $bucket = $conf->{bucket};
17 0 0         croak 'must specify bucket' unless $bucket;
18              
19 0           my $cv = AE::cv;
20             Module::AnyEvent::Helper::bind_scalar($cv, $self->list_bucket_async($conf), sub {
21              
22 0     0     my $response = shift->recv;
23 0 0         return $response unless $response->{is_truncated};
24 0           my $all = $response;
25              
26 0           my $iter; $iter = sub {
27 0   0       my $next_marker = $response->{next_marker}
28             || $response->{keys}->[-1]->{key};
29 0           $conf->{marker} = $next_marker;
30 0           $conf->{bucket} = $bucket;
31             Module::AnyEvent::Helper::bind_scalar($cv, $self->list_bucket_async($conf), sub {
32 0           $response = shift->recv;
33 0           push @{ $all->{keys} }, @{ $response->{keys} };
  0            
  0            
34 0 0         if($response->{is_truncated}) {
35 0           $iter->();
36             } else {
37 0           delete $all->{is_truncated};
38 0           delete $all->{next_marker};
39 0           return $all;
40             }
41 0           });
42 0           };
43 0           $iter->();
44 0           });
45 0           return $cv;
46             }
47              
48 1         17 use Module::AnyEvent::Helper::Filter -as => __PACKAGE__, -target => 'Net::Amazon::S3',
49             -transformer => 'Net::Amazon::S3',
50             -remove_func => [qw(list_bucket_all)],
51             -translate_func => [qw(buckets add_bucket delete_bucket list_bucket add_key get_key head_key delete_key _send_request _do_http _send_request_expect_nothing _send_request_expect_nothing_probed)],
52             -replace_func => [qw(request)]
53 1     1   596 ;
  1         94032  
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             AnyEvent::Net::Amazon::S3 - Use the Amazon S3 - Simple Storage Service
66              
67             =head1 VERSION
68              
69             version v0.04.0.80
70              
71             =head1 SYNOPSIS
72              
73             # Can be used as same as Net::Amazon::S3
74             use AnyEvent::Net::Amazon::S3;
75             my $aws_access_key_id = 'fill me in';
76             my $aws_secret_access_key = 'fill me in too';
77              
78             my $s3 = AnyEvent::Net::Amazon::S3->new(
79             { aws_access_key_id => $aws_access_key_id,
80             aws_secret_access_key => $aws_secret_access_key,
81             retry => 1,
82             }
83             );
84              
85             # a bucket is a globally-unique directory
86             # list all buckets that i own
87             my $response = $s3->buckets;
88             foreach my $bucket ( @{ $response->{buckets} } ) {
89             print "You have a bucket: " . $bucket->bucket . "\n";
90             }
91              
92             # create a new bucket
93             my $bucketname = 'acmes_photo_backups';
94             my $bucket = $s3->add_bucket( { bucket => $bucketname } )
95             or die $s3->err . ": " . $s3->errstr;
96              
97             # or use an existing bucket
98             $bucket = $s3->bucket($bucketname);
99              
100             # store a file in the bucket
101             $bucket->add_key_filename( '1.JPG', 'DSC06256.JPG',
102             { content_type => 'image/jpeg', },
103             ) or die $s3->err . ": " . $s3->errstr;
104              
105             # store a value in the bucket
106             $bucket->add_key( 'reminder.txt', 'this is where my photos are backed up' )
107             or die $s3->err . ": " . $s3->errstr;
108              
109             # list files in the bucket
110             $response = $bucket->list_all
111             or die $s3->err . ": " . $s3->errstr;
112             foreach my $key ( @{ $response->{keys} } ) {
113             my $key_name = $key->{key};
114             my $key_size = $key->{size};
115             print "Bucket contains key '$key_name' of size $key_size\n";
116             }
117              
118             # fetch file from the bucket
119             $response = $bucket->get_key_filename( '1.JPG', 'GET', 'backup.jpg' )
120             or die $s3->err . ": " . $s3->errstr;
121              
122             # fetch value from the bucket
123             $response = $bucket->get_key('reminder.txt')
124             or die $s3->err . ": " . $s3->errstr;
125             print "reminder.txt:\n";
126             print " content length: " . $response->{content_length} . "\n";
127             print " content type: " . $response->{content_type} . "\n";
128             print " etag: " . $response->{content_type} . "\n";
129             print " content: " . $response->{value} . "\n";
130              
131             # delete keys
132             $bucket->delete_key('reminder.txt') or die $s3->err . ": " . $s3->errstr;
133             $bucket->delete_key('1.JPG') or die $s3->err . ": " . $s3->errstr;
134              
135             # and finally delete the bucket
136             $bucket->delete_bucket or die $s3->err . ": " . $s3->errstr;
137              
138             =head1 DESCRIPTION
139              
140             This module provides the same interface as L<Net::Amazon::S3>.
141             In addition, some asynchronous methods returning AnyEvent condition variable are added.
142              
143             Note: This is the legacy interface, please check out
144             L<AnyEvent::Net::Amazon::S3::Client> instead.
145              
146             =for test_synopsis no warnings;
147              
148             =head1 METHODS
149              
150             All L<Net::Amazon::S3> methods are available.
151             In addition, there are the following asynchronous methods.
152             Arguments of the methods are identical as original but return value becomes L<AnyEvent> condition variable.
153             You can get actual return value by calling C<shift-E<gt>recv()>.
154              
155             =over 4
156              
157             =item buckets_async
158              
159             =item add_bucket_async
160              
161             =item delete_bucket_async
162              
163             =item list_bucket_async
164              
165             =item list_bucket_all_async
166              
167             =item add_key_async
168              
169             =item get_key_async
170              
171             =item head_key_async
172              
173             =item delete_key_async
174              
175             =back
176              
177             =for Pod::Coverage BUILD
178             bucket
179              
180             =head1 TESTING
181              
182             The following description is extracted from L<Net::Amazon::S3>.
183             They are all applicable to this module.
184              
185             Testing S3 is a tricky thing. Amazon wants to charge you a bit of
186             money each time you use their service. And yes, testing counts as using.
187             Because of this, the application's test suite skips anything approaching
188             a real test unless you set these three environment variables:
189              
190             =over 4
191              
192             =item AMAZON_S3_EXPENSIVE_TESTS
193              
194             Doesn't matter what you set it to. Just has to be set
195              
196             =item AWS_ACCESS_KEY_ID
197              
198             Your AWS access key
199              
200             =item AWS_ACCESS_KEY_SECRET
201              
202             Your AWS sekkr1t passkey. Be forewarned that setting this environment variable
203             on a shared system might leak that information to another user. Be careful.
204              
205             =back
206              
207             =head1 SEE ALSO
208              
209             =over 4
210              
211             =item *
212              
213             L<AnyEvent::Net::Amazon::S3::Bucket>
214              
215             =item *
216              
217             L<Net::Amazaon::S3> - Based on it as original.
218              
219             =item *
220              
221             L<Module::AnyEvent::Helper> - Used by this module. There are some description for needs of _async methods.
222              
223             =back
224              
225             =head1 AUTHOR
226              
227             Yasutaka ATARASHI <yakex@cpan.org>
228              
229             =head1 COPYRIGHT AND LICENSE
230              
231             This software is copyright (c) 2012 by Yasutaka ATARASHI.
232              
233             This is free software; you can redistribute it and/or modify it under
234             the same terms as the Perl 5 programming language system itself.
235              
236             =cut