diff --git a/presigned.go b/presigned.go index 937a59b..a39000b 100644 --- a/presigned.go +++ b/presigned.go @@ -14,6 +14,7 @@ import ( const ( defaultPresignedURLFormat = "%s.s3.amazonaws.com" // + defaultProtocol = "https://" // ) // 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, diff --git a/presigned_test.go b/presigned_test.go index 1708eca..42f8556 100644 --- a/presigned_test.go +++ b/presigned_test.go @@ -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",