AWS-S3
view release on metacpan or search on metacpan
t/010_basic.t view on Meta::CPAN
#!/usr/bin/perl -w
use strict;
use warnings 'all';
use Test::More;
use Data::Dumper;
use FindBin qw/ $Bin /;
use lib "$Bin/../lib";
use Carp 'confess';
$SIG{__DIE__} = \&confess;
plan skip_all => '$ENV{AWS_ACCESS_KEY_ID} && $ENV{AWS_SECRET_ACCESS_KEY} must both be defined to run this test'
unless( $ENV{AWS_ACCESS_KEY_ID} && $ENV{AWS_SECRET_ACCESS_KEY} );
use_ok('AWS::S3');
my $s3 = AWS::S3->new(
access_key_id => $ENV{AWS_ACCESS_KEY_ID},
secret_access_key => $ENV{AWS_SECRET_ACCESS_KEY},
session_token => $ENV{AWS_SESSION_TOKEN},
region => $ENV{AWS_REGION},
);
isa_ok $s3->ua, 'LWP::UserAgent';
cleanup();
ok my $owner = $s3->owner(), "s3.owner returns a value";
isa_ok $owner, 'AWS::S3::Owner';
ok $owner->id, 'owner.id';
ok $owner->display_name, 'owner.display_name';
my $bucket_name = $ENV{AWS_TEST_BUCKET} || "aws-s3-test-" . int(rand() * 1_000_000) . '-' . time() . "-foo";
ok my $bucket = $s3->add_bucket( name => $bucket_name, location => 'us-east-1' ), "created bucket '$bucket_name'";
#exit;
if( $bucket )
{
if(0) {
# Try cloudfront integration if we've got it:
eval { require AWS::CloudFront; require AWS::CloudFront::S3Origin; };
die $@ if $@;
unless( $@ )
{
my $cf = AWS::CloudFront->new(
access_key_id => $s3->access_key_id,
secret_access_key => $s3->secret_access_key,
);
my $dist = $cf->add_distribution(
Origin => AWS::CloudFront::S3Origin->new(
DNSName => $bucket->name . '.s3.amazonaws.com',
)
);
$bucket->enable_cloudfront_distribution( $dist );
}# end unless()
}
# my $acl = $bucket->acl;
# ok $bucket->acl( 'private' ), 'set bucket.acl to private';
# is $acl, $bucket->acl, 'get bucket.acl returns private';
# ok $bucket->location_constraint( 'us-east-1' ), 'set bucket.location_constraint to us-east-1';
# is $bucket->location_constraint, 'us-east-1', 'get bucket.location returns us-east-1';
# is $s3->bucket($bucket->name)->location_constraint, 'us-east-1', 'get bucket.location returns us-east-1 second time';
# is $bucket->policy, '', 'get bucket.policy returns empty string';
my $test_str = "This is the original value right here!"x20;
my $filename = 'foo/bar.txt';
ADD_FILE: {
my $file = $bucket->add_file(
key => $filename,
contents => \$test_str
);
ok( $file, 'bucket.add_file() works' );
unlike $file->etag, qr("), 'file.etag does not contain any double-quotes (")';
};
GET_FILE: {
ok my $file = $bucket->file($filename), 'bucket.file(filename) works';
is ${ $file->contents }, $test_str, 'file.contents is correct';
};
ADD_FILE_WITH_CODE: {
my $text = "This is the content"x4;
ok $bucket->add_file(
key => 'code/test.txt',
contents => sub { return \$text }
), 'add file with code contents worked';
ok my $file = $bucket->file('code/test.txt'), "got file back from bucket";
is ${$file->contents}, $text, "file.contents on code is correct";
$file->contents( sub { return \uc($text) } );
is ${$file->contents}, uc($text), "file.contents on code is correct after update";
$file->delete;
};
# Set contents:
SET_CONTENTS: {
my $new_contents = "This is the updated value"x10;
ok my $file = $bucket->file($filename), 'bucket.file(filename) works';
$file->contents( \$new_contents );
# Now check it:
is ${$bucket->file($filename)->contents}, $new_contents, "set file.contents works";
# use alternative update method
$new_contents = 'More new content';
$file->update( contents => \$new_contents );
is ${$bucket->file($filename)->contents}, $new_contents, "set file.update works";
};
DELETE_FILE: {
eval { $bucket->delete };
ok $@, 'bucket.delete fails when bucket is not empty.';
like $@, qr/BucketNotEmpty/, 'error looks like BucketNotEmpty';
ok $bucket->file($filename)->delete, 'file.delete';
ok ! $bucket->file($filename), 'file no longer exists in bucket';
};
ADD_MANY_FILES: {
my %info = ( );
# Add the files:
( run in 0.941 second using v1.01-cache-2.11-cpan-39bf76dae61 )