Amazon-S3-Thin

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.20 2018-08-20T15:12:52Z
   - Features
   - [Environment] Abandon perl 5.8 (break B.C.)
   - [Signature v4] Make it work with signature v4. (use path style addressing for v4) (@showaltb)
   - [Signature v4] Add `region` option. It is required by default (break B.C.)  (@showaltb)
   - [Signature v4] Add `signature_version` option.  (@showaltb)
   - [Core] Remove `host` option (break B.C.)
   - [S3 api]Add API calls of `put_bucket` and `delete_bucket`
   - [Core] Suppresses uri encoding of the bucket key.  (@showaltb)
   - [Environment] make it work with Carton on travis
   - [Dev] Add `debug` option

   - Internal design
   - Add Credentials class to contain aws credentials
   - Add Resource class to express URI
   
0.16 2015-05-08T04:51:36Z
   - get_object() takes optinal headers

0.15 2015-04-26T06:44:48Z
   - add garu to contributors

README.md  view on Meta::CPAN

- `aws_secret_access_key` (**REQUIRED \[provider: credentials\]**) - an secret access key
 of your credentials.
- `version` (**OPTIONAL \[provider: metadata\]**) - version of metadata service to use, either 1 or 2.
[read more](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html)
- `role` (**OPTIONAL \[provider: metadata\]**) - IAM instance role to use, otherwise the first is selected
- `secure` - whether to use https or not. Default is 0 (http).
- `ua` - a user agent object, compatible with LWP::UserAgent.
Default is an instance of [LWP::UserAgent](https://metacpan.org/pod/LWP%3A%3AUserAgent).
- `signature_version` - AWS signature version to use. Supported values
are 2 and 4. Default is 4.
- `debug` - debug option. Default is 0 (false). 
If set 1, contents of HTTP request and response are shown on stderr
- `virtual_host` - whether to use virtual-hosted style request format. Default is 0 (path-style).

# ACCESSORS

The following accessors are provided. You can use them to get/set your
object's attributes.

## secure

Whether to use https (1) or http (0) when connecting to S3.

## ua

The user agent used internally to perform requests and return responses.
If you set this attribute, please make sure you do so with an object
compatible with [LWP::UserAgent](https://metacpan.org/pod/LWP%3A%3AUserAgent) (i.e. providing the same interface).

## debug

Debug option.

# Operations on Buckets

## put\_bucket( $bucket \[, $headers\])

**Arguments**:

- 1. bucket - a string with the bucket

lib/Amazon/S3/Thin.pm  view on Meta::CPAN

        delete $self->{aws_access_key_id};
        delete $self->{aws_secret_access_key};
        delete $self->{aws_session_token};
    }
    delete $self->{credential_provider};

    bless $self, $class;

    $self->secure(0)                unless defined $self->secure;
    $self->ua($self->_default_ua)   unless defined $self->ua;
    $self->debug(0)                 unless defined $self->debug;
    $self->virtual_host(0)          unless defined $self->virtual_host;

    $self->{signature_version} = 4  unless defined $self->{signature_version};
    if ($self->{signature_version} == 4 && ! $self->{region}) {
        croak "Please set region when you use signature v4";
    }

    $self->{signer} = $self->_load_signer($self->{signature_version});
    return $self;
}

lib/Amazon/S3/Thin.pm  view on Meta::CPAN


sub secure {
    my $self = shift;
    if (@_) {
        $self->{secure} = shift;
    } else {
        return $self->{secure};
    }
}

sub debug {
    my $self = shift;
    if (@_) {
        $self->{debug} = shift;
    } else {
        return $self->{debug};
    }
}

sub ua {
    my $self = shift;
    if (@_) {
        $self->{ua} = shift;
    } else {
        return $self->{ua};
    }

lib/Amazon/S3/Thin.pm  view on Meta::CPAN

    my $self = shift;
    if (@_) {
        $self->{virtual_host} = shift;
    } else {
        return $self->{virtual_host};
    }
}

sub _send {
    my ($self, $request) = @_;
    warn "[Request]\n" , $request->as_string if $self->{debug};
    my $response = $self->ua->request($request);
    warn "[Response]\n" , $response->as_string if $self->{debug};
    return $response;
}

# API calls

sub get_object {
    my ($self, $bucket, $key, $headers) = @_;
    my $request = $self->_compose_request('GET', $self->_resource($bucket, $key), $headers);
    return $self->_send($request);
}

lib/Amazon/S3/Thin.pm  view on Meta::CPAN

=item * C<role> (B<OPTIONAL [provider: metadata]>) - IAM instance role to use, otherwise the first is selected

=item * C<secure> - whether to use https or not. Default is 0 (http).

=item * C<ua> - a user agent object, compatible with LWP::UserAgent.
Default is an instance of L<LWP::UserAgent>.

=item * C<signature_version> - AWS signature version to use. Supported values
are 2 and 4. Default is 4.

=item * C<debug> - debug option. Default is 0 (false). 
If set 1, contents of HTTP request and response are shown on stderr

=item * C<virtual_host> - whether to use virtual-hosted style request format. Default is 0 (path-style).

=back

=head1 ACCESSORS

The following accessors are provided. You can use them to get/set your
object's attributes.

lib/Amazon/S3/Thin.pm  view on Meta::CPAN

=head2 secure

Whether to use https (1) or http (0) when connecting to S3.

=head2 ua

The user agent used internally to perform requests and return responses.
If you set this attribute, please make sure you do so with an object
compatible with L<LWP::UserAgent> (i.e. providing the same interface).

=head2 debug

Debug option.

=head1 Operations on Buckets

=head2 put_bucket( $bucket [, $headers])

B<Arguments>:

=over 2

t/01_accessors.t  view on Meta::CPAN


    $arg = +{
        %crd
    };

    $client = Amazon::S3::Thin->new($arg);

    is $client->secure() , 0;
}

diag "test debug()";
{
    $arg = +{
        %crd,
        debug => 1,
    };

    $client = Amazon::S3::Thin->new($arg);

    is $client->debug() , 1;

    $arg = +{
        %crd
    };

    $client = Amazon::S3::Thin->new($arg);

    is $client->debug() , 0;

    $client->debug(1);
    is $client->debug() , 1;
}

diag "test ua()";
{
    $arg = +{
        %crd,
    };

    $client = Amazon::S3::Thin->new($arg);

xt/90_functional.t  view on Meta::CPAN

use warnings;
use Test::More;
use Config::Tiny;

use Amazon::S3::Thin;

if (!$ENV{EXTENDED_TESTING}) {
    plan skip_all => 'Skip functional test because it would call S3 APIs and charge real money. $ENV{EXTENDED_TESTING} is not set.';
}

my $debug = 1;
my $use_https = 1;

my $config_file = $ENV{HOME} . '/.aws/credentials';
my $profile = 's3thin';
my $bucket = $ENV{TEST_S3THIN_BUCKET} || 'dqneo-private-test';
my $region = 'ap-northeast-1';
my $host = "s3.$region.amazonaws.com";

my $crd = Config::Tiny->read($config_file)->{$profile};

my $arg = {
    %$crd,
    region => $region,
    secure => $use_https,
    debug => $debug,
};
my $protocol = $use_https ? 'https' : 'http';
my $client = Amazon::S3::Thin->new($arg);

my $key =  "dir/s3test.txt";
my $body = "hello amazon s3";

# 0. HEAD to check existance
# 1. DELETE
# 2. PUT to create

xt/91_functional.t  view on Meta::CPAN

use warnings;
use Test::More;
use Config::Tiny;

use Amazon::S3::Thin;

if (!$ENV{EXTENDED_TESTING}) {
    plan skip_all => 'Skip functional test because it would call S3 APIs and charge real money. $ENV{EXTENDED_TESTING} is not set.';
}

my $debug = 1;
my $use_https = 1;

my $config_file = $ENV{HOME} . '/.aws/credentials';
my $profile = 's3thin';
my $bucket = $ENV{TEST_S3THIN_BUCKET} || 'dqneo-private-test';

my $crd = Config::Tiny->read($config_file)->{$profile};

sub test_with_existing_bucket {
    my $crd = shift;

xt/91_functional.t  view on Meta::CPAN

    # because in signature v4 we must know the region of a bucket before accessing it.
    my $region = 'ap-northeast-1';

    my %opt = (
        aws_access_key_id => $crd->{aws_access_key_id},
        aws_secret_access_key => $crd->{aws_secret_access_key},
        secure => $use_https,
        signature_version => $arg->{signature_version},
        region => $region,
#        use_path_style => $arg->{use_path_style},
        debug => $debug,
    );

    my $s3client = Amazon::S3::Thin->new(\%opt);
    ok $s3client, 'new';

    # These bucket and key suppose to exists beforehand.
    my $key = 'hello.txt';

    my $response;

xt/91_functional.t  view on Meta::CPAN

    my $crd = shift;
    my $arg = shift;
    diag(' region ' . $arg->{region});
    my %opt = (
        aws_access_key_id => $crd->{aws_access_key_id},
        aws_secret_access_key => $crd->{aws_secret_access_key},
        secure => $use_https,
        signature_version => $arg->{signature_version},
        region => $arg->{region},
#        use_path_style => $arg->{use_path_style},
        debug => $debug,
    );
    my $s3client = Amazon::S3::Thin->new(\%opt);
    my $bucket = 's3thin-' . $arg->{region} . $arg->{signature_version}  . time();
    my $response;
    $response = $s3client->put_bucket($bucket);
    is $response->code , 200 , 'create new bucket';

    my $key = 'hobbit.txt';
    my $content = "In a hole in the ground there lived a hobbit.\n";
    $response = $s3client->put_object($bucket, $key, $content);

xt/92_presigned_post.t  view on Meta::CPAN

use Config::Tiny;
use File::Basename qw(basename);
use LWP::UserAgent;

use Amazon::S3::Thin;

if (!$ENV{EXTENDED_TESTING}) {
    plan skip_all => 'Skip functional test because it would call S3 APIs and charge real money. $ENV{EXTENDED_TESTING} is not set.';
}

my $debug = 1;
my $use_https = 1;

my $config_file = $ENV{HOME} . '/.aws/credentials';
my $profile = 's3thin';
my $bucket = $ENV{TEST_S3THIN_BUCKET} || 'dqneo-private-test';
my $filename = 'xt/upload.txt';
my $content = do {
    open my $fh, '<', $filename or die $!;
    local $/; <$fh>;
};

my $crd = Config::Tiny->read($config_file)->{$profile};

my $arg = {
    %$crd,
    region => 'ap-northeast-1',
    secure => $use_https,
    debug => $debug,
};
my $client = Amazon::S3::Thin->new($arg);
my $ua = LWP::UserAgent->new;

sub upload {
    my $presigned = shift;
    return $ua->post(
        $presigned->{url},
        Content_Type => 'multipart/form-data',
        Content      => [

xt/93_session_token.t  view on Meta::CPAN

=cut

if (!$ENV{EXTENDED_TESTING}) {
    plan skip_all => 'Skip functional test because it would call S3 APIs and charge real money. $ENV{EXTENDED_TESTING} is not set.';
}

unless ($ENV{AWS_ACCESS_KEY_ID} && $ENV{AWS_SECRET_ACCESS_KEY} && $ENV{AWS_SESSION_TOKEN}) {
    die "AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN are not set";
}

my $debug = 1;
my $use_https = 1;

my $bucket = $ENV{TEST_S3THIN_BUCKET} || 'dqneo-private-test';

my $arg = {
    aws_access_key_id     => $ENV{AWS_ACCESS_KEY_ID},
    aws_secret_access_key => $ENV{AWS_SECRET_ACCESS_KEY},
    aws_session_token     => $ENV{AWS_SESSION_TOKEN},
    region                => 'ap-northeast-1',
    secure                => $use_https,
    debug                 => $debug,
};
my $client = Amazon::S3::Thin->new($arg);

subtest 'put and get and delete object' => sub {
    my $key =  "dir/s3test.txt";
    my $body = "hello amazon s3";

    my $res;
    $res = $client->put_object($bucket, $key, $body);
    is $res->code, 200;

xt/94_virtual_host.t  view on Meta::CPAN

use warnings;
use Test::More;
use Config::Tiny;

use Amazon::S3::Thin;

if (!$ENV{EXTENDED_TESTING}) {
    plan skip_all => 'Skip functional test because it would call S3 APIs and charge real money. $ENV{EXTENDED_TESTING} is not set.';
}

my $debug = 1;
my $use_https = 1;

my $config_file = $ENV{HOME} . '/.aws/credentials';
my $profile = 's3thin';
my $bucket = $ENV{TEST_S3THIN_BUCKET} || 'dqneo-private-test';
my $region = 'ap-northeast-1';
my $host = "$bucket.s3.amazonaws.com";

my $crd = Config::Tiny->read($config_file)->{$profile};

my $arg = {
    %$crd,
    region       => $region,
    secure       => $use_https,
    debug        => $debug,
    virtual_host => 1,
};
my $protocol = $use_https ? 'https' : 'http';
my $client = Amazon::S3::Thin->new($arg);

my $key =  "dir/s3test.txt";
my $body = "hello amazon s3";

# 0. HEAD to check existance
# 1. DELETE

xt/95_delete_multiple_objects.t  view on Meta::CPAN

use warnings;
use Test::More;
use Config::Tiny;

use Amazon::S3::Thin;

if (!$ENV{EXTENDED_TESTING}) {
    plan skip_all => 'Skip functional test because it would call S3 APIs and charge real money. $ENV{EXTENDED_TESTING} is not set.';
}

my $debug = 1;
my $use_https = 1;

my $config_file = $ENV{HOME} . '/.aws/credentials';
my $profile = 's3thin';
my $bucket = $ENV{TEST_S3THIN_BUCKET} || 'dqneo-private-test';
my $region = 'ap-northeast-1';
my $host = "s3.$region.amazonaws.com";

my $crd = Config::Tiny->read($config_file)->{$profile};

my $arg = {
    %$crd,
    region => $region,
    secure => $use_https,
    debug  => $debug,
};
my $protocol = $use_https ? 'https' : 'http';
my $client = Amazon::S3::Thin->new($arg);

my $key1 =  "dir/s3test_1.txt";
my $key2 =  "dir/s3test_2.txt";
my $body = "hello amazon s3";

my $res;
my $req;



( run in 0.803 second using v1.01-cache-2.11-cpan-49f99fa48dc )