Tie::Amazon::S3 - tie Amazon S3 buckets to Perl hashes


Tie-Amazon-S3 documentation Contained in the Tie-Amazon-S3 distribution.

Index


Code Index:

NAME

Top

Tie::Amazon::S3 - tie Amazon S3 buckets to Perl hashes

VERSION

Top

Version 0.02

SYNOPSIS

Top

    use Tie::Amazon::S3;

    tie my %bucket, 'Tie::Amazon::S3', 'my-amazon-aws-id',
        'my-amazon-aws-secret', 'my-amazon-s3-bucket';

    # use it as you would any Perl hash
    $bucket{testfile} = 'this is a testfile';
    print $bucket{testfile};
    ...

METHODS

Top

TIEHASH

Constructor for tying a Net::Amazon::S3 object to a Perl hash.

STORE

Store some scalar into an S3 bucket (Perl hash) key.

FETCH

Fetch an S3 bucket key.

EXISTS

Check if a given key exists in the bucket.

DELETE

Delete a key from the bucket.

CLEAR

Clear the bucket of keys.

SCALAR

Get the count of keys in the bucket.

FIRSTKEY

Get the first key iterator.

NEXTKEY

Get the next key iterator.

s3_croak

Croak to the module user if S3 errs.

AUTHOR

Top

Zak B. Elep, <zakame at cpan.org>

BUGS

Top

The tests cover a few bases, but being not version 1.00 yet, there's going to be some bugs.

Please report any bugs or feature requests to

    C<bug-tie-amazon-s3 at rt.cpan.org>,

or through the web interface at

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

I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Tie::Amazon::S3




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tie-Amazon-S3

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Tie-Amazon-S3

* CPAN Ratings

http://cpanratings.perl.org/d/Tie-Amazon-S3

* Search CPAN

http://search.cpan.org/dist/Tie-Amazon-S3

* The Net::Amazon::S3 module

Net::Amazon::S3

* Amazon's Simple Storage Service

http://s3.amazonaws.com

ACKNOWLEDGEMENTS

Top

Leon Brocard, Brad Fitzpatrick, and the AWS programmers for their work.

COPYRIGHT & LICENSE

Top


Tie-Amazon-S3 documentation Contained in the Tie-Amazon-S3 distribution.
package Tie::Amazon::S3;

use warnings;
use strict;

use Carp 'croak';
use Net::Amazon::S3;

our $VERSION = '0.02';
our @ISA = qw(Net::Amazon::S3);

sub TIEHASH {
    my ( $class, $id, $key, $bucket ) = @_;
    my $self = $class->SUPER::new(
        {
            aws_access_key_id => $id,
            aws_secret_access_key => $key,
        },
    );
    $self->{BUCKET} = $self->bucket($bucket);
    my %key = ();
    my $list = $self->{BUCKET}->list_all or $self->s3_croak;
    map { $key{$_} => sub { $self->{BUCKET}->get_key($_)
                                or $self->s3_croak; } }
        @{ $list->{keys} };
    $self->{KEYS} = \%key;
    bless $self => $class;
}

sub STORE {
    my ( $self, $key, $value ) = @_;
    $self->{BUCKET}->add_key( $key, $value ) or $self->s3_croak;
    $self->{KEYS}->{$key} = sub { $self->{BUCKET}->get_key($key)
                              or $self->s3_croak };
}

sub FETCH {
    my ( $self, $key ) = @_;
    if ( exists $self->{KEYS}->{$key} ) {
            $self->{KEYS}->{$key}->()->{value};
    } else {
        return undef;
    }
}

sub EXISTS {
    my ( $self, $key ) = @_;
    exists $self->{KEYS}->{$key};
}

sub DELETE {
    my ( $self, $key ) = @_;
    # emulate native delete, returning whatever FETCH returned for this
    # key
    my $value = $self->FETCH( $key );
    $self->{BUCKET}->delete_key( $key ) or $self->s3_croak;
    my $deleted = delete $self->{KEYS}->{$key};
    return $value;
}

sub CLEAR {
    my ( $self ) = shift;
    foreach my $key ( keys %{ $self->{KEYS} } ) {
        $self->DELETE($key);
    }
}

sub SCALAR {
    my ( $self ) = shift;
    return scalar keys %{ $self->{KEYS} };
}

sub FIRSTKEY { each %{ $_[0]->{KEYS} } }

sub NEXTKEY { each %{ $_[0]->{KEYS} } }

sub s3_croak { croak $_[0]->err, ': ', $_[0]->errstr };


1; # End of Tie::Amazon::S3