Amazon-S3-Thin

 view release on metacpan or  search on metacpan

t/05_presigned_post.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Amazon::S3::Thin;
use MIME::Base64 ();
use JSON::PP ();

BEGIN {
    *CORE::GLOBAL::time = sub() { 1572566400 }; # Fri Nov  1 00:00:00 2019
}

my $_JSON;
sub policy { MIME::Base64::encode_base64(($_JSON ||= JSON::PP->new->utf8->canonical)->encode(@_), '') }

my $args = {
    aws_access_key_id     => 'dummy_access_key_id',
    aws_secret_access_key => 'dummy_secret_access_key',
    region                => 'ap-north-east-1',
    ua                    => MockUA->new,
};
my $client = Amazon::S3::Thin->new({%$args});

my $bucket = 'test-bucket';
my $key = 'dir/private.txt';

is_deeply $client->generate_presigned_post($bucket, $key), {
    url    => 'http://s3.ap-north-east-1.amazonaws.com/test-bucket/',
    fields => [
        key                => $key,
        'x-amz-algorithm'  => 'AWS4-HMAC-SHA256',
        'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request',
        'x-amz-date'       => '20191101T000000Z',
        policy             => policy({
            conditions => [
                {bucket             => 'test-bucket'},
                {key                => 'dir/private.txt'},
                {'x-amz-algorithm'  => 'AWS4-HMAC-SHA256'},
                {'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request'},
                {'x-amz-date'       => '20191101T000000Z'},
            ],
            expiration => '2019-11-01T01:00:00Z',
        }),
        'x-amz-signature' => 'ef0b9394345aa20773e07ecdf1f704135a0fd7a5db78e2e5433b7331668f1cc3',
    ],
};

is_deeply $client->generate_presigned_post($bucket, 'foo-${filename}'), {
    url    => 'http://s3.ap-north-east-1.amazonaws.com/test-bucket/',
    fields => [
        key                => 'foo-${filename}',
        'x-amz-algorithm'  => 'AWS4-HMAC-SHA256',
        'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request',
        'x-amz-date'       => '20191101T000000Z',
        policy             => policy({
            conditions => [
                {bucket             => 'test-bucket'},
                ['starts-with'      => '$key', 'foo-'],
                {'x-amz-algorithm'  => 'AWS4-HMAC-SHA256'},
                {'x-amz-credential' => 'dummy_access_key_id/20191101/ap-north-east-1/s3/aws4_request'},
                {'x-amz-date'       => '20191101T000000Z'},
            ],
            expiration => '2019-11-01T01:00:00Z',
        }),
        'x-amz-signature' => '28baaf6392b4a747f1e65d9bb33f205342e025bf23f3d7ab37de880828ac0f52',
    ],
}, '${filename} expands starts-with condition';

is_deeply $client->generate_presigned_post($bucket, $key, [
    'Content-Type'   => 'image/png',
    'x-amz-meta-foo' => 'bar',
]), {
    url    => 'http://s3.ap-north-east-1.amazonaws.com/test-bucket/',
    fields => [



( run in 1.007 second using v1.01-cache-2.11-cpan-140bd7fdf52 )