rlauer6 / perl-amazon-s3

A portable client library for working with and managing Amazon S3 buckets and keys.
http://search.cpan.org/dist/Amazon-S3/
2 stars 7 forks source link

NAME

Amazon::S3 - A portable client library for working with and managing Amazon S3 buckets and keys.

Amazon::S3

SYNOPSIS

use Amazon::S3;

my $aws_access_key_id     = "Fill me in!";
my $aws_secret_access_key = "Fill me in too!";

my $s3 = Amazon::S3->new(
    {   aws_access_key_id     => $aws_access_key_id,
        aws_secret_access_key => $aws_secret_access_key,
        retry                 => 1
    }
);

my $response = $s3->buckets;

# create a bucket
my $bucket_name = $aws_access_key_id . '-net-amazon-s3-test';

my $bucket = $s3->add_bucket( { bucket => $bucket_name } )
    or die $s3->err . ": " . $s3->errstr;

# store a key with a content-type and some optional metadata
my $keyname = 'testing.txt';

my $value   = 'T';

$bucket->add_key(
    $keyname, $value,
    {   content_type        => 'text/plain',
        'x-amz-meta-colour' => 'orange',
    }
);

# copy an object
$bucket->copy_object(
  source => $source,
  key    => $new_keyname
);

# list keys in the bucket
$response = $bucket->list
    or die $s3->err . ": " . $s3->errstr;

print $response->{bucket}."\n";

for my $key (@{ $response->{keys} }) {
      print "\t".$key->{key}."\n";  
}

# delete key from bucket
$bucket->delete_key($keyname);

# delete multiple keys from bucket
$bucket->delete_keys([$key1, $key2, $key3]);

# delete bucket
$bucket->delete_bucket;

DESCRIPTION

This documentation refers to version 2.0.2.

Amazon::S3 provides a portable client interface to Amazon Simple Storage System (S3).

This module is rather dated, however with some help from a few contributors it has had some recent updates. Recent changes include implementations of:

Additionally, this module now implements Signature Version 4 signing, unit tests have been updated and more documentation has been added or corrected. Credentials are encrypted if you have encryption modules installed.

NEW!

The Amazon::S3 modules have been heavily refactored over the last few releases to increase maintainability and to add new features. New features include:

Comparison to Other Perl S3 Modules

Other implementations for accessing Amazon's S3 service include Net::Amazon::S3 and the Paws project. Amazon::S3 ostensibly was intended to be a drop-in replacement for Net:Amazon::S3 that "traded some performance in return for portability". That statement is no longer accurate as Amazon::S3 may have changed the interface in ways that might break your applications if you are relying on compatibility with Net::Amazon::S3.

However, Net::Amazon::S3 and Paws::S3 today, are dependent on Moose which may in fact level the playing field in terms of performance penalties that may have been introduced by recent updates to Amazon::S3. Changes to Amazon::S3 include the use of more Perl modules in lieu of raw Perl code to increase maintainability and stability as well as some refactoring. Amazon::S3 also strives now to adhere to best practices as much as possible.

Paws::S3 may be a much more robust implementation of a Perl S3 interface, however this module may still appeal to those that favor simplicity of the interface and a lower number of dependencies. The new Amazon::S3::BucketV2 module now provides access to nearly all of the main S3 API metods.

Below is the original description of the module.

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 sign up for an Amazon Web Services account, required to use this library and the S3 service, please visit the Amazon Web Services web site at http://www.amazonaws.com/.

You will be billed accordingly by Amazon when you use this module and must be responsible for these costs.

To learn more about Amazon's S3 service, please visit: http://s3.amazonaws.com/.

The need for this module arose from some work that needed to work with S3 and would be distributed, installed and used on many various environments where compiled dependencies may not be an option. Net::Amazon::S3 used XML::LibXML tying it to that specific and often difficult to install option. In order to remove this potential barrier to entry, this module is forked and then modified to use XML::SAX via XML::Simple.

LIMITATIONS AND DIFFERENCES WITH EARLIER VERSIONS

As noted, this module is no longer a drop-in replacement for Net::Amazon::S3 and has limitations and differences that may impact the use of this module in your applications. Additionally, one of the original intents of this fork of Net::Amazon::S3 was to reduce the number of dependencies and make it easy to install. Recent changes to this module have introduced new dependencies in order to improve the maintainability and provide additional features. Installing CPAN modules is never easy, especially when the dependencies of the dependencies are impossible to control and include may include XS modules.

METHODS AND SUBROUTINES

Unless otherwise noted methods will return an undef if an error occurs. You can get more information about the error by calling err() and errstr().

new

Create a new S3 client object. Takes some arguments:

signer

Sets or retrieves the signer object. API calls must be signed using your AWS credentials. By default, starting with version 0.54 the module will use Net::Amazon::Signature::V4 as the signer and instantiate a signer object in the constructor. Note however, that signers need your credentials and they will get stored by that class, making them susceptible to inadvertant exfiltration. You have a few options here:

region

Sets the region for the API calls. This will also be the default when instantiating the bucket object unless you pass the region parameter in the bucket method or use the verify_region flag that will always verify the region of the bucket using the get_location_constraint method.

default: us-east-1

buckets

buckets([verify-region])

Returns a reference to a hash containing the metadata for all of the buckets owned by the accout or (see below) or undef on error.

add_bucket

add_bucket(bucket-configuration)

bucket-configuration is a reference to a hash with bucket configuration parameters.

Note that since April of 2023, new buckets are created that block public access by default. If you attempt to set an ACL with public permissions the create operation will fail. To create a public bucket you must first create the bucket with private permissions, remove the public block and subsequently apply public permissions.

See "delete_public_access_block".

Returns a Amazon::S3::Bucket object on success or undef on failure.

bucket

bucket(bucket, [region])

bucket({ bucket => bucket-name, verify_region => boolean, region => region });

Takes a scalar argument or refernce to a hash of arguments.

You can pass the region or set verify_region indicating that you want the bucket constructor to detemine the bucket region.

If you do not pass the region or set the verify_region value, the region will be set to the default region set in your Amazon::S3 object.

See Amazon::S3::Bucket for a complete description of the bucket method.

delete_bucket

Takes either a Amazon::S3::Bucket object or a reference to a hash containing:

Returns a boolean indicating the success or failure of the API call. Check err or errstr for error messages.

Note from the Amazon's documentation

If a bucket is empty, you can delete it. After a bucket is deleted, the name becomes available for reuse. However, after you delete the bucket, you might not be able to reuse the name for various reasons.

For example, when you delete the bucket and the name becomes available for reuse, another AWS account might create a bucket with that name. In addition, some time might pass before you can reuse the name of a deleted bucket. If you want to use the same bucket name, we recommend that you don't delete the bucket.

delete_public_access_block

delete_public_access_block(bucket-obj)

Removes the public access block flag for the bucket.

dns_bucket_names

Set or get a boolean that indicates whether to use DNS bucket names.

default: true

err

Returns the last error. Usually this is the error code returned from an API call or a short message that the describes the error. Use errstr for a more descriptive explanation of the error condition.

errstr

Detailed error description.

list_bucket, list_bucket_v2

List keys in a bucket. Note that this method will only return max-keys. If you want all of the keys you should use list_bucket_all or list_bucket_all_v2.

See the note in the delimiter and max-keys descriptions below regarding how keys are counted against the max-keys value.

Takes a reference to a hash of arguments:

Returns undef on error and a reference to a hash of data on success:

The return value looks like this:

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

Each key is a reference to a hash 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
}

get_bucket_location

get_bucket_location(bucket-name)
get_bucket_locaiton(bucket-obj)

This is a convenience routines for the get_location_constraint() of the bucket object. This method will return the default region of 'us-east-1' when get_location_constraint() returns a null value.

my $region = $s3->get_bucket_location('my-bucket');

Starting with version 0.55, Amazon::S3::Bucket will call this get_location_constraint() to determine the region for the bucket. You can get the region for the bucket by using the region() method of the bucket object.

my $bucket = $s3->bucket('my-bucket');
my $bucket_region = $bucket->region;

get_logger

Returns the logger object. If you did not set a logger when you created the object then an instance of Amazon::S3::Logger is returned. You can log to STDERR using this logger. For example:

$s3->get_logger->debug('this is a debug message');

$s3->get_logger->trace(sub { return Dumper([$response]) });

list_bucket_all, list_bucket_all_v2

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.

_You are encouraged to use the newer list_bucket_all_v2 method._

list_object_versions

list_object_versions( args ) 

Returns metadata about all versions of the objects in a bucket. You can also use request parameters as selection criteria to return metadata about a subset of all the object versions.

This method will only return the raw result set and does not perform pagination or unravel common prefixes as do other methods like list_bucket. This may change in the future.

See https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectVersions.html for more information about the request parameters and the result body.

args is hash reference containing the following parameters:

err

The S3 error code for the last error encountered.

errstr

A human readable error string for the last error encountered.

error

The decoded XML string as a hash object of the last error.

last_response

Returns the last HTTP::Response object.

last_request

Returns the last HTTP::Request object.

level

Set the logging level.

default: error

turn_on_special_retry

Called to add extra retry codes if retry has been set

turn_off_special_retry

Called to turn off special retry codes when we are deliberately triggering them

ABOUT

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 certain environment variables.

For more on testing this module see README-TESTING.md

Consider using an S3 mocking service like minio or LocalStack if you want to create real tests for your applications or this module.

Here's bash script for testing using LocalStack

#!/bin/bash
# -*- mode: sh; -*-

BUCKET=net-amazon-s3-test-test 
ENDPOINT_URL=s3.localhost.localstack.cloud:4566

AMAZON_S3_EXPENSIVE_TESTS=1 \
AMAZON_S3_HOST=$ENDPOINT_URL \
AMAZON_S3_LOCALSTACK=1 \
AWS_ACCESS_KEY_ID=test \
AWS_ACCESS_SECRET_KEY=test  \
AMAZON_S3_DOMAIN_BUCKET_NAMES=1 make test 2>&1 | tee test.log

To run the tests...clone the project and build the software.

cd src/main/perl
./test.localstack

ADDITIONAL INFORMATION

LOGGING AND DEBUGGING

Additional debugging information can be output to STDERR by setting the level option when you instantiate the Amazon::S3 object. Levels are represented as a string. The valid levels are:

fatal
error
warn
info
debug
trace

You can set an optionally pass in a logger that implements a subset of the Log::Log4perl interface. Your logger should support at least these method calls. If you do not supply a logger the default logger (Amazon::S3::Logger) will be used.

get_logger()
fatal()
error()
warn()
info()
debug()
trace()
level()

At the trace level, every HTTP request and response will be output to STDERR. At the debug level information regarding the higher level methods will be output to STDERR. There currently is no additional information logged at lower levels.

S3 LINKS OF INTEREST

SUPPORT

Bugs should be reported via the CPAN bug tracker at

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Amazon-S3

For other issues, contact the author.

REPOSITORY

https://github.com/rlauer6/perl-amazon-s3

AUTHOR

Original author: Timothy Appnel tima@cpan.org

Current maintainer: Rob Lauer bigfoot@cpan.org

SEE ALSO

Amazon::S3::Bucket, Net::Amazon::S3

COPYRIGHT AND LICENCE

This module was initially based on Net::Amazon::S3 0.41, by Leon Brocard. Net::Amazon::S3 was based on example code from Amazon with this 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.

The software is released under the Artistic License. The terms of the Artistic License are described at http://www.perl.com/language/misc/Artistic.html. Except where otherwise noted, Amazon::S3 is Copyright 2008, Timothy Appnel, tima@cpan.org. All rights reserved.