diff --git a/config/config.go b/config/config.go index 178d10f7..881d5fbf 100644 --- a/config/config.go +++ b/config/config.go @@ -80,23 +80,26 @@ var ( CookieBaseURL string LocalFileSystemRoot string - S3Enabled bool - S3Region string - S3Endpoint string - GCSEnabled bool - GCSKey string - ABSEnabled bool - ABSName string - ABSKey string - ABSEndpoint string - SwiftEnabled bool - SwiftUsername string - SwiftAPIKey string - SwiftAuthURL string - SwiftDomain string - SwiftTenant string - SwiftAuthVersion int + S3Enabled bool + S3Region string + S3Endpoint string + + GCSEnabled bool + GCSKey string + + ABSEnabled bool + ABSName string + ABSKey string + ABSEndpoint string + + SwiftEnabled bool + SwiftUsername string + SwiftAPIKey string + SwiftAuthURL string + SwiftDomain string + SwiftTenant string + SwiftAuthVersion int SwiftConnectTimeoutSeconds int SwiftTimeoutSeconds int diff --git a/docs/serving_files_from_openstack_swift.md b/docs/serving_files_from_openstack_swift.md index 6b0ba5ff..00988f24 100644 --- a/docs/serving_files_from_openstack_swift.md +++ b/docs/serving_files_from_openstack_swift.md @@ -2,7 +2,7 @@ imgproxy can process images from OpenStack Object Storage, also known as Swift. To use this feature, do the following: -1. Set `IMGPROXY_USE_SWIFT` environment variable to `true` +1. Set the `IMGPROXY_USE_SWIFT` environment variable to `true` 2. Configure Swift authentication with the following environment variables * `IMGPROXY_SWIFT_USERNAME`: the username for Swift API access. Default: blank * `IMGPROXY_SWIFT_API_KEY`: the API key for Swift API access. Default: blank @@ -11,6 +11,4 @@ imgproxy can process images from OpenStack Object Storage, also known as Swift. * `IMGPROXY_SWIFT_TENANT`: the tenant name (optional, v2 auth only). Default: blank * `IMGPROXY_SWIFT_DOMAIN`: the Swift domain name (optional, v3 auth only): Default: blank -3. Use `swift://%{container}/%{object_path}` as the source image URL. e.g. an original object storage URL in the format of - `/v1/{account}/{container}/{object_path}` such as `http://127.0.0.1:8080/v1/AUTH_test/images/flowers/rose.jpg` should - be converted to `swift://images/flowers/rose.jpg`. +3. Use `swift://%{container}/%{object_path}` as the source image URL, e.g. an original object storage URL in the format of `/v1/{account}/{container}/{object_path}`, such as `http://127.0.0.1:8080/v1/AUTH_test/images/flowers/rose.jpg`, should be converted to `swift://images/flowers/rose.jpg`. diff --git a/transport/swift/swift.go b/transport/swift/swift.go index 3f4762fe..01547028 100644 --- a/transport/swift/swift.go +++ b/transport/swift/swift.go @@ -7,8 +7,9 @@ import ( "strings" "time" - "github.com/imgproxy/imgproxy/v3/config" "github.com/ncw/swift/v2" + + "github.com/imgproxy/imgproxy/v3/config" ) type transport struct { @@ -43,9 +44,7 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error) container := req.URL.Host objectName := strings.TrimPrefix(req.URL.Path, "/") - headers := make(swift.Headers) - - object, headers, err := t.con.ObjectOpen(req.Context(), container, objectName, false, headers) + object, objectHeaders, err := t.con.ObjectOpen(req.Context(), container, objectName, false, make(swift.Headers)) if err != nil { return nil, fmt.Errorf("error opening object: %v", err) @@ -54,11 +53,12 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error) header := make(http.Header) if config.ETagEnabled { - if etag, ok := headers["Etag"]; ok { + if etag, ok := objectHeaders["Etag"]; ok { header.Set("ETag", etag) if len(etag) > 0 && etag == req.Header.Get("If-None-Match") { object.Close() + return &http.Response{ StatusCode: http.StatusNotModified, Proto: "HTTP/1.0", @@ -74,6 +74,10 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error) } } + for k, v := range objectHeaders { + header.Set(k, v) + } + return &http.Response{ Status: "200 OK", StatusCode: 200,