Skip to content

Commit 139c607

Browse files
Merge pull request #75 from chhawchharia/CI-21342
fix: [CI-21342] Harden AWS SDK v2 migration for S3-compatible endpoints
2 parents a3b1d59 + 56d48e4 commit 139c607

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

aws.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"mime"
1010
"os"
1111
"path/filepath"
12+
"strings"
1213
"time"
1314

1415
"github.com/aws/aws-sdk-go-v2/aws"
@@ -50,9 +51,13 @@ func NewAWS(p *Plugin) AWS {
5051

5152
s3Opts := []func(*s3.Options){}
5253
if p.Endpoint != "" {
54+
endpoint := normalizeEndpoint(p.Endpoint)
5355
s3Opts = append(s3Opts, func(o *s3.Options) {
54-
o.BaseEndpoint = aws.String(p.Endpoint)
56+
o.BaseEndpoint = aws.String(endpoint)
5557
o.UsePathStyle = p.PathStyle
58+
// S3-compatible services (MinIO, Spaces, B2, etc.) may not support the
59+
// CRC32 checksums that SDK v2 sends by default with PutObject.
60+
o.RequestChecksumCalculation = aws.RequestChecksumCalculationWhenRequired
5661
})
5762
} else if p.PathStyle {
5863
s3Opts = append(s3Opts, func(o *s3.Options) {
@@ -68,6 +73,13 @@ func NewAWS(p *Plugin) AWS {
6873
return AWS{c, cf, r, l, p}
6974
}
7075

76+
func normalizeEndpoint(endpoint string) string {
77+
if endpoint == "" || strings.Contains(endpoint, "://") {
78+
return endpoint
79+
}
80+
return "https://" + endpoint
81+
}
82+
7183
func (a *AWS) Upload(local, remote string) error {
7284
ctx := context.Background()
7385
p := a.plugin
@@ -142,7 +154,7 @@ func (a *AWS) Upload(local, remote string) error {
142154
var apiErr smithy.APIError
143155
isNotFound := false
144156
if ok := errors.As(err, &apiErr); ok {
145-
if apiErr.ErrorCode() == "404" || apiErr.ErrorCode() == "NotFound" {
157+
if apiErr.ErrorCode() == "404" || apiErr.ErrorCode() == "NotFound" || apiErr.ErrorCode() == "NoSuchKey" {
146158
isNotFound = true
147159
}
148160
}
@@ -208,7 +220,7 @@ func (a *AWS) Upload(local, remote string) error {
208220
_, _ = io.Copy(hash, file)
209221
sum := fmt.Sprintf("\"%x\"", hash.Sum(nil))
210222

211-
if sum == *head.ETag {
223+
if head.ETag != nil && sum == *head.ETag {
212224
shouldCopy := false
213225

214226
if head.ContentType == nil && contentType != "" {

0 commit comments

Comments
 (0)