feat: stand-alone port s3 server (#6242)

* feat: single port s3 server

* fix: unable to PUT files if not in root dir
This commit is contained in:
itsHenry
2024-03-24 15:16:00 +08:00
committed by GitHub
parent 022e0ca292
commit 9c84b6596f
9 changed files with 70 additions and 21 deletions

View File

@ -171,3 +171,8 @@ func Cors(r *gin.Engine) {
config.AllowMethods = conf.Conf.Cors.AllowMethods
r.Use(cors.New(config))
}
func InitS3(e *gin.Engine) {
Cors(e)
S3Server(e.Group("/"))
}

View File

@ -6,20 +6,25 @@ import (
"strings"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/server/common"
"github.com/alist-org/alist/v3/server/s3"
"github.com/gin-gonic/gin"
)
func S3(g *gin.RouterGroup) {
if !setting.GetBool(conf.S3Enabled) {
if !conf.Conf.S3.Enable {
g.Any("/*path", func(c *gin.Context) {
common.ErrorStrResp(c, "S3 server is not enabled", 403)
})
return
}
h, _ := s3.NewServer(context.Background(), []string{setting.GetStr(conf.S3AccessKeyId) + "," + setting.GetStr(conf.S3SecretAccessKey)})
if conf.Conf.S3.Port != -1 {
g.Any("/*path", func(c *gin.Context) {
common.ErrorStrResp(c, "S3 server bound to single port", 403)
})
return
}
h, _ := s3.NewServer(context.Background())
g.Any("/*path", func(c *gin.Context) {
adjustedPath := strings.TrimPrefix(c.Request.URL.Path, path.Join(conf.URL.Path, "/s3"))
@ -27,3 +32,14 @@ func S3(g *gin.RouterGroup) {
gin.WrapH(h)(c)
})
}
func S3Server(g *gin.RouterGroup) {
if !conf.Conf.S3.Enable {
g.Any("/*path", func(c *gin.Context) {
common.ErrorStrResp(c, "S3 server is not enabled", 403)
})
return
}
h, _ := s3.NewServer(context.Background())
g.Any("/*path", gin.WrapH(h))
}

View File

@ -299,7 +299,7 @@ func (b *s3Backend) PutObject(
Mimetype: meta["Content-Type"],
}
err = fs.PutDirectly(ctx, path.Dir(reqPath), stream)
err = fs.PutDirectly(ctx, reqPath, stream)
if err != nil {
return result, err
}

View File

@ -11,7 +11,7 @@ import (
)
// Make a new S3 Server to serve the remote
func NewServer(ctx context.Context, authpair []string) (h http.Handler, err error) {
func NewServer(ctx context.Context) (h http.Handler, err error) {
var newLogger logger
faker := gofakes3.New(
newBackend(),
@ -19,7 +19,7 @@ func NewServer(ctx context.Context, authpair []string) (h http.Handler, err erro
gofakes3.WithLogger(newLogger),
gofakes3.WithRequestID(rand.Uint64()),
gofakes3.WithoutVersioning(),
gofakes3.WithV4Auth(authlistResolver(authpair)),
gofakes3.WithV4Auth(authlistResolver()),
gofakes3.WithIntegrityCheck(true), // Check Content-MD5 if supplied
)

View File

@ -5,7 +5,6 @@ package s3
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/Mikubill/gofakes3"
@ -15,7 +14,6 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/pkg/utils"
)
type Bucket struct {
@ -150,15 +148,13 @@ func prefixParser(p *gofakes3.Prefix) (path, remaining string) {
// }
// }
func authlistResolver(list []string) map[string]string {
authList := make(map[string]string)
for _, v := range list {
parts := strings.Split(v, ",")
if len(parts) != 2 {
utils.Log.Infof(fmt.Sprintf("Ignored: invalid auth pair %s", v))
continue
}
authList[parts[0]] = parts[1]
func authlistResolver() map[string]string {
s3accesskeyid := setting.GetStr(conf.S3AccessKeyId)
s3secretaccesskey := setting.GetStr(conf.S3SecretAccessKey)
if s3accesskeyid == "" && s3secretaccesskey == "" {
return nil
}
authList := make(map[string]string)
authList[s3accesskeyid] = s3secretaccesskey
return authList
}