Skip to content

Commit

Permalink
fix(s3): fake mode of s3
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Jul 4, 2024
1 parent 25a53a7 commit 970f004
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions pkg/filesystem/s3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package s3
import (
"context"
"fmt"
"net/http"

"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/octohelm/unifs/pkg/strfmt"
"net/http"
"net/http/httptest"
"time"
)

type Config struct {
Expand All @@ -22,16 +23,21 @@ func (c *Config) Client(ctx context.Context) (*minio.Client, error) {
}

insecure := false
if c.Endpoint.Extra.Get("insecure") == "true" || c.Endpoint.Extra.Get("insecure") == "true" {
if c.Endpoint.Extra.Get("insecure") == "true" {
insecure = true
}

o := &minio.Options{
Creds: credentials.NewStaticV4(c.Endpoint.Username, c.Endpoint.Password, ""),
Secure: !insecure,
//Transport: &logRoundTripper{
// nextRoundTripper: &http.Transport{},
//},
}

if c.Endpoint.Extra.Get("skipBucketCheck") == "true" {
o.Transport = &fakeBucket{
name: c.Bucket(),
prefix: c.Prefix(),
nextRoundTripper: &http.Transport{},
}
}

client, err := minio.New(c.Endpoint.Host(), o)
Expand Down Expand Up @@ -65,6 +71,46 @@ func (c *Config) Prefix() string {
return "/"
}

type fakeBucket struct {
nextRoundTripper http.RoundTripper
name string
prefix string
}

func (rt *fakeBucket) RoundTrip(req *http.Request) (*http.Response, error) {
if req.Method == http.MethodGet && req.URL.Path == "/"+rt.name+"/" {
r := httptest.NewRecorder()
r.WriteHeader(http.StatusOK)
_, _ = r.WriteString(`<?xml version="1.0" encoding="UTF-8"?>
<GetBucketResult>
<Bucket>` + rt.name + `</Bucket>
<PublicAccessBlockEnabled>false</PublicAccessBlockEnabled>
<CreationDate>` + time.Now().Format(time.RFC3339) + `</CreationDate>
</GetBucketResult>
`)

return r.Result(), nil
}

if req.URL.Path == "/"+rt.name+rt.prefix {
resp := httptest.NewRecorder()
resp.Header().Set("Last-Modified", time.Now().Format(rfc822TimeFormat))
resp.WriteHeader(http.StatusOK)
return resp.Result(), nil
}

resp, err := rt.nextRoundTripper.RoundTrip(req)
if err != nil {
return resp, nil
}
resp.Header.Set("Last-Modified", time.Now().Format(rfc822TimeFormat))
return resp, nil
}

const (
rfc822TimeFormat = "Mon, 2 Jan 2006 15:04:05 GMT"
)

type logRoundTripper struct {
nextRoundTripper http.RoundTripper
}
Expand Down

0 comments on commit 970f004

Please sign in to comment.