Amazon-S3-Thin
view release on metacpan or search on metacpan
t/04_request_v2.t
t/05_presigned_post.t
t/06_request_virtual_host.t
t/07_copy_200_error.t
xt/90_functional.t
xt/91_functional.t
xt/92_presigned_post.t
xt/93_session_token.t
xt/94_virtual_host.t
xt/95_delete_multiple_objects.t
xt/upload.txt
META.yml
For more information, please refer to
[Amazon's documentation for COPY](http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html).
## put\_object( $bucket, $key, $content \[, $headers\] )
**Arguments**:
- 1. bucket - a string with the destination bucket
- 2. key - a string with the destination key
- 3. content - a string with the content to be uploaded
- 4. headers (**optional**) - hashref with extra header information
**Returns**: an [HTTP::Response](https://metacpan.org/pod/HTTP%3A%3AResponse) object for the request.
The PUT operation adds an object to a bucket. Amazon S3 never adds partial
objects; if you receive a success response, Amazon S3 added the entire
object to the bucket.
For more information, please refer to
[Amazon's documentation for PUT](http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html).
- 1. bucket (_string_) - a string with the destination bucket
- 2. key (_string_) - a string with the destination key
- 3. fields (_arrayref_) - an arrayref of key/value pairs to prefilled form fields to build on top of
- 4. conditions (_arrayref_) - an arrayref of condition (arrayref or hashref) to include in the policy
- 5. expires\_in (_number_) - a number of seconds from the current time before expiring presigned url
**Returns**: a hashref with two elements `url` and `fields`. `url` is the url to post to. `fields` is an arrayref
filled with the form fields and respective values to use when submitting the post. (You must follow the order of `fields`)
This method generates presigned url for uploading a file to Amazon S3 using HTTP POST.
The original implementation from boto3, this was transplanted referencing [S3Client.generate\_presigned\_post()](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.generate_presigned_post).
Note: this method is supported only signature v4.
This is an example of generating a presigned url and uploading `test.txt` file.
In this case, you can set the object metadata `x-amz-meta-foo` with any value and the uploading size is limited to 1MB.
my $presigned = $s3->generate_presigned_post('my.bucket', 'my/key.ext', [
'x-amz-meta-foo' => 'bar',
], [
['starts-with' => '$x-amz-meta-foo', ''],
['content-length-range' => 1, 1024*1024],
], 24*60*60);
my $ua = LWP::UserAgent->new;
my $res = $ua->post(
lib/Amazon/S3/Thin.pm view on Meta::CPAN
B<Arguments>:
=over 4
=item 1. bucket - a string with the destination bucket
=item 2. key - a string with the destination key
=item 3. content - a string with the content to be uploaded
=item 4. headers (B<optional>) - hashref with extra header information
=back
B<Returns>: an L<HTTP::Response> object for the request.
The PUT operation adds an object to a bucket. Amazon S3 never adds partial
objects; if you receive a success response, Amazon S3 added the entire
object to the bucket.
lib/Amazon/S3/Thin.pm view on Meta::CPAN
=item 4. conditions (I<arrayref>) - an arrayref of condition (arrayref or hashref) to include in the policy
=item 5. expires_in (I<number>) - a number of seconds from the current time before expiring presigned url
=back
B<Returns>: a hashref with two elements C<url> and C<fields>. C<url> is the url to post to. C<fields> is an arrayref
filled with the form fields and respective values to use when submitting the post. (You must follow the order of C<fields>)
This method generates presigned url for uploading a file to Amazon S3 using HTTP POST.
The original implementation from boto3, this was transplanted referencing L<< S3Client.generate_presigned_post()|https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.generate_presigned_post >>.
Note: this method is supported only signature v4.
This is an example of generating a presigned url and uploading C<test.txt> file.
In this case, you can set the object metadata C<x-amz-meta-foo> with any value and the uploading size is limited to 1MB.
my $presigned = $s3->generate_presigned_post('my.bucket', 'my/key.ext', [
'x-amz-meta-foo' => 'bar',
], [
['starts-with' => '$x-amz-meta-foo', ''],
['content-length-range' => 1, 1024*1024],
], 24*60*60);
my $ua = LWP::UserAgent->new;
my $res = $ua->post(
lib/Amazon/S3/Thin/Signer/V2.pm view on Meta::CPAN
use Carp;
use Digest::HMAC_SHA1;
use MIME::Base64 ();
use HTTP::Date ();
my $AMAZON_HEADER_PREFIX = 'x-amz-';
# reserved subresources such as acl or torrent
our @ordered_subresources = qw(
acl delete lifecycle location logging notification partNumber policy
requestPayment torrent uploadId uploads versionId versioning versions
website
);
sub new {
my ($class, $credentials, $host) = @_;
if (ref($credentials) ne 'Amazon::S3::Thin::Credentials') {
croak "credentials object is not given."
}
my $self = {
credentials => $credentials,
t/02_signer_v2.t view on Meta::CPAN
is(
$signer->string_to_sign($verb,$path,$hdr),
$string_to_sign,
'string to sign'
);
my $sig = $signer->calculate_signature($verb, $path, $hdr);
is $sig, 'lx3byBScXR6KzyMaifNkardMwNk=', "puppy delete (DELETE)";
}
{
diag "test Amazon example upload";
my $verb = "PUT";
my $date = "Tue, 27 Mar 2007 21:06:08 +0000";
# TODO: ports should be stripped from the path for signing
# my $path = "static.johnsmith.net:8080/db-backup.dat.gz";
my $path = "static.johnsmith.net/db-backup.dat.gz";
my $user_agent = "curl/7.15.5";
my $x_amz_acl = "public-read";
my $content_type = "application/x-download";
my $content_md5 = "4gJE4saaMU4BqNR0kLY+lw==";
t/02_signer_v2.t view on Meta::CPAN
$hdr->header("Content-Disposition", $content_disposition);
$hdr->header("Content-Encoding", $content_encoding);
$hdr->header("Content-Length", $content_length);
is(
$signer->string_to_sign($verb,$path,$hdr),
$string_to_sign,
'string to sign'
);
my $sig = $signer->calculate_signature($verb, $path, $hdr);
is $sig, 'ilyl83RwaSoYIEdixDQcA4OnAnc=', "puppy upload (PUT)";
}
{
diag "test Amazon example list buckets";
my $verb = "GET";
my $date = "Wed, 28 Mar 2007 01:29:59 +0000";
my $path = "";
my $string_to_sign = "$verb\n\n\n$date\n/$path";
xt/92_presigned_post.t view on Meta::CPAN
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 => [
@{$presigned->{fields}},
file => [$filename],
],
);
}
subtest 'upload with content-type and metadata' => sub {
my $key = 'upload.txt';
my $presigned = $client->generate_presigned_post($bucket, $key, [
'Content-Type' => 'image/png',
'x-amz-meta-foo' => 'bar',
], [
['starts-with' => '$x-amz-meta-foo', ''],
['starts-with' => '$Content-Type', 'image/'],
]);
my $res = upload($presigned);
is $res->code, 204, 'upload via presigned url';
$res = $client->get_object($bucket, $key);
is $res->code, 200, 'get uploaded object';
is $res->content, $content;
is $res->header('content-type'), 'image/png';
is $res->header('x-amz-meta-foo'), 'bar';
$res = $client->delete_object($bucket, $key);
is $res->code, 204, 'delete uploaded object';
};
subtest 'upload with filename foo-${filename}' => sub {
my $presigned = $client->generate_presigned_post($bucket, 'foo-${filename}', [], []);
my $res = upload($presigned);
is $res->code, 204, 'upload via presigned url';
my $key = 'foo-' . basename($filename);
$res = $client->get_object($bucket, $key);
is $res->code, 200, 'get uploaded object';
is $res->content, $content;
$res = $client->delete_object($bucket, $key);
is $res->code, 204, 'delete uploaded object';
};
subtest 'allowable size for uploading is restricted' => sub {
my $key = 'upload.txt';
my $presigned = $client->generate_presigned_post($bucket, $key, [], [
# allow a file size from 0 to 1 byte
['content-length-range', 0, 1],
]);
my $res = upload($presigned);
is $res->code, 400, 'too large';
$res = $client->head_object($bucket, $key);
ok $res->is_error, 'file is not uploaded';
};
subtest 'upload after the expiration date' => sub {
my $key = 'upload.txt';
my $presigned = $client->generate_presigned_post($bucket, $key, [], [], 1);
diag 'sleep 1 second to expire presigned url...';
sleep 1;
my $res = upload($presigned);
is $res->code, 403, 'expire';
$res = $client->head_object($bucket, $key);
ok $res->is_error, 'file is not uploaded';
};
done_testing;
xt/93_session_token.t view on Meta::CPAN
is $res->code, 200;
is $res->content, $body;
$res = $client->delete_object($bucket, $key);
is $res->code, 204;
};
subtest 'generate_presigned_post' => sub {
my $ua = LWP::UserAgent->new;
my $key = 'upload.txt';
my $presigned = $client->generate_presigned_post($bucket, $key, [], []);
my $res = $ua->post(
$presigned->{url},
Content_Type => 'multipart/form-data',
Content => [
@{$presigned->{fields}},
file => ['xt/upload.txt'],
],
);
is $res->code, 204;
$res = $client->get_object($bucket, $key);
is $res->code, 200;
$res = $client->delete_object($bucket, $key);
is $res->code, 204;
};
xt/upload.txt view on Meta::CPAN
this is test file to upload via presigned url
( run in 1.273 second using v1.01-cache-2.11-cpan-b16cb0d3907 )