From ad68b705eecdff620d357087058f0f8925fc7273 Mon Sep 17 00:00:00 2001 From: Kevin Su Date: Mon, 11 Mar 2024 23:33:06 -0700 Subject: [PATCH] Ensure that requestHeaders is correctly assigned a value (#15) Signed-off-by: Kevin Su --- azure/container.go | 16 ++++++++-------- google/container.go | 14 +++++++------- s3/container.go | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/azure/container.go b/azure/container.go index 2e0a701..40624f0 100644 --- a/azure/container.go +++ b/azure/container.go @@ -42,7 +42,7 @@ func (c *container) PreSignRequest(ctx context.Context, method stow.ClientMethod params stow.PresignRequestParams) (response stow.PresignResponse, err error) { containerName := c.id blobName := key - + var requestHeaders map[string]string permissions := sas.BlobPermissions{} switch method { case stow.ClientMethodGet: @@ -50,6 +50,13 @@ func (c *container) PreSignRequest(ctx context.Context, method stow.ClientMethod case stow.ClientMethodPut: permissions.Add = true permissions.Write = true + + requestHeaders = map[string]string{"Content-Length": strconv.Itoa(len(params.ContentMD5)), "Content-MD5": params.ContentMD5} + requestHeaders["x-ms-blob-type"] = "BlockBlob" // https://learn.microsoft.com/en-us/rest/api/storageservices/put-blob?tabs=microsoft-entra-id#remarks + + if params.AddContentMD5Metadata { + requestHeaders[fmt.Sprintf("x-ms-meta-%s", stow.FlyteContentMD5)] = params.ContentMD5 + } } sasQueryParams, err := c.preSigner(ctx, sas.BlobSignatureValues{ @@ -68,13 +75,6 @@ func (c *container) PreSignRequest(ctx context.Context, method stow.ClientMethod // Create the SAS URL for the resource you wish to access, and append the SAS query parameters. qp := sasQueryParams.Encode() - requestHeaders := map[string]string{"Content-Length": strconv.Itoa(len(params.ContentMD5)), "Content-MD5": params.ContentMD5} - requestHeaders["x-ms-blob-type"] = "BlockBlob" // https://learn.microsoft.com/en-us/rest/api/storageservices/put-blob?tabs=microsoft-entra-id#remarks - - if params.AddContentMD5Metadata { - requestHeaders[fmt.Sprintf("x-ms-meta-%s", stow.FlyteContentMD5)] = params.ContentMD5 - } - return stow.PresignResponse{Url: fmt.Sprintf("%s/%s?%s", c.client.URL(), blobName, qp), RequiredRequestHeaders: requestHeaders}, nil } diff --git a/google/container.go b/google/container.go index c2f1c55..f52f2d5 100644 --- a/google/container.go +++ b/google/container.go @@ -45,22 +45,22 @@ func (c *Container) Bucket() *storage.BucketHandle { func (c *Container) PreSignRequest(_ context.Context, clientMethod stow.ClientMethod, id string, params stow.PresignRequestParams) (response stow.PresignResponse, err error) { + headers := make([]string, 0, 3) + var requestHeaders map[string]string if len(params.HttpMethod) == 0 { switch clientMethod { case stow.ClientMethodGet: params.HttpMethod = http.MethodGet case stow.ClientMethodPut: params.HttpMethod = http.MethodPut + requestHeaders = map[string]string{"Content-Length": strconv.Itoa(len(params.ContentMD5)), "Content-MD5": params.ContentMD5} + if params.AddContentMD5Metadata { + headers = append(headers, fmt.Sprintf("%s%s: %s", googleMetadataPrefix, stow.FlyteContentMD5, params.ContentMD5)) + requestHeaders[fmt.Sprintf("%s%s", googleMetadataPrefix, stow.FlyteContentMD5)] = params.ContentMD5 + } } } - headers := make([]string, 0, 3) - requestHeaders := map[string]string{"Content-Length": strconv.Itoa(len(params.ContentMD5)), "Content-MD5": params.ContentMD5} - if params.AddContentMD5Metadata { - headers = append(headers, fmt.Sprintf("%s%s: %s", googleMetadataPrefix, stow.FlyteContentMD5, params.ContentMD5)) - requestHeaders[fmt.Sprintf("%s%s", googleMetadataPrefix, stow.FlyteContentMD5)] = params.ContentMD5 - } - url, error := c.Bucket().SignedURL(id, &storage.SignedURLOptions{ Method: params.HttpMethod, Expires: time.Now().Add(params.ExpiresIn), diff --git a/s3/container.go b/s3/container.go index 863f407..a865971 100644 --- a/s3/container.go +++ b/s3/container.go @@ -46,7 +46,7 @@ func (c *container) PreSignRequest(ctx context.Context, clientMethod stow.Client } metadata := make(map[string]*string) - requestHeaders := map[string]string{"Content-Length": strconv.Itoa(len(params.ContentMD5)), "Content-MD5": params.ContentMD5} + requestHeaders = map[string]string{"Content-Length": strconv.Itoa(len(params.ContentMD5)), "Content-MD5": params.ContentMD5} if params.AddContentMD5Metadata { metadata[stow.FlyteContentMD5] = aws.String(params.ContentMD5) requestHeaders[fmt.Sprintf("x-amz-meta-%s", stow.FlyteContentMD5)] = params.ContentMD5