Polish swift:// support

This commit is contained in:
DarthSim
2022-04-06 17:35:44 +06:00
parent 7a2296aee8
commit afe283790f
3 changed files with 30 additions and 25 deletions

View File

@@ -80,15 +80,19 @@ var (
CookieBaseURL string CookieBaseURL string
LocalFileSystemRoot string LocalFileSystemRoot string
S3Enabled bool S3Enabled bool
S3Region string S3Region string
S3Endpoint string S3Endpoint string
GCSEnabled bool GCSEnabled bool
GCSKey string GCSKey string
ABSEnabled bool ABSEnabled bool
ABSName string ABSName string
ABSKey string ABSKey string
ABSEndpoint string ABSEndpoint string
SwiftEnabled bool SwiftEnabled bool
SwiftUsername string SwiftUsername string
SwiftAPIKey string SwiftAPIKey string
@@ -96,7 +100,6 @@ var (
SwiftDomain string SwiftDomain string
SwiftTenant string SwiftTenant string
SwiftAuthVersion int SwiftAuthVersion int
SwiftConnectTimeoutSeconds int SwiftConnectTimeoutSeconds int
SwiftTimeoutSeconds int SwiftTimeoutSeconds int

View File

@@ -2,7 +2,7 @@
imgproxy can process images from OpenStack Object Storage, also known as Swift. To use this feature, do the following: 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 2. Configure Swift authentication with the following environment variables
* `IMGPROXY_SWIFT_USERNAME`: the username for Swift API access. Default: blank * `IMGPROXY_SWIFT_USERNAME`: the username for Swift API access. Default: blank
* `IMGPROXY_SWIFT_API_KEY`: the API key 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_TENANT`: the tenant name (optional, v2 auth only). Default: blank
* `IMGPROXY_SWIFT_DOMAIN`: the Swift domain name (optional, v3 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 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`.
`/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`.

View File

@@ -7,8 +7,9 @@ import (
"strings" "strings"
"time" "time"
"github.com/imgproxy/imgproxy/v3/config"
"github.com/ncw/swift/v2" "github.com/ncw/swift/v2"
"github.com/imgproxy/imgproxy/v3/config"
) )
type transport struct { type transport struct {
@@ -43,9 +44,7 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error)
container := req.URL.Host container := req.URL.Host
objectName := strings.TrimPrefix(req.URL.Path, "/") objectName := strings.TrimPrefix(req.URL.Path, "/")
headers := make(swift.Headers) object, objectHeaders, err := t.con.ObjectOpen(req.Context(), container, objectName, false, make(swift.Headers))
object, headers, err := t.con.ObjectOpen(req.Context(), container, objectName, false, headers)
if err != nil { if err != nil {
return nil, fmt.Errorf("error opening object: %v", err) 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) header := make(http.Header)
if config.ETagEnabled { if config.ETagEnabled {
if etag, ok := headers["Etag"]; ok { if etag, ok := objectHeaders["Etag"]; ok {
header.Set("ETag", etag) header.Set("ETag", etag)
if len(etag) > 0 && etag == req.Header.Get("If-None-Match") { if len(etag) > 0 && etag == req.Header.Get("If-None-Match") {
object.Close() object.Close()
return &http.Response{ return &http.Response{
StatusCode: http.StatusNotModified, StatusCode: http.StatusNotModified,
Proto: "HTTP/1.0", 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{ return &http.Response{
Status: "200 OK", Status: "200 OK",
StatusCode: 200, StatusCode: 200,