The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

VERSION

version 0.60

SYNOPSIS

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

my $s3 = Net::Amazon::S3->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

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 Net::Amazon::S3::Client instead.

Development of this code happens here: http://github.com/pfig/net-amazon-s3/

Homepage for the project (just started) is at http://pfig.github.com/net-amazon-s3/

METHODS

new

Create a new S3 client object. Takes some arguments:

buckets

Returns undef on error, else hashref of results

add_bucket

Takes a hashref:

Returns 0 on failure, Net::Amazon::S3::Bucket object on success

bucket BUCKET

Takes a scalar argument, the name of the bucket you're creating

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

delete_bucket

Takes either a Net::Amazon::S3::Bucket object or a hashref containing

Returns false (and fails) if the bucket isn't empty.

Returns true if the bucket is successfully deleted.

list_bucket

List all keys in this bucket.

Takes a hashref of arguments:

MANDATORY

OPTIONAL

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:

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'. This is a convenience method, but may make multiple requests to S3 under the hood.

Takes the same arguments as list_bucket.

add_key

DEPRECATED. DO NOT USE

get_key

DEPRECATED. DO NOT USE

head_key

DEPRECATED. DO NOT USE

delete_key

DEPRECATED. DO NOT USE

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. 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:

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

SEE ALSO

Net::Amazon::S3::Bucket

AUTHOR

Pedro Figueiredo me@pedrofigueiredo.org

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Amazon Digital Services, Leon Brocard, Brad Fitzpatrick, Pedro Figueiredo.

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