swift.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package swift
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "time"
  10. "github.com/ncw/swift/v2"
  11. "github.com/imgproxy/imgproxy/v3/config"
  12. defaultTransport "github.com/imgproxy/imgproxy/v3/transport"
  13. "github.com/imgproxy/imgproxy/v3/transport/notmodified"
  14. )
  15. type transport struct {
  16. con *swift.Connection
  17. }
  18. func New() (http.RoundTripper, error) {
  19. trans, err := defaultTransport.New(false)
  20. if err != nil {
  21. return nil, err
  22. }
  23. c := &swift.Connection{
  24. UserName: config.SwiftUsername,
  25. ApiKey: config.SwiftAPIKey,
  26. AuthUrl: config.SwiftAuthURL,
  27. AuthVersion: config.SwiftAuthVersion,
  28. Domain: config.SwiftDomain, // v3 auth only
  29. Tenant: config.SwiftTenant, // v2 auth only
  30. Timeout: time.Duration(config.SwiftTimeoutSeconds) * time.Second,
  31. ConnectTimeout: time.Duration(config.SwiftConnectTimeoutSeconds) * time.Second,
  32. Transport: trans,
  33. }
  34. ctx := context.Background()
  35. err = c.Authenticate(ctx)
  36. if err != nil {
  37. return nil, fmt.Errorf("swift authentication error: %s", err)
  38. }
  39. return transport{con: c}, nil
  40. }
  41. func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
  42. // Users should have converted the object storage URL in the format of swift://{container}/{object}
  43. container := req.URL.Host
  44. objectName := strings.TrimPrefix(req.URL.Path, "/")
  45. if len(container) == 0 || len(objectName) == 0 {
  46. body := strings.NewReader("Invalid Swift URL: container name or object name is empty")
  47. return &http.Response{
  48. StatusCode: http.StatusNotFound,
  49. Proto: "HTTP/1.0",
  50. ProtoMajor: 1,
  51. ProtoMinor: 0,
  52. Header: http.Header{},
  53. ContentLength: int64(body.Len()),
  54. Body: io.NopCloser(body),
  55. Close: false,
  56. Request: req,
  57. }, nil
  58. }
  59. reqHeaders := make(swift.Headers)
  60. if r := req.Header.Get("Range"); len(r) > 0 {
  61. reqHeaders["Range"] = r
  62. }
  63. object, objectHeaders, err := t.con.ObjectOpen(req.Context(), container, objectName, false, reqHeaders)
  64. header := make(http.Header)
  65. if err != nil {
  66. if errors.Is(err, swift.ObjectNotFound) || errors.Is(err, swift.ContainerNotFound) {
  67. return &http.Response{
  68. StatusCode: http.StatusNotFound,
  69. Proto: "HTTP/1.0",
  70. ProtoMajor: 1,
  71. ProtoMinor: 0,
  72. Header: header,
  73. Body: io.NopCloser(strings.NewReader(err.Error())),
  74. Close: false,
  75. Request: req,
  76. }, nil
  77. }
  78. return nil, fmt.Errorf("error opening object: %v", err)
  79. }
  80. if config.ETagEnabled {
  81. if etag, ok := objectHeaders["Etag"]; ok {
  82. header.Set("ETag", etag)
  83. }
  84. }
  85. if config.LastModifiedEnabled {
  86. if lastModified, ok := objectHeaders["Last-Modified"]; ok {
  87. header.Set("Last-Modified", lastModified)
  88. }
  89. }
  90. if resp := notmodified.Response(req, header); resp != nil {
  91. object.Close()
  92. return resp, nil
  93. }
  94. for k, v := range objectHeaders {
  95. header.Set(k, v)
  96. }
  97. return &http.Response{
  98. Status: "200 OK",
  99. StatusCode: 200,
  100. Proto: "HTTP/1.0",
  101. ProtoMajor: 1,
  102. ProtoMinor: 0,
  103. Header: header,
  104. Body: object,
  105. Close: true,
  106. Request: req,
  107. }, nil
  108. }