File: S3.pm

package info (click to toggle)
libnet-amazon-s3-perl 0.991-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,620 kB
  • sloc: perl: 9,906; makefile: 20
file content (1124 lines) | stat: -rw-r--r-- 31,924 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
package Net::Amazon::S3;
# ABSTRACT: Use the Amazon S3 - Simple Storage Service
$Net::Amazon::S3::VERSION = '0.991';
use Moose 0.85;
use MooseX::StrictConstructor 0.16;

use Carp;
use Digest::HMAC_SHA1;
use Safe::Isa ();

use Net::Amazon::S3::Bucket;
use Net::Amazon::S3::Client;
use Net::Amazon::S3::Client::Bucket;
use Net::Amazon::S3::Client::Object;
use Net::Amazon::S3::Error::Handler::Legacy;
use Net::Amazon::S3::HTTPRequest;
use Net::Amazon::S3::Request;
use Net::Amazon::S3::Response;
use Net::Amazon::S3::Operation::Bucket::Acl::Fetch;
use Net::Amazon::S3::Operation::Bucket::Acl::Set;
use Net::Amazon::S3::Operation::Bucket::Create;
use Net::Amazon::S3::Operation::Bucket::Delete;
use Net::Amazon::S3::Operation::Bucket::Location;
use Net::Amazon::S3::Operation::Bucket::Tags::Add;
use Net::Amazon::S3::Operation::Bucket::Tags::Delete;
use Net::Amazon::S3::Operation::Buckets::List;
use Net::Amazon::S3::Operation::Object::Acl::Fetch;
use Net::Amazon::S3::Operation::Object::Acl::Set;
use Net::Amazon::S3::Operation::Object::Add;
use Net::Amazon::S3::Operation::Object::Delete;
use Net::Amazon::S3::Operation::Object::Fetch;
use Net::Amazon::S3::Operation::Object::Head;
use Net::Amazon::S3::Operation::Object::Restore;
use Net::Amazon::S3::Operation::Object::Tags::Add;
use Net::Amazon::S3::Operation::Object::Tags::Delete;
use Net::Amazon::S3::Operation::Object::Upload::Abort;
use Net::Amazon::S3::Operation::Object::Upload::Complete;
use Net::Amazon::S3::Operation::Object::Upload::Create;
use Net::Amazon::S3::Operation::Object::Upload::Part;
use Net::Amazon::S3::Operation::Object::Upload::Parts;
use Net::Amazon::S3::Operation::Objects::Delete;
use Net::Amazon::S3::Operation::Objects::List;
use Net::Amazon::S3::Signature::V2;
use Net::Amazon::S3::Signature::V4;
use Net::Amazon::S3::Utils;
use Net::Amazon::S3::Vendor;
use Net::Amazon::S3::Vendor::Amazon;
use LWP::UserAgent::Determined;
use URI::Escape qw(uri_escape_utf8);

my $AMAZON_S3_HOST = 's3.amazonaws.com';

has authorization_context => (
	is => 'ro',
	isa => 'Net::Amazon::S3::Authorization',
	required => 0,

	handles => {
		aws_access_key_id     => 'aws_access_key_id',
		aws_secret_access_key => 'aws_secret_access_key',
		aws_session_token     => 'aws_session_token',
	},
);

has vendor => (
	is => 'ro',
	isa => 'Net::Amazon::S3::Vendor',
	required => 1,

	handles => {
		authorization_method => 'authorization_method',
		host                 => 'host',
		secure               => 'use_https',
		use_virtual_host     => 'use_virtual_host',
	},
);

has 'timeout' => ( is => 'ro', isa => 'Num',  required => 0, default => 30 );
has 'retry'   => ( is => 'ro', isa => 'Bool', required => 0, default => 0 );
has 'ua'
	=> is => 'rw'
	=> isa => 'LWP::UserAgent'
	=> required => 0
	=> lazy => 1
	=> builder => '_build_ua'
;
has 'err'    => ( is => 'rw', isa => 'Maybe[Str]',     required => 0 );
has 'errstr' => ( is => 'rw', isa => 'Maybe[Str]',     required => 0 );
has keep_alive_cache_size => ( is => 'ro', isa => 'Int', required => 0, default => 10 );

has error_handler_class => (
	is => 'ro',
	lazy => 1,
	default => 'Net::Amazon::S3::Error::Handler::Legacy',
);

has error_handler => (
	is => 'ro',
	lazy => 1,
	default => sub { $_[0]->error_handler_class->new (s3 => $_[0]) },
);

has bucket_class => (
	is          => 'ro',
	init_arg    => undef,
	lazy        => 1,
	default     => 'Net::Amazon::S3::Bucket',
);

sub _build_arg_authorization_context {
	my ($args) = @_;

	my $aws_access_key_id     = delete $args->{aws_access_key_id};
	my $aws_secret_access_key = delete $args->{aws_secret_access_key};
	my $use_iam_role          = delete $args->{use_iam_role};
	my $aws_session_token     = delete $args->{aws_session_token};

	if ($args->{authorization_context}) {
		return $args->{authorization_context};
	}

	if ($use_iam_role || $aws_session_token) {
		require Net::Amazon::S3::Authorization::IAM;

		return Net::Amazon::S3::Authorization::IAM->new (
			aws_access_key_id     => $aws_access_key_id,
			aws_secret_access_key => $aws_secret_access_key,
			aws_session_token     => $aws_session_token,
		)
	}

	require Net::Amazon::S3::Authorization::Basic;

	return Net::Amazon::S3::Authorization::Basic->new (
		aws_access_key_id => $aws_access_key_id,
		aws_secret_access_key => $aws_secret_access_key,
	);
}

sub _build_arg_vendor {
	my ($args) = @_;

	my %backward =
		map  { $_ => delete $args->{$_} }
		grep { exists  $args->{$_} }
		qw[ host secure use_virtual_host authorization_method ]
		;

	return $args->{vendor}
		if $args->{vendor};

	$backward{host} = $AMAZON_S3_HOST
		unless exists $backward{host};

	$backward{use_https} = delete $backward{secure}
		if exists $backward{secure};

	my $vendor_class = $backward{host} eq $AMAZON_S3_HOST
		? 'Net::Amazon::S3::Vendor::Amazon'
		: 'Net::Amazon::S3::Vendor'
		;

	return $vendor_class->new (%backward);
}

around BUILDARGS => sub {
	my ($orig, $class) = (shift, shift);
	my $args = $class->$orig (@_);

	# support compat authorization arguments
	$args->{authorization_context} = _build_arg_authorization_context $args;
	$args->{vendor}                = _build_arg_vendor                $args;

	$args;
};


sub _build_ua {
	my $self = shift;

	my $ua;
	if ( $self->retry ) {
		$ua = LWP::UserAgent::Determined->new(
			keep_alive            => $self->keep_alive_cache_size,
			requests_redirectable => [qw(GET HEAD DELETE PUT POST)],
		);
		$ua->timing('1,2,4,8,16,32');
	} else {
		$ua = LWP::UserAgent->new(
			keep_alive            => $self->keep_alive_cache_size,
			requests_redirectable => [qw(GET HEAD DELETE PUT POST)],
		);
	}

	$ua->timeout( $self->timeout );
	$ua->env_proxy;

	return $ua;
}

sub buckets {
	my ($self, %args) = @_;

	my $response = $self->_perform_operation (
		'Net::Amazon::S3::Operation::Buckets::List',
		%args,
	);

	return unless $response->is_success;

	my $owner_id          = $response->owner_id;;
	my $owner_displayname = $response->owner_displayname;

	my @buckets;
	foreach my $bucket ($response->buckets) {
		push @buckets, $self->bucket_class->new (
			account       => $self,
			bucket        => $bucket->{name},
			creation_date => $bucket->{creation_date},
		);

	}

	return +{
		owner_id          => $owner_id,
		owner_displayname => $owner_displayname,
		buckets           => \@buckets,
	};
}

sub add_bucket {
	my $self = shift;
	my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);

	my $response = $self->_perform_operation (
		'Net::Amazon::S3::Operation::Bucket::Create',

		%args,
	);

	return unless $response->is_success;

	return $self->bucket ($args{bucket});
}

sub bucket {
	my $self = shift;
	my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);

	return $args{bucket}
		if $args{bucket}->$Safe::Isa::_isa ($self->bucket_class);

	return $self->bucket_class->new ({
		account => $self,
		bucket => $args{bucket},
		(region => $args{region}) x defined $args{region},
	});
}

sub delete_bucket {
	my $self = shift;
	my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);

	croak 'must specify bucket'
		unless defined $args{bucket};

	my $response = $self->_perform_operation (
		'Net::Amazon::S3::Operation::Bucket::Delete',
		%args,
	);

	return unless $response->is_success;

	return 1;
}

sub list_bucket {
	my $self = shift;
	my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);

	my $response = $self->_perform_operation (
		'Net::Amazon::S3::Operation::Objects::List',
		%args,
	);

	return unless $response->is_success;

	my $return = {
		bucket      => $response->bucket,
		prefix      => $response->prefix,
		marker      => $response->marker,
		next_marker => $response->next_marker,
		max_keys    => $response->max_keys,
		is_truncated => $response->is_truncated,
	};

	my @keys;
	foreach my $node ($response->contents) {
		push @keys, {
			key           => $node->{key},
			last_modified => $node->{last_modified},
			etag          => $node->{etag},
			size          => $node->{size},
			storage_class => $node->{storage_class},
			owner_id      => $node->{owner}{id},
			owner_displayname => $node->{owner}{displayname},
			};
	}
	$return->{keys} = \@keys;

	if ( $args{delimiter} ) {
		$return->{common_prefixes} = [ $response->common_prefixes ];
	}

	return $return;
}

sub list_bucket_all {
	my $self = shift;
	my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket (\@_);

	my $bucket = $args{bucket};
	croak 'must specify bucket' unless $bucket;

	my $response = $self->list_bucket (%args);
	return $response unless $response->{is_truncated};
	my $all = $response;

	while (1) {
		my $next_marker = $response->{next_marker}
			|| $response->{keys}->[-1]->{key};
		$response       = $self->list_bucket (
			%args,
			marker => $next_marker,
		);
		push @{ $all->{keys} }, @{ $response->{keys} };
		last unless $response->{is_truncated};
	}

	delete $all->{is_truncated};
	delete $all->{next_marker};
	return $all;
}

# compat wrapper; deprecated as of 2005-03-23
sub add_key {
	my $self = shift;
	my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket_and_object (\@_);

	my $bucket = $self->bucket (delete $args{bucket});
	return $bucket->add_key (%args);
}

# compat wrapper; deprecated as of 2005-03-23
sub get_key {
	my $self = shift;
	my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket_and_object (\@_);

	my $bucket = $self->bucket (delete $args{bucket});
	return $bucket->get_key (%args);
}

# compat wrapper; deprecated as of 2005-03-23
sub head_key {
	my ( $self, $conf ) = @_;
	my $bucket = $self->bucket (delete $conf->{bucket});
	return $bucket->head_key( $conf->{key} );
}

# compat wrapper; deprecated as of 2005-03-23
sub delete_key {
	my $self = shift;
	my %args = Net::Amazon::S3::Utils->parse_arguments_with_bucket_and_object (\@_);

	my $bucket = $self->bucket (delete $args{bucket});
	return $bucket->delete_key (%args);
}

sub _perform_operation {
	my ($self, $operation, %params) = @_;

	my $error_handler = delete $params{error_handler};
	$error_handler = $self->error_handler unless defined $error_handler;

	my $request_class  = $operation . '::Request';
	my $response_class = $operation . '::Response';
	my $filename = delete $params{filename};

	my $request  = $request_class->new (s3 => $self, %params);
	my $http_response = $self->ua->request ($request->http_request, $filename);
	my $response    = $response_class->new (http_response => $http_response);

	$error_handler->handle_error ($response);

	return $response;
}

sub _urlencode {
	my ( $self, $unencoded ) = @_;
	return uri_escape_utf8( $unencoded, '^A-Za-z0-9_~\-\.' );
}

__PACKAGE__->meta->make_immutable;

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Net::Amazon::S3 - Use the Amazon S3 - Simple Storage Service

=head1 VERSION

version 0.991

=head1 SYNOPSIS

  use Net::Amazon::S3;
  use Net::Amazon::S3::Authorization::Basic;
  use Net::Amazon::S3::Authorization::IAM;
  my $aws_access_key_id     = 'fill me in';
  my $aws_secret_access_key = 'fill me in too';

  my $s3 = Net::Amazon::S3->new (
    authorization_context => Net::Amazon::S3::Authorization::Basic->new (
      aws_access_key_id     => $aws_access_key_id,
      aws_secret_access_key => $aws_secret_access_key,
    ),
    retry => 1,
  );

  # or use an IAM role.
  my $s3 = Net::Amazon::S3->new (
    authorization_context => Net::Amazon::S3::Authorization::IAM->new (
      aws_access_key_id     => $aws_access_key_id,
      aws_secret_access_key => $aws_secret_access_key,
    ),
    retry => 1,
  );

  # a bucket is a globally-unique directory
  # list all buckets that i own
  my $response = $s3->buckets;
  foreach my $bucket ( @{ $response->{buckets} } ) {
      print "You have a bucket: " . $bucket->bucket . "\n";
  }

  # create a new bucket
  my $bucketname = 'acmes_photo_backups';
  my $bucket = $s3->add_bucket( { bucket => $bucketname } )
      or die $s3->err . ": " . $s3->errstr;

  # or use an existing bucket
  $bucket = $s3->bucket($bucketname);

  # store a file in the bucket
  $bucket->add_key_filename( '1.JPG', 'DSC06256.JPG',
      { content_type => 'image/jpeg', },
  ) or die $s3->err . ": " . $s3->errstr;

  # store a value in the bucket
  $bucket->add_key( 'reminder.txt', 'this is where my photos are backed up' )
      or die $s3->err . ": " . $s3->errstr;

  # list files in the bucket
  $response = $bucket->list_all
      or die $s3->err . ": " . $s3->errstr;
  foreach my $key ( @{ $response->{keys} } ) {
      my $key_name = $key->{key};
      my $key_size = $key->{size};
      print "Bucket contains key '$key_name' of size $key_size\n";
  }

  # fetch file from the bucket
  $response = $bucket->get_key_filename( '1.JPG', 'GET', 'backup.jpg' )
      or die $s3->err . ": " . $s3->errstr;

  # fetch value from the bucket
  $response = $bucket->get_key('reminder.txt')
      or die $s3->err . ": " . $s3->errstr;
  print "reminder.txt:\n";
  print "  content length: " . $response->{content_length} . "\n";
  print "    content type: " . $response->{content_type} . "\n";
  print "            etag: " . $response->{content_type} . "\n";
  print "         content: " . $response->{value} . "\n";

  # delete keys
  $bucket->delete_key('reminder.txt') or die $s3->err . ": " . $s3->errstr;
  $bucket->delete_key('1.JPG')        or die $s3->err . ": " . $s3->errstr;

  # and finally delete the bucket
  $bucket->delete_bucket or die $s3->err . ": " . $s3->errstr;

=head1 DESCRIPTION

This module provides a Perlish interface to Amazon S3. From the
developer blurb: "Amazon S3 is storage for the Internet. It is
designed to make web-scale computing easier for developers. Amazon S3
provides a simple web services interface that can be used to store and
retrieve any amount of data, at any time, from anywhere on the web. It
gives any developer access to the same highly scalable, reliable,
fast, inexpensive data storage infrastructure that Amazon uses to run
its own global network of web sites. The service aims to maximize
benefits of scale and to pass those benefits on to developers".

To find out more about S3, please visit: http://s3.amazonaws.com/

To use this module you will need to sign up to Amazon Web Services and
provide an "Access Key ID" and " Secret Access Key". If you use this
module, you will incurr costs as specified by Amazon. Please check the
costs. If you use this module with your Access Key ID and Secret
Access Key you must be responsible for these costs.

I highly recommend reading all about S3, but in a nutshell data is
stored in values. Values are referenced by keys, and keys are stored
in buckets. Bucket names are global.

Note: This is the legacy interface, please check out
L<Net::Amazon::S3::Client> instead.

Development of this code happens here: https://github.com/rustyconover/net-amazon-s3

=head2 Bucket names with dots, HTTPS, and Signature V4

At the moment Amazon S3 doesn't play well with HTTPS and virtual bucket hosts
if bucket name contains dots.

Due the current implementation of Signature V4 handling you should use workaround
consisting of usage of region hostnames

	my $bucket_region = $global_s3->bucket ($bucket)->_head_region;

	my $region_s3 = Net::Amazon:S3->new (
		...,
		vendor => Net::Amazon::S3::Vendor::Amazon->new (
			host => "s3-$bucket_region.amazonaws.com",
			use_virtual_host => 0,
		),
	);

	my $bucket = $region_s3->bucket ($bucket);

And use bucket instance / region s3 connection.

=head1 METHODS

=head2 new

Create a new S3 client object. Takes some arguments:

=over

=item authorization_context

Class that provides authorization information.

See one of available implementations for more

=over

=item L<Net::Amazon::S3::Authorization::Basic>

=item L<Net::Amazon::S3::Authorization::IAM>

=back

=item vendor

Instance of L<Net::Amazon::S3::Vendor> holding vendor specific deviations.

S3 became widely used object storage protocol with many vendors providing
different feature sets and different compatibility level.

One common difference is bucket's HEAD request to determine its region.

To maintain currently known differences along with any differencies that
may rise in feature it's better to hold vendor specification in dedicated
classes. This also allows users to build their own fine-tuned vendor classes.

=over

=item L<Net::Amazon::S3::Vendor::Amazon>

=item L<Net::Amazon::S3::Vendor::Generic>

=back

=item aws_access_key_id

Deprecated.

When used it's used to create authorization context.

Use your Access Key ID as the value of the AWSAccessKeyId parameter
in requests you send to Amazon Web Services (when required). Your
Access Key ID identifies you as the party responsible for the
request.

=item aws_secret_access_key

Deprecated.

When used it's used to create authorization context.

Since your Access Key ID is not encrypted in requests to AWS, it
could be discovered and used by anyone. Services that are not free
require you to provide additional information, a request signature,
to verify that a request containing your unique Access Key ID could
only have come from you.

DO NOT INCLUDE THIS IN SCRIPTS OR APPLICATIONS YOU DISTRIBUTE. YOU'LL BE SORRY

=item aws_session_token

Deprecated.

When used it's used to create authorization context.

If you are using temporary credentials provided by the AWS Security Token
Service, set the token here, and it will be added to the request in order to
authenticate it.

=item use_iam_role

Deprecated.

When used it's used to create authorization context.

If you'd like to use IAM provided temporary credentials, pass this option
with a true value.

=item secure

Deprecated.

Set this to C<0> if you don't want to use SSL-encrypted connections when talking
to S3. Defaults to C<1>.

To use SSL-encrypted connections, LWP::Protocol::https is required.

See L<#vendor> and L<Net::Amazon::S3::Vendor>.

=item keep_alive_cache_size

Set this to C<0> to disable Keep-Alives.  Default is C<10>.

=item timeout

How many seconds should your script wait before bailing on a request to S3? Defaults
to 30.

=item retry

If this library should retry upon errors. This option is recommended.
This uses exponential backoff with retries after 1, 2, 4, 8, 16, 32 seconds,
as recommended by Amazon. Defaults to off.

When retry is on, request will be automatically retried when one of following
HTTP statuses happens

=over

=item 408 - Request Timeout

=item 500 - Internal Server Error

=item 502 - Bad Gateway

=item 503 - Service Unavailable

=item 504 - Gateway Timeout

=back

For more details see L<LWP::UserAgent::Determined>.

=item host

Deprecated.

The S3 host endpoint to use. Defaults to 's3.amazonaws.com'. This allows
you to connect to any S3-compatible host.

See L<#vendor> and L<Net::Amazon::S3::Vendor>.

=item use_virtual_host

Deprecated.

Use the virtual host method ('bucketname.s3.amazonaws.com') instead of specifying the
bucket at the first part of the path. This is particularly useful if you want to access
buckets not located in the US-Standard region (such as EU, Asia Pacific or South America).
See L<http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html> for the pros and cons.

See L<#vendor> and L<Net::Amazon::S3::Vendor>.

=item authorization_method

Deprecated.

Authorization implementation package name.

This library provides L<< Net::Amazon::S3::Signature::V2 >> and L<< Net::Amazon::S3::Signature::V4 >>

Default is Signature 4 if host is C<< s3.amazonaws.com >>, Signature 2 otherwise

See L<#vendor> and L<Net::Amazon::S3::Vendor>.

=item error_handler_class

Error handler class name (package name), see L<< Net::Amazon::S3::Error::Handler >>
for more.

Default: L<< Net::Amazon::S3::Error::Handler::Legacy >>

=item error_handler

Instance of error handler class.

=back

=head3 Notes

When using L<Net::Amazon::S3> in child processes using fork (such as in
combination with the excellent L<Parallel::ForkManager>) you should create the
S3 object in each child, use a fresh LWP::UserAgent in each child, or disable
the L<LWP::ConnCache> in the parent:

    $s3->ua( LWP::UserAgent->new( 
        keep_alive => 0, requests_redirectable => [qw'GET HEAD DELETE PUT POST'] );

=head2 buckets

Returns undef on error, else hashref of results

=head2 add_bucket

	# Create new bucket with default location
	my $bucket = $s3->add_bucket ('new-bucket');

	# Create new bucket in another location
	my $bucket = $s3->add_bucket ('new-bucket', location_constraint => 'eu-west-1');
	my $bucket = $s3->add_bucket ('new-bucket', { location_constraint => 'eu-west-1' });
	my $bucket = $s3->add_bucket (bucket => 'new-bucket', location_constraint => 'eu-west-1');
	my $bucket = $s3->add_bucket ({ bucket => 'new-bucket', location_constraint => 'eu-west-1' });

Method creates and returns new bucket.

In case of error it reports it and returns C<undef> (refer L</"ERROR HANDLING">).

Recognized positional arguments (refer L</"CALLING CONVENTION">)

=over

=item bucket

Required, recognized as positional.

The name of the bucket you want to add.

=back

Recognized optional arguments

=over

=item acl

	acl => 'private'
	acl => Net::Amazon::S3::ACL::Canned->PRIVATE
	acl => Net::Amazon::S3::ACL::Set->grant_read (email => 'foo@bar.baz')

I<Available since v0.94>

Set ACL to the newly created bucket. Refer L<Net::Amazon::S3::ACL> for possibilities.

=item acl_short (deprecated)

I<Deprecated since v0.94>

When specified its value is used to populate C<acl> argument (unless it exists).

=item location_constraint

Optional.

Sets the location constraint of the new bucket. If left unspecified, the
default S3 datacenter location will be used.

This library recognizes regions according Amazon S3 documentation

=over

=item →

L<https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region>

=item →

L<https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html#API_CreateBucket_RequestSyntax>

=back

=back

Provides operation L<CreateBucket|https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html>.

=head2 bucket BUCKET

	# build bucket with guessed region
	$s3->bucket ('foo');
	$s3->bucket (bucket => 'foo');
	$s3->bucket (name   => 'foo');

	# build with explicit region
	$s3->bucket ('foo', region => 'bar');

Returns an (unverified) bucket object from an account. Does no network access.

However, when guessing region, C<HeadRegion> operation may be called before
first network access.

Region is mandatory when using Signature V4 authorization, which is default
for AWS. AWS limits number of HTTP requests, see L<https://aws.amazon.com/premiumsupport/knowledge-center/s3-request-limit-avoid-throttling/>

=head2 delete_bucket

	$s3->delete_bucket ($bucket);
	$s3->delete_bucket (bucket => $bucket);

Deletes bucket from account.

Returns C<true> if the bucket is successfully deleted.

Returns C<false> and reports an error otherwise (refer L</"ERROR HANDLING">)

Positional arguments (refer L</"CALLING CONVENTION">)

=over

=item bucket

Required.

The name of the bucket or L<Net::Amazon::S3::Bucket> instance you want to delete.

=back

Provides operation L<"DeleteBucket"|https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucket.html>

=head2 list_bucket

List all keys in this bucket.

Takes a hashref of arguments:

MANDATORY

=over

=item bucket

The name of the bucket you want to list keys on

=back

OPTIONAL

=over

=item prefix

Restricts the response to only contain results that begin with the
specified prefix. If you omit this optional argument, the value of
prefix for your query will be the empty string. In other words, the
results will be not be restricted by prefix.

=item delimiter

If this optional, Unicode string parameter is included with your
request, then keys that contain the same string between the prefix
and the first occurrence of the delimiter will be rolled up into a
single result element in the CommonPrefixes collection. These
rolled-up keys are not returned elsewhere in the response.  For
example, with prefix="USA/" and delimiter="/", the matching keys
"USA/Oregon/Salem" and "USA/Oregon/Portland" would be summarized
in the response as a single "USA/Oregon" element in the CommonPrefixes
collection. If an otherwise matching key does not contain the
delimiter after the prefix, it appears in the Contents collection.

Each element in the CommonPrefixes collection counts as one against
the MaxKeys limit. The rolled-up keys represented by each CommonPrefixes
element do not.  If the Delimiter parameter is not present in your
request, keys in the result set will not be rolled-up and neither
the CommonPrefixes collection nor the NextMarker element will be
present in the response.

=item max-keys

This optional argument limits the number of results returned in
response to your query. Amazon S3 will return no more than this
number of results, but possibly less. Even if max-keys is not
specified, Amazon S3 will limit the number of results in the response.
Check the IsTruncated flag to see if your results are incomplete.
If so, use the Marker parameter to request the next page of results.
For the purpose of counting max-keys, a 'result' is either a key
in the 'Contents' collection, or a delimited prefix in the
'CommonPrefixes' collection. So for delimiter requests, max-keys
limits the total number of list results, not just the number of
keys.

=item marker

This optional parameter enables pagination of large result sets.
C<marker> specifies where in the result set to resume listing. It
restricts the response to only contain results that occur alphabetically
after the value of marker. To retrieve the next page of results,
use the last key from the current page of results as the marker in
your next request.

See also C<next_marker>, below.

If C<marker> is omitted,the first page of results is returned.

=back

Returns undef on error and a hashref of data on success:

The hashref looks like this:

  {
        bucket          => $bucket_name,
        prefix          => $bucket_prefix,
        common_prefixes => [$prefix1,$prefix2,...]
        marker          => $bucket_marker,
        next_marker     => $bucket_next_available_marker,
        max_keys        => $bucket_max_keys,
        is_truncated    => $bucket_is_truncated_boolean
        keys            => [$key1,$key2,...]
   }

Explanation of bits of that:

=over

=item common_prefixes

If list_bucket was requested with a delimiter, common_prefixes will
contain a list of prefixes matching that delimiter.  Drill down into
these prefixes by making another request with the prefix parameter.

=item is_truncated

B flag that indicates whether or not all results of your query were
returned in this response. If your results were truncated, you can
make a follow-up paginated request using the Marker parameter to
retrieve the rest of the results.

=item next_marker

A convenience element, useful when paginating with delimiters. The
value of C<next_marker>, if present, is the largest (alphabetically)
of all key names and all CommonPrefixes prefixes in the response.
If the C<is_truncated> flag is set, request the next page of results
by setting C<marker> to the value of C<next_marker>. This element
is only present in the response if the C<delimiter> parameter was
sent with the request.

=back

Each key is a hashref that looks like this:

     {
        key           => $key,
        last_modified => $last_mod_date,
        etag          => $etag, # An MD5 sum of the stored content.
        size          => $size, # Bytes
        storage_class => $storage_class # Doc?
        owner_id      => $owner_id,
        owner_displayname => $owner_name
    }

=head2 list_bucket_all

List all keys in this bucket without having to worry about
'marker'. This is a convenience method, but may make multiple requests
to S3 under the hood.

Takes the same arguments as list_bucket.

=head2 _perform_operation

    my $response = $s3->_perform_operation ('Operation' => (
        # ... operation request parameters
    ));

Internal operation implementation method, takes request construction parameters,
performs necessary HTTP requests(s) and returns Response instance.

Method takes same named parameters as realted Request class.

Method provides available contextual parameters by default (eg s3, bucket)

Method invokes contextual error handler.

=head1 CALLING CONVENTION

I<Available since v0.97> - calling convention extentend

In order to make method calls somehow consistent, backward compatible,
and extendable, API's methods support multiple ways how to provide their arguments

=over

=item plain named arguments (preferred)

	method (named => 'argument', another => 'argument');

=item trailing configuration hash

	method ({ named => 'argument', another => 'argument' });
	method (positional, { named => 'argument', another => 'argument' } );

Last argument of every method can be configuration hash, treated as additional
named arguments. Can be combined with named arguments.

=item positional arguments with optional named arguments

	method (positional, named => 'argument', another => 'argument');
	method (positional, { named => 'argument', another => 'argument' } );

For methods supporting mandatory positional arguments additional named
arguments and/or configuration hash is supported.

Named arguments or configuration hash can specify value of positional
arguments as well removing it from list of required positional arguments
for given call (see example)

	$s3->bucket->add_key ('key', 'value', acl => $acl);
	$s3->bucket->add_key ('value', key => 'key', acl => $acl);
	$s3->bucket->add_key (key => 'key', value => 'value', acl => $acl);

=back

=head1 ERROR HANDLING

L<Net::Amazon::S3> supports pluggable error handling via
L<Net::Amazon::S3::Error::Handler>.

When response ends up with an error, every method reports it, and in case it
receives control back (no exception), it returns C<undef>.

Default error handling for L<Net::Amazon::S3> is L<Net::Amazon::S3::Error::Handler::Legacy>
which (mostly) sets C<err> and C<errstr>.

=head1 LICENSE

This module contains code modified from Amazon that contains the
following notice:

  #  This software code is made available "AS IS" without warranties of any
  #  kind.  You may copy, display, modify and redistribute the software
  #  code either by itself or as incorporated into your code; provided that
  #  you do not remove any proprietary notices.  Your use of this software
  #  code is at your own risk and you waive any claim against Amazon
  #  Digital Services, Inc. or its affiliates with respect to your use of
  #  this software code. (c) 2006 Amazon Digital Services, Inc. or its
  #  affiliates.

=head1 TESTING

Testing S3 is a tricky thing. Amazon wants to charge you a bit of
money each time you use their service. And yes, testing counts as using.
Because of this, the application's test suite skips anything approaching
a real test unless you set these three environment variables:

=over

=item AMAZON_S3_EXPENSIVE_TESTS

Doesn't matter what you set it to. Just has to be set

=item AWS_ACCESS_KEY_ID

Your AWS access key

=item AWS_ACCESS_KEY_SECRET

Your AWS sekkr1t passkey. Be forewarned that setting this environment variable
on a shared system might leak that information to another user. Be careful.

=back

=head1 AUTHOR

Leon Brocard <acme@astray.com> and unknown Amazon Digital Services programmers.

Brad Fitzpatrick <brad@danga.com> - return values, Bucket object

Pedro Figueiredo <me@pedrofigueiredo.org> - since 0.54

Branislav Zahradník <barney@cpan.org> - since v0.81

=head1 SEE ALSO

L<Net::Amazon::S3::Bucket>

=head1 AUTHOR

Branislav Zahradník <barney@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2022 by Amazon Digital Services, Leon Brocard, Brad Fitzpatrick, Pedro Figueiredo, Rusty Conover, Branislav Zahradník.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut