Parcourir la source

Ensure S3 endpoint scheme; Trim all redundant slashes from S3 object key

DarthSim il y a 1 an
Parent
commit
e703f6d4d0
2 fichiers modifiés avec 8 ajouts et 2 suppressions
  1. 2 0
      CHANGELOG.md
  2. 6 2
      transport/s3/s3.go

+ 2 - 0
CHANGELOG.md

@@ -2,6 +2,8 @@
 
 ## [Unreleased]
 ### Changed
+- Automatically add `http://` scheme to the `IMGPROXY_S3_ENDPOINT` value if it has no scheme.
+- Trim redundant slashes in the S3 object key.
 - (pro) Allow specifying [gradient](https://docs.imgproxy.net/latest/usage/processing#gradient) direction as an angle in degrees.
 
 ### Fix

+ 6 - 2
transport/s3/s3.go

@@ -73,8 +73,12 @@ func New() (http.RoundTripper, error) {
 	clientOptions := []func(*s3.Options){}
 
 	if len(config.S3Endpoint) != 0 {
+		endpoint := config.S3Endpoint
+		if !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") {
+			endpoint = "http://" + endpoint
+		}
 		clientOptions = append(clientOptions, func(o *s3.Options) {
-			o.BaseEndpoint = aws.String(config.S3Endpoint)
+			o.BaseEndpoint = aws.String(endpoint)
 			o.UsePathStyle = true
 		})
 	}
@@ -95,7 +99,7 @@ func New() (http.RoundTripper, error) {
 
 func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
 	bucket := req.URL.Host
-	key := strings.TrimPrefix(req.URL.Path, "/")
+	key := strings.TrimLeft(req.URL.Path, "/")
 
 	if len(bucket) == 0 || len(key) == 0 {
 		body := strings.NewReader("Invalid S3 URL: bucket name or object key is empty")