Amazon-S3Curl-PurePerl

 view release on metacpan or  search on metacpan

lib/Amazon/S3Curl/PurePerl.pm  view on Meta::CPAN




sub download_cmd {
    my ($self) = @_;
    my $args = $self->_req('GET');
    push @$args, ( "-o", $self->local_file );
    return $args;
}

sub upload_cmd {
    my ($self) = @_;
    my $url = $self->url;
    #trailing slash for upload means curl will plop on the filename at the end, ruining the hash signature.
    if ( $url =~ m|/$| ) {
        my $file_name = ( File::Spec->splitpath( $self->local_file ) )[-1];
        $url .= $file_name;
    }
    my $args = $self->_req('PUT',$url);
    splice( @$args, $#$args, 0, "-T", $self->local_file );
    return $args;
}

sub delete_cmd {

lib/Amazon/S3Curl/PurePerl.pm  view on Meta::CPAN

    my $args = $self->$meth;
    log_info { "running " . join( " ", @_ ) } @$args;
    capture(@$args);
    return 1;
}

sub download {
    return shift->_exec("download");
}

sub upload {
    return shift->_exec("upload");
}

sub delete {
    return shift->_exec("delete");
}

sub head {
    return shift->_exec("head");
}

sub _local_file_required {
    my $method = shift;
    sub {
        die "parameter local_file required for $method"
          unless shift->local_file;
    };
}

before download => _local_file_required('download');
before upload => _local_file_required('upload');
1;
__END__

=head1 NAME

Amazon::S3Curl::PurePerl - Pure Perl s3 helper/downloader.

=head1 VERSION

version 0.054

lib/Amazon/S3Curl/PurePerl.pm  view on Meta::CPAN

=head1 PARAMETERS

=over

=item aws_access_key ( required )

=item aws_secret_key ( required )

=item url ( required )

This is the (url to download|upload to|delete).
It should be a relative path with the bucket name, and whatever pseudopaths you want.

For upload:
Left with a trailing slash, it'll DWYM, curl style.

=item local_file

This is the (path to download to|file to upload).

=back


=head1 METHODS

=head2 new

Constructor, provided by Moo.


=head2 download

    $s3curl->download;

download url to local_file.

=head2 upload

    $s3curl->upload;

Upload local_file to url.

=head2 delete

    $s3curl->delete;

Delete url.

=head2 delete_cmd

=head2 download_cmd

=head2 upload_cmd

Just get the command to execute in the form of an arrayref, don't actually execute it:

    my $cmd = $s3curl->download_cmd;
    system(@$cmd);

=head2 url_exists

Check to see if a given url returns a 404 or 200. return 1 if 200, return 0 if 404, die otherwise.

xt/s3curl.t  view on Meta::CPAN

use strict;
use warnings FATAL => 'all';
use Test::More tests => 15;
use Amazon::S3Curl::PurePerl;
use File::Temp;
use File::Spec;
use IPC::System::Simple qw[ system ];
my $download_file = File::Temp->new;
my $upload_file = File::Temp->new;
$upload_file->autoflush(1);
die 'you need $ENV{AWS_ACCESS_KEY} and $ENV{AWS_SECRET_KEY}  and $ENV{S3_TEST_BUCKET} for the live tests.'
  unless $ENV{AWS_ACCESS_KEY} && $ENV{AWS_SECRET_KEY} && $ENV{S3_TEST_BUCKET};

my $filename = (File::Spec->splitpath($upload_file))[-1];

my $test_string = "testo\ntesto2";
print $upload_file $test_string;

my %pp_args = (
    aws_access_key => $ENV{AWS_ACCESS_KEY},
    aws_secret_key => $ENV{AWS_SECRET_KEY},
    local_file     => "$upload_file",
    url            => "/$ENV{S3_TEST_BUCKET}/$filename"
);

ok my $uploader = Amazon::S3Curl::PurePerl->new(%pp_args),
  "instantiated uploader";

ok !$uploader->url_exists, "file does not exist yet.";
ok $uploader->upload, "uploaded file";
ok $uploader->url_exists, "file exists.";

ok my $downloader = Amazon::S3Curl::PurePerl->new(
    %pp_args, 
    local_file => "$download_file"
  ),
  "Amazon::S3Curl::PurePerl instantiated";
ok $downloader->download;
ok(  system( @{ $downloader->download_cmd } ) == 0,"System call returned 0" );
{
    local $/ = undef;
    open(my $fh_left, "<", $download_file ) or die "$@ $!";
    open(my $fh_right, "<", $upload_file) or die "$@ $!";
    my $str = <$fh_left>;
    my $str2 = <$fh_right>;
    is( $str, $str2, "downloaded file matches uploaded file" );
};

ok $downloader->delete, "delete remote file";

ok my $should_die = Amazon::S3Curl::PurePerl->new(%pp_args, local_file => undef );
eval {
    $should_die->download;
    fail "should've died";
};
ok $@, "died without local_file param.";
my $ret1 = system(@{ $uploader->upload_cmd });
my $ret2 = system(@{ $downloader->download_cmd });
ok $downloader->delete, "delete remote file";
ok !$downloader->url_exists, "file is gone (404)";
diag $ret1;
ok !$ret1,"uploaded using upload_cmd => system";
ok !$ret2,"downloaded using download_cmd => system";



( run in 0.649 second using v1.01-cache-2.11-cpan-b16cb0d3907 )