AWS-S3

 view release on metacpan or  search on metacpan

lib/AWS/S3/Signer/V4.pm  view on Meta::CPAN


    # https://docs.aws.amazon.com/prescriptive-guidance/latest/defining-bucket-names-data-lakes/faq.html
    # Only lowercase letters, numbers, dashes, and dots are allowed in S3 bucket names.
    # Bucket names must be three to 63 characters in length,
    # must begin and end with a number or letter,
    # and cannot be in an IP address format.
    my $bucket_re = '[a-z0-9][a-z0-9\-\.]{1,61}[a-z0-9]';
    my $domain_re = 'amazonaws\.com';
    my $region_re = '(?:af|ap|ca|eu|il|me|mx|sa|us)-[a-z]+-\d';

    my ( $service, $url_style );

    # listed in order of appearance found in the docs:
    # https://community.aws/content/2biM1C0TkMkvJ2BLICiff8MKXS9/format-and-parse-amazon-s3-url?lang=en
    if ( $host =~ /^(\w+)([-.])($region_re)\.$domain_re/ ) {
        $service = $1;
        $region ||= $3;
        $url_style = $2 eq '-' ? 'regional dash-style' : 'regional dot-style';
    }
    elsif ( $host =~ /^$bucket_re\.($region_re)\.s3\.$domain_re/ ) {
        $service = 's3';
        $region ||= $1;
        $url_style = 'regional virtual-hosted-style';
    }
    elsif ( $host =~ /^s3\.$domain_re/ ) {
        $service = 's3';
        $region  = 'us-east-1';
        $url_style = 'legacy with path-style';
    }
    elsif ( $host =~ /^$bucket_re\.s3\.$domain_re/ ) {
        $service = 's3';
        $region ||= 'us-east-1';
        $url_style = 'legacy with virtual-hosted-style';
    }
    elsif ( $host =~ /^$bucket_re\.s3[\.-]($region_re)\.$domain_re/ ) {
        $service = 's3';
        $region ||= $1;
        $url_style = 'regional virtual-hosted-style';
    }
    elsif ($host =~ /^([\w-]+)\.([\w-]+)\.$domain_re/) {
        $service = $1;
        $region    ||= $2;
        $url_style = 'legacy path-style service';
    }
    elsif ( $host =~ /^([\w-]+)\.$domain_re/ ) {
        $service = $1;
        $region    = 'us-east-1';
        $url_style = 'legacy path-style';
    }
    elsif ( exists PAAPI_REGION->{$host} ) {
        $service = 'ProductAdvertisingAPI';
        $region  = PAAPI_REGION->{$host};
    }

    return ( $service, $region, $url_style );
}

sub _parse_scope {
    my $self  = shift;
    my $scope = shift;
    return split '/', $scope;
}

sub _datetime {
    my $self    = shift;

t/aws/s3/signer/v4_parse_host.t  view on Meta::CPAN

        me-south-1
        mx-central-1
        sa-east-1
        us-east-1 us-east-2
        us-west-1 us-west-2
    / ) {

        foreach my $host (

            # https://community.aws/content/2biM1C0TkMkvJ2BLICiff8MKXS9/format-and-parse-amazon-s3-url?lang=en
            [ "$bucket.s3.amazonaws.com"         => [ 's3','us-east-1','legacy with virtual-hosted-style' ] ],
            [ "$bucket.$region.s3.amazonaws.com" => [ 's3',$region    ,'regional virtual-hosted-style'    ] ],
            [ "s3.$region.amazonaws.com"         => [ 's3',$region    ,'regional dot-style'               ] ],
            [ "s3-$region.amazonaws.com"         => [ 's3',$region    ,'regional dash-style'              ] ],
            [ "s3.amazonaws.com"                 => [ 's3','us-east-1','legacy with path-style'           ] ],
            [ "cognito-identity.$region.amazonaws.com" => [ 'cognito-identity',$region,'legacy path-style service' ] ],
        ) {
            cmp_deeply(
                [ $signer->parse_host( $host->[0] ) ],
                $host->[1],
                $host->[0] . ' -> ' . join( ", ",@{ $host->[1] } ),
            );
        }

        foreach my $host (

            # https://community.aws/content/2biM1C0TkMkvJ2BLICiff8MKXS9/format-and-parse-amazon-s3-url?lang=en
            [ "$bucket.s3.amazonaws.com"         => [ 's3',$region    ,'legacy with virtual-hosted-style' ] ],
            [ "$bucket.$region.s3.amazonaws.com" => [ 's3',$region    ,'regional virtual-hosted-style'    ] ],
            [ "s3.$region.amazonaws.com"         => [ 's3',$region    ,'regional dot-style'               ] ],
            [ "s3-$region.amazonaws.com"         => [ 's3',$region    ,'regional dash-style'              ] ],
            [ "s3.amazonaws.com"                 => [ 's3','us-east-1','legacy with path-style'           ] ],
            [ "cognito-identity.$region.amazonaws.com" => [ 'cognito-identity',$region,'legacy path-style service' ] ],
        ) {
            cmp_deeply(
                [ $signer->parse_host( $host->[0],$region ) ],
                $host->[1],
                $host->[0] . ' -> ' . join( ", ",@{ $host->[1] } ) . ' (passed region)',
            );
        }
    }
}



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