AnyEvent-Net-Amazon-S3
view release on metacpan or search on metacpan
{
# Create a private key, then make it publicly readable with a short
# acl. Delete it at the end so we're back to having a single key in
# the bucket.
my $keyname2 = 'testing2.txt';
my $value = 'T2';
$bucket_obj->add_key(
$keyname2,
$value,
{ content_type => 'text/plain',
'x-amz-meta-colour' => 'blue',
acl_short => 'private',
}
);
is_request_response_code(
"http://$bucketname.s3.amazonaws.com/$keyname2",
403, "cannot access the private key" );
unlike_acl_allusers_read( $bucket_obj, $keyname2 );
ok( $bucket_obj->set_acl(
{ key => $keyname2, acl_short => 'public-read' }
)
);
is_request_response_code(
"http://$bucketname.s3.amazonaws.com/$keyname2",
200, "can access the publicly readable key" );
like_acl_allusers_read( $bucket_obj, $keyname2 );
$bucket_obj->delete_key($keyname2);
}
{
# Copy a key, keeping metadata
my $keyname2 = 'testing2.txt';
$bucket_obj->copy_key( $keyname2, "/$bucketname/$keyname" );
is_request_response_code(
"http://$bucketname.s3.amazonaws.com/$keyname2",
403, "cannot access the private key" );
# Overwrite, making publically readable
$bucket_obj->copy_key( $keyname2, "/$bucketname/$keyname",
{ acl_short => 'public-read' } );
sleep 1;
is_request_response_code(
"http://$bucketname.s3.amazonaws.com/$keyname2",
200, "can access the publicly readable key" );
# Now copy it over itself, making it private
$bucket_obj->edit_metadata( $keyname2, { short_acl => 'private' } );
is_request_response_code(
"http://$bucketname.s3.amazonaws.com/$keyname2",
403, "cannot access the private key" );
# Get rid of it, bringing us back to only one key
$bucket_obj->delete_key($keyname2);
# Expect a nonexistent key copy to fail
ok( !$bucket_obj->copy_key( "newkey", "/$bucketname/$keyname2" ),
"Copying a nonexistent key fails" );
}
# list keys in the bucket
$response = $bucket_obj->list
or die $s3->err . ": " . $s3->errstr;
is( $response->{bucket}, $bucketname );
is( $response->{prefix}, '' );
is( $response->{marker}, '' );
is( $response->{max_keys}, 1_000 );
is( $response->{is_truncated}, 0 );
my @keys = @{ $response->{keys} };
is( @keys, 1 );
my $key = $keys[0];
is( $key->{key}, $keyname );
# the etag is the MD5 of the value
is( $key->{etag}, 'b9ece18c950afbfa6b0fdbfa4ff731d3' );
is( $key->{size}, 1 );
is( $key->{owner_id}, $OWNER_ID );
is( $key->{owner_displayname}, $OWNER_DISPLAYNAME );
# You can't delete a bucket with things in it
ok( !$bucket_obj->delete_bucket() );
$bucket_obj->delete_key($keyname);
# now play with the file methods
my $readme_md5 = file_md5_hex('README');
my $readme_size = -s 'README';
$keyname .= "2";
$bucket_obj->add_key_filename(
$keyname, 'README',
{ content_type => 'text/plain',
'x-amz-meta-colour' => 'orangy',
}
);
$response = $bucket_obj->get_key($keyname);
is( $response->{content_type}, 'text/plain' );
like( $response->{value}, qr/AnyEvent-Net-Amazon-S3/ );
is( $response->{etag}, $readme_md5 );
is( $response->{'x-amz-meta-colour'}, 'orangy' );
is( $response->{content_length}, $readme_size );
unlink('t/README');
$response = $bucket_obj->get_key_filename( $keyname, undef, 't/README' );
( run in 1.076 second using v1.01-cache-2.11-cpan-39bf76dae61 )