fix(s3): correctly handle URL when RemoveBucket is enabled (#497)

* fix(s3): correctly handle URL when RemoveBucket is enabled

* fix(s3): handle errors
This commit is contained in:
ILoveScratch
2025-07-02 15:23:22 +08:00
committed by GitHub
parent 5c4cd1b198
commit 9725e0fd76

View File

@ -97,7 +97,10 @@ func (d *S3) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*mo
input.ResponseContentDisposition = &disposition
}
req, _ := d.linkClient.GetObjectRequest(input)
req, reqErr := d.linkClient.GetObjectRequest(input)
if reqErr != nil {
return nil, fmt.Errorf("failed to create GetObject request: %w", reqErr)
}
var link model.Link
var err error
if d.CustomHost != "" {
@ -107,8 +110,30 @@ func (d *S3) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*mo
err = req.Build()
link.URL = req.HTTPRequest.URL.String()
}
if err != nil {
return nil, fmt.Errorf("failed to generate link URL: %w", err)
}
if d.RemoveBucket {
link.URL = strings.Replace(link.URL, "/"+d.Bucket, "", 1)
parsedURL, parseErr := url.Parse(link.URL)
if parseErr != nil {
log.Errorf("Failed to parse URL for bucket removal: %v, URL: %s", parseErr, link.URL)
return nil, fmt.Errorf("failed to parse URL for bucket removal: %w", parseErr)
}
path := parsedURL.Path
bucketPrefix := "/" + d.Bucket
if strings.HasPrefix(path, bucketPrefix) {
path = strings.TrimPrefix(path, bucketPrefix)
if path == "" {
path = "/"
}
parsedURL.Path = path
link.URL = parsedURL.String()
log.Debugf("Removed bucket '%s' from URL path: %s -> %s", d.Bucket, bucketPrefix, path)
} else {
log.Warnf("URL path does not contain expected bucket prefix '%s': %s", bucketPrefix, path)
}
}
} else {
if common.ShouldProxy(d, fileName) {