Skip to content

Commit

Permalink
s3: optimize presigned HEAD request in GET context
Browse files Browse the repository at this point in the history
Signed-off-by: Janusz Marcinkiewicz <[email protected]>
  • Loading branch information
VirrageS committed Feb 17, 2025
1 parent a78ea39 commit 1061d4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ais/s3/presigned.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ func parseCredentialHeader(hdr string) (region string) {
}

func (pts *PresignedReq) DoHead(client *http.Client) (*PresignedResp, error) {
canOptimize := pts.oreq.Method == http.MethodGet
canOptimize = canOptimize && pts.oreq.Header.Get(cos.S3HdrContentSHA256) == ""
canOptimize = canOptimize && !strings.Contains(pts.oreq.Header.Get(cos.S3HdrSignedHeaders), strings.ToLower(cos.HdrRange))
if canOptimize {
// Temporarily override `Range` header value.
//
// This method could be executed in context of GET request in which case we
// don't want to retrieve the object but just the metadata. Note that we cannot
// simply use HEAD here since the request might not be signed for this method.
hdrRangeValue, hdrRangeExists := pts.oreq.Header[cos.HdrRange]
pts.oreq.Header.Set(cos.HdrRange, cos.HdrRangeValPrefix+"0-0")
defer func() {
if hdrRangeExists {
pts.oreq.Header[cos.HdrRange] = hdrRangeValue
} else {
pts.oreq.Header.Del(cos.HdrRange)
}
}()
}

resp, err := pts.DoReader(client)
if err != nil || resp == nil {
return resp, err
Expand Down
1 change: 1 addition & 0 deletions cmn/cos/http_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
// https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
S3UnsignedPayload = "UNSIGNED-PAYLOAD"
S3HdrContentSHA256 = "x-amz-content-sha256"
S3HdrSignedHeaders = "x-ams-signedheaders"

S3HdrBckRegion = "x-amz-bucket-region"

Expand Down

0 comments on commit 1061d4c

Please sign in to comment.