fix: Set protocol

This commit is contained in:
Rohan Verma
2018-12-29 21:05:17 +05:30
parent f50dc493f8
commit 8b0be7fb53
2 changed files with 14 additions and 6 deletions
+13 -5
View File
@@ -14,6 +14,7 @@ import (
const (
defaultPresignedURLFormat = "%s.s3.amazonaws.com" // <bucket>
defaultProtocol = "https://" // <bucket>
)
// PresignedInput is passed to GeneratePresignedURL as a parameter.
@@ -24,6 +25,7 @@ type PresignedInput struct {
Timestamp time.Time
ExtraHeaders map[string]string
ExpirySeconds int
Protocol string
}
// GeneratePresignedURL creates a Presigned URL that can be used
@@ -31,12 +33,18 @@ type PresignedInput struct {
// (https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html)
func (s3 *S3) GeneratePresignedURL(in PresignedInput) string {
var (
nowTime = in.Timestamp.UTC()
cred = fmt.Sprintf("%s/"+s3.buildCredentialWithoutKey(nowTime), s3.AccessKey)
amzdate = nowTime.Format(amzDateISO8601TimeFormat)
expiry = strconv.Itoa(in.ExpirySeconds)
nowTime = in.Timestamp.UTC()
cred = fmt.Sprintf("%s/"+s3.buildCredentialWithoutKey(nowTime), s3.AccessKey)
amzdate = nowTime.Format(amzDateISO8601TimeFormat)
expiry = strconv.Itoa(in.ExpirySeconds)
protocol = in.Protocol
)
// Set the protocol as default if not provided.
if protocol == "" {
protocol = defaultProtocol
}
// Add host to Headers
signedHeaders := in.ExtraHeaders
if signedHeaders == nil {
@@ -151,7 +159,7 @@ func (s3 *S3) GeneratePresignedURL(in PresignedInput) string {
b.Reset()
// Start Generating URL
fmt.Fprintf(&b, "http://"+defaultPresignedURLFormat+"/", in.Bucket)
fmt.Fprintf(&b, protocol+defaultPresignedURLFormat+"/", in.Bucket)
fmt.Fprintf(&b, "%s?", in.ObjectKey)
// We don't need to have a sorted order here,
+1 -1
View File
@@ -16,7 +16,7 @@ func TestS3_GeneratePresignedURL(t *testing.T) {
"AKIAIOSFODNN7EXAMPLE",
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
)
want := "http://examplebucket.s3.amazonaws.com/test.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20130524%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20130524T000000Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=aeeed9bbccd4d02ee5c0109b86d86835f995330da4c265957d157751f604d404"
want := "https://examplebucket.s3.amazonaws.com/test.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20130524%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20130524T000000Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=aeeed9bbccd4d02ee5c0109b86d86835f995330da4c265957d157751f604d404"
if got := s.GeneratePresignedURL(PresignedInput{
Bucket: "examplebucket",
ObjectKey: "test.txt",