Net::Amazon::S3(3pm)
π Net::Amazon::S3 π
okay bestie, welcome to the manual page for the Perl module Net::Amazon::S3 β aka the iconic legacy way to talk to Amazon S3 from Perl. It's giving "User Contributed Perl Documentation", it's giving main character energy for your object storage. Let's get into it π«Ά
β¨ NAME β¨
Net::Amazon::S3 β Use the Amazon S3 - Simple Storage Service
πΈ VERSION πΈ
version 0.992
π¦ SYNOPSIS π¦
like, here's the whole vibe in one code block, no cap:
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;
π DESCRIPTION π
so this module hands you a Perlish interface to Amazon S3. and like, from the official developer blurb, direct quote bestie:
"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".
wanna know more about S3? go here: http://s3.amazonaws.com/ π
to actually use this module you gotta sign up to Amazon Web Services and provide an "Access Key ID" and a " Secret Access Key". and ngl, if you use this module, you will incurr costs as specified by Amazon. please check the costs, fr fr. if you use this module with your Access Key ID and Secret Access Key, you are responsible for those costs, periodt.
I highly recommend reading all about S3 (genuinely, it's a lot), but in a nutshell: data is stored in values. values are referenced by keys, and keys are stored in buckets. and bucket names are global. global global.
β οΈ Note, bestie: this is the legacy interface, please check out Net::Amazon::S3::Client instead. π
development of this code happens here: https://github.com/happy-barney/perl-net-amazon-s3
π Bucket names with dots, HTTPS, and Signature V4 π
so rn Amazon S3 doesn't play well with HTTPS and virtual bucket hosts if the bucket name contains dots. it's giving drama. π
due the current implementation of Signature V4 handling you should use the 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 the bucket instance / region s3 connection. iykyk β¨
π METHODS π
π§ new
creates a brand new S3 client object, slay. takes some arguments:
authorization_contextβ class that provides authorization information. see one of the available implementations for more:Net::Amazon::S3::Authorization::BasicNet::Amazon::S3::Authorization::IAM
vendorβ instance ofNet::Amazon::S3::Vendorholding vendor specific deviations. S3 became a widely used object storage protocol with many vendors providing different feature sets and different compatibility levels. 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 the vendor specification in dedicated classes. this also allows users to build their own fine-tuned vendor classes. πNet::Amazon::S3::Vendor::AmazonNet::Amazon::S3::Vendor::Generic
aws_access_key_idβ Deprecated. when used, it's used to create the authorization context. use your Access Key ID as the value of theAWSAccessKeyIdparameter in requests you send to Amazon Web Services (when required). your Access Key ID identifies you as the party responsible for the request.aws_secret_access_keyβ Deprecated. when used, it's used to create the 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. like literally, bestie. π
aws_session_tokenβ Deprecated. when used, it's used to create the 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.use_iam_roleβ Deprecated. when used, it's used to create the authorization context. if you'd like to use IAM provided temporary credentials, pass this option with a true value.secureβ Deprecated. set this to0if you don't want to use SSL-encrypted connections when talking to S3. defaults to1. to use SSL-encrypted connections,LWP::Protocol::httpsis required. see #vendor andNet::Amazon::S3::Vendor.keep_alive_cache_sizeβ set this to0to disable Keep-Alives. default is10.timeoutβ how many seconds should your script wait before bailing on a request to S3? defaults to30.retryβ if this library should retry upon errors. this option is recommended. it uses exponential backoff with retries after1,2,4,8,16,32seconds, as recommended by Amazon. defaults to off. when retry is on, the request will be automatically retried when one of the following HTTP statuses happens:408- Request Timeout500- Internal Server Error502- Bad Gateway503- Service Unavailable504- Gateway Timeout for more details seeLWP::UserAgent::Determined.
hostβ Deprecated. the S3 host endpoint to use. defaults to's3.amazonaws.com'. this allows you to connect to any S3-compatible host. see #vendor andNet::Amazon::S3::Vendor.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 <http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html> for the pros and cons. see #vendor andNet::Amazon::S3::Vendor.authorization_methodβ Deprecated. authorization implementation package name. this library providesNet::Amazon::S3::Signature::V2andNet::Amazon::S3::Signature::V4default is Signature 4 if host is"s3.amazonaws.com", Signature 2 otherwise. see #vendor andNet::Amazon::S3::Vendor.error_handler_classβ error handler class name (package name), seeNet::Amazon::S3::Error::Handlerfor more. default:Net::Amazon::S3::Error::Handler::Legacyerror_handlerβ instance of error handler class.
π Notes π
when using Net::Amazon::S3 in child processes using fork (such as in combination with the excellent Parallel::ForkManager) you should create the S3 object in each child, use a fresh LWP::UserAgent in each child, or disable the LWP::ConnCache in the parent:
$s3->ua( LWP::UserAgent->new(
keep_alive => 0, requests_redirectable => [qw'GET HEAD DELETE PUT POST'] );
π buckets π
returns undef on error, else a hashref of results. simple as that, bestie.
π« 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' });
this method creates and returns a new bucket. iconic behavior honestly. β¨
in case of error it reports it and returns "undef" (refer to "ERROR HANDLING").
recognized positional arguments (refer to "CALLING CONVENTION"):
bucketβ required, recognized as positional.bucketβ Required, bestie. This is the name of the bucket you wanna add. π
Recognized optional arguments π
aclβ sets the ACL on the freshly-created bucket. Here's the three flavours it comes in:
acl => 'private'
acl => Net::Amazon::S3::ACL::Canned->PRIVATE
acl => Net::Amazon::S3::ACL::Set->grant_read (email => 'foo@bar.baz')
Available since v0.94. Go peek at Net::Amazon::S3::ACL for all the possibilities, ok? β¨
acl_shortβ deprecated since v0.94 π₯² When you specify it, its value gets used to populate theaclargument (unless that one already exists). So like, it's a fallback. Old news, but we don't forget it.location_constraintβ Optional. Sets the location constraint of the new bucket. If you leave it unspecified, the default S3 datacenter location gets used, no cap.
This library recognizes regions according Amazon S3 documentation πΊοΈ
- <https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region>
- <https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html#API_CreateBucket_RequestSyntax>
Provides operation CreateBucket <https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html>. π
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. Lowkey important: it does no network access. Chill.
However β and this is the catch, bestie β when guessing region, the HeadRegion operation may be called before that first network access. So like, it might reach out. π
Region is mandatory when you're using Signature V4 authorization, which is the default for AWS. And AWS limits the number of HTTP requests, so don't go wild β see <https://aws.amazon.com/premiumsupport/knowledge-center/s3-request-limit-avoid-throttling/>.
delete_bucket π
$s3->delete_bucket ($bucket);
$s3->delete_bucket (bucket => $bucket);
Deletes bucket from account. Poof. Gone.
Returns true if the bucket is successfully deleted. π
Returns false and reports an error otherwise (refer to "ERROR HANDLING"). π
Positional arguments (refer to "CALLING CONVENTION")
bucketβ Required. The name of the bucket, or aNet::Amazon::S3::Bucketinstance, that you want to delete.
Provides operation DeleteBucket <https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucket.html>.
list_bucket π
List all keys in this bucket. Takes a hashref of arguments:
MANDATORY
bucketβ The name of the bucket you want to list keys on.
OPTIONAL
prefixβ Restricts the response to only contain results that begin with the specified prefix. If you omit this optional argument, the value ofprefixfor your query will be the empty string. In other words, the results will not be restricted by prefix. Simple as that. β¨delimiterβ If this optional, Unicode string parameter is included with your request, then keys that contain the same string between theprefixand the first occurrence of thedelimiterwill be rolled up into a single result element in theCommonPrefixescollection. These rolled-up keys are not returned elsewhere in the response. For example, withprefix="USA/"anddelimiter="/", the matching keys"USA/Oregon/Salem"and"USA/Oregon/Portland"would be summarized in the response as a single"USA/Oregon"element in theCommonPrefixescollection. If an otherwise matching key does not contain the delimiter after the prefix, it appears in theContentscollection.
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. iykyk π¦
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 ifmax-keysis not specified, Amazon S3 will limit the number of results in the response. Check theIsTruncatedflag to see if your results are incomplete. If so, use theMarkerparameter to request the next page of results. For the purpose of countingmax-keys, a 'result' is either a key in the 'Contents' collection, or a delimited prefix in the 'CommonPrefixes' collection. So for delimiter requests,max-keyslimits the total number of list results, not just the number of keys.markerβ This optional parameter enables pagination of large result sets.markerspecifies where in the result set to resume listing. It restricts the response to only contain results that occur alphabetically after the value ofmarker. To retrieve the next page of results, use the last key from the current page of results as themarkerin your next request.
See also next_marker, below. π
If marker is omitted, the first page of results is returned.
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 π
common_prefixesβ Iflist_bucketwas requested with a delimiter,common_prefixeswill contain a list of prefixes matching that delimiter. Drill down into these prefixes by making another request with theprefixparameter.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 theMarkerparameter to retrieve the rest of the results.next_markerβ A convenience element, useful when paginating with delimiters. The value ofnext_marker, if present, is the largest (alphabetically) of all key names and allCommonPrefixesprefixes in the response. If theis_truncatedflag is set, request the next page of results by settingmarkerto the value ofnext_marker. This element is only present in the response if thedelimiterparameter was sent with the request.
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
}
list_bucket_all π
List all keys in this bucket without having to worry about marker. It's a convenience method, but heads up β it may make multiple requests to S3 under the hood. π₯
Takes the same arguments as list_bucket.
_perform_operation βοΈ
my $response = $s3->_perform_operation ('Operation' => (
# ... operation request parameters
));
Internal operation implementation method. It takes request construction parameters, performs the necessary HTTP request(s), and returns a Response instance.
Method takes same named parameters as the related Request class.
Method provides available contextual parameters by default (eg s3, bucket).
Method invokes the contextual error handler.
CALLING CONVENTION π£οΈ
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. Iconic, honestly. π
- plain named arguments (preferred)
method (named => 'argument', another => 'argument');
- trailing configuration hash
method ({ named => 'argument', another => 'argument' });
method (positional, { named => 'argument', another => 'argument' } );
Last argument of every method can be a configuration hash, treated as additional named arguments. Can be combined with named arguments. β¨
- 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 a configuration hash is supported.
Named arguments or configuration hash can specify the value of positional arguments as well, removing it from the list of required positional arguments for the 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);
ERROR HANDLING π
Net::Amazon::S3 supports pluggable error handling via Net::Amazon::S3::Error::Handler.
When a response ends up with an error, every method reports it, and in case it receives control back (no exception), it returns undef.
Default error handling for Net::Amazon::S3 is Net::Amazon::S3::Error::Handler::Legacy which (mostly) sets err and errstr. So if something's off, that's your girl. π
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.
TESTING π§ͺ
Testing S3 is a tricky thing, bestie. 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:
AMAZON_S3_EXPENSIVE_TESTSβ Doesn't matter what you set it to. Just has to be set. Literally any value. Set it to1, set it toyes, set it toslay. Just set it. π₯AWS_ACCESS_KEY_IDβ Your AWS access key.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. Fr fr, be careful. π
β¨ AUTHOR β¨
Okay so this page was a group project and we're crediting everybody, periodt. π
- Leon Brocard
<acme@astray.com>and unknown Amazon Digital Services programmers β yeah, the mystery queens never got named, we still love them tho. - Brad Fitzpatrick
<brad@danga.com>β he did the return values and theBucketobject. icon behavior. π - Pedro Figueiredo
<me@pedrofigueiredo.org>β on board since 0.54. - Branislav ZahradnΓk
<barney@cpan.org>β on board since v0.81.
β¨ SEE ALSO β¨
Net::Amazon::S3::Bucketβ bestie, go read this one next, it's the whole other half of the saga π¦
β¨ AUTHOR β¨
(again, because the credits said so π)
Branislav ZahradnΓk <barney@cpan.org>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2025 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. So like β go forth, remix it, ship it, slay. π«Ά
No cap, that's the actual license situation. Same terms as Perl 5, that's it, that's the vibe. β¨
and that's the whole page bestie!! π this one's from perl v5.40.1, dated 2025-12-07 β go forth and put things in buckets, i'm obsessed with you ππ
π
π¦ where this page comes from
- ubuntu resolute: libnet-amazon-s3-perl 0.992-1 Β· other
- ubuntu stonking: libnet-amazon-s3-perl 0.992-1 Β· other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms