File Coverage

blib/lib/Signer/AWSv4/S3.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 0 2 0.0
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Signer::AWSv4::S3;
2 1     1   57987 use Moo;
  1         9457  
  1         3  
3 1     1   1796 use Types::Standard qw/Str/;
  1         63722  
  1         7  
4             extends 'Signer::AWSv4';
5              
6             has bucket => (is => 'ro', isa => Str, required => 1);
7             has key => (is => 'ro', isa => Str, required => 1);
8             has content_disposition => (is => 'ro', isa => Str);
9             has content_type => (is => 'ro', isa => Str);
10             has content_encoding => (is => 'ro', isa => Str);
11             has content_language => (is => 'ro', isa => Str);
12             has cache_control => (is => 'ro', isa => Str);
13             has version_id => (is => 'ro', isa => Str);
14              
15             has '+service' => (default => 's3');
16             has '+uri' => (init_arg => undef, lazy => 1, default => sub {
17             my $self = shift;
18             sprintf "/%s/%s", $self->bucket, $self->key;
19             });
20              
21             has bucket_host => (is => 'ro', isa => Str, init_arg => undef, lazy => 1, default => sub {
22             my $self = shift;
23             if ($self->region =~m/us-east-1/i) {
24             return 's3.amazonaws.com';
25             } else {
26             return 's3-' . $self->region . '.amazonaws.com';
27             }
28             });
29              
30             has '+unsigned_payload' => (default => 1);
31              
32             sub build_params {
33 3     3 0 25 my $self = shift;
34             {
35 3         54 'X-Amz-Algorithm' => $self->aws_algorithm,
36             'X-Amz-Credential' => $self->access_key . "/" . $self->credential_scope,
37             'X-Amz-Date' => $self->date_timestamp,
38             'X-Amz-Expires' => $self->expires,
39             'X-Amz-SignedHeaders' => $self->signed_header_list,
40             ('response-content-disposition' => $self->content_disposition) x!! $self->content_disposition,
41             ('response-content-type' => $self->content_type) x!! $self->content_type,
42             ('response-content-encoding' => $self->content_encoding) x!! $self->content_encoding,
43             ('response-content-language' => $self->content_language) x!! $self->content_language,
44             ('response-cache-control' => $self->cache_control) x!! $self->cache_control,
45             (versionId => $self->version_id) x!! $self->version_id,
46             }
47             }
48              
49             sub build_headers {
50 3     3 0 24 my $self = shift;
51             {
52 3         39 Host => $self->bucket_host,
53             }
54             }
55              
56             has signed_url => (is => 'ro', isa => Str, init_arg => undef, lazy => 1, default => sub {
57             my $self = shift;
58             return join '', 'https://', $self->bucket_host, $self->uri, '?', $self->signed_qstring;
59             });
60              
61             1;
62             ### main pod documentation begin ###
63              
64             =encoding UTF-8
65              
66             =head1 NAME
67              
68             Signer::AWSv4::S3 - Implements the AWS v4 signature algorithm
69              
70             =head1 SYNOPSIS
71              
72             use Signer::AWSv4::S3;
73             $s3_sig = Signer::AWSv4::S3->new(
74             access_key => 'AKIAIOSFODNN7EXAMPLE',
75             secret_key => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
76             method => 'GET',
77             key => 'test.txt',
78             bucket => 'examplebucket',
79             region => 'us-east-1',
80             expires => 86400,
81             version_id => '1234561zOnAAAJKHxVKBxxEyuy_78901j',
82             content_type => 'text/plain',
83             content_disposition => 'inline; filename=New Name.txt',
84             );
85             say $s3_sig->signed_url;
86              
87             =head1 DESCRIPTION
88              
89             Generates S3 Presigned URLs.
90              
91             =head1 Request Attributes
92              
93             This module adds two required attributes in the constructor for obtaining an
94             S3 Presigned URL:
95              
96             =head2 key
97              
98             The name of the object in S3. This should not start with a slash (/)
99              
100             =head2 bucket
101              
102             The name of the S3 bucket
103              
104             =head2 versionId
105              
106             VersionId used to reference a specific version of the object.
107              
108             =head1 Overriding Response Header Values
109              
110             There are times when you want to override certain response header values in a GET response.
111              
112             =head2 cache_control
113              
114             Sets the Cache-Control header of the response.
115              
116             =head2 content_disposition
117              
118             Sets the Content-Disposition header of the response
119              
120             =head2 content_encoding
121              
122             Sets the Content-Encoding header of the response.
123              
124             =head2 content_language
125              
126             Sets the Content-Language header of the response.
127              
128             =head2 content_type
129              
130             Sets the Content-Type header of the response.
131              
132             =head1 Signature Attributes
133              
134             Apart from those in L, a convenience attribute is added:
135              
136             =head2 signed_url
137              
138             The presigned URL to download the object
139              
140             =head1 BUGS and SOURCE
141              
142             The source code is located here: L
143              
144             Please report bugs to: L
145              
146             =head1 AUTHOR
147              
148             Jose Luis Martinez
149             pplusdomain@gmail.com
150              
151             =head1 COPYRIGHT and LICENSE
152              
153             Copyright (c) 2018 by Jose Luis Martinez
154              
155             This code is distributed under the Apache 2 License. The full text of the license can be found in the LICENSE file included with this module.
156              
157             =cut