fix: Set protocol
This commit is contained in:
+13
-5
@@ -14,6 +14,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultPresignedURLFormat = "%s.s3.amazonaws.com" // <bucket>
|
defaultPresignedURLFormat = "%s.s3.amazonaws.com" // <bucket>
|
||||||
|
defaultProtocol = "https://" // <bucket>
|
||||||
)
|
)
|
||||||
|
|
||||||
// PresignedInput is passed to GeneratePresignedURL as a parameter.
|
// PresignedInput is passed to GeneratePresignedURL as a parameter.
|
||||||
@@ -24,6 +25,7 @@ type PresignedInput struct {
|
|||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
ExtraHeaders map[string]string
|
ExtraHeaders map[string]string
|
||||||
ExpirySeconds int
|
ExpirySeconds int
|
||||||
|
Protocol string
|
||||||
}
|
}
|
||||||
|
|
||||||
// GeneratePresignedURL creates a Presigned URL that can be used
|
// 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)
|
// (https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html)
|
||||||
func (s3 *S3) GeneratePresignedURL(in PresignedInput) string {
|
func (s3 *S3) GeneratePresignedURL(in PresignedInput) string {
|
||||||
var (
|
var (
|
||||||
nowTime = in.Timestamp.UTC()
|
nowTime = in.Timestamp.UTC()
|
||||||
cred = fmt.Sprintf("%s/"+s3.buildCredentialWithoutKey(nowTime), s3.AccessKey)
|
cred = fmt.Sprintf("%s/"+s3.buildCredentialWithoutKey(nowTime), s3.AccessKey)
|
||||||
amzdate = nowTime.Format(amzDateISO8601TimeFormat)
|
amzdate = nowTime.Format(amzDateISO8601TimeFormat)
|
||||||
expiry = strconv.Itoa(in.ExpirySeconds)
|
expiry = strconv.Itoa(in.ExpirySeconds)
|
||||||
|
protocol = in.Protocol
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Set the protocol as default if not provided.
|
||||||
|
if protocol == "" {
|
||||||
|
protocol = defaultProtocol
|
||||||
|
}
|
||||||
|
|
||||||
// Add host to Headers
|
// Add host to Headers
|
||||||
signedHeaders := in.ExtraHeaders
|
signedHeaders := in.ExtraHeaders
|
||||||
if signedHeaders == nil {
|
if signedHeaders == nil {
|
||||||
@@ -151,7 +159,7 @@ func (s3 *S3) GeneratePresignedURL(in PresignedInput) string {
|
|||||||
b.Reset()
|
b.Reset()
|
||||||
|
|
||||||
// Start Generating URL
|
// Start Generating URL
|
||||||
fmt.Fprintf(&b, "http://"+defaultPresignedURLFormat+"/", in.Bucket)
|
fmt.Fprintf(&b, protocol+defaultPresignedURLFormat+"/", in.Bucket)
|
||||||
fmt.Fprintf(&b, "%s?", in.ObjectKey)
|
fmt.Fprintf(&b, "%s?", in.ObjectKey)
|
||||||
|
|
||||||
// We don't need to have a sorted order here,
|
// We don't need to have a sorted order here,
|
||||||
|
|||||||
+1
-1
@@ -16,7 +16,7 @@ func TestS3_GeneratePresignedURL(t *testing.T) {
|
|||||||
"AKIAIOSFODNN7EXAMPLE",
|
"AKIAIOSFODNN7EXAMPLE",
|
||||||
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
"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{
|
if got := s.GeneratePresignedURL(PresignedInput{
|
||||||
Bucket: "examplebucket",
|
Bucket: "examplebucket",
|
||||||
ObjectKey: "test.txt",
|
ObjectKey: "test.txt",
|
||||||
|
|||||||
Reference in New Issue
Block a user