From fd99c5461c74b530cd7806b692b5e42e56842c0a Mon Sep 17 00:00:00 2001 From: Zeng1998 Date: Mon, 6 Mar 2023 20:04:19 +0800 Subject: [PATCH] feat(s3): customize filenames via placeholders (#1285) * feat(s3): customize filenames via placeholders * fix go-static-checks * add tips on the frontend * fix eslint check * remove yarn.lock * remove Config.Path * update tips * fix * update --- plugin/storage/s3/s3.go | 4 +--- server/resource.go | 21 +++++++++++++++++-- .../components/CreateStorageServiceDialog.tsx | 5 +++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/plugin/storage/s3/s3.go b/plugin/storage/s3/s3.go index 58fed5cd..5ab103fe 100644 --- a/plugin/storage/s3/s3.go +++ b/plugin/storage/s3/s3.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "path" "github.com/aws/aws-sdk-go-v2/aws" s3config "github.com/aws/aws-sdk-go-v2/config" @@ -19,7 +18,6 @@ type Config struct { SecretKey string Bucket string EndPoint string - Path string Region string URLPrefix string } @@ -57,7 +55,7 @@ func (client *Client) UploadFile(ctx context.Context, filename string, fileType uploader := manager.NewUploader(client.Client) uploadOutput, err := uploader.Upload(ctx, &awss3.PutObjectInput{ Bucket: aws.String(client.Config.Bucket), - Key: aws.String(path.Join(client.Config.Path, filename)), + Key: aws.String(filename), Body: src, ContentType: aws.String(fileType), ACL: types.ObjectCannedACL(*aws.String("public-read")), diff --git a/server/resource.go b/server/resource.go index be00dd4d..484d7817 100644 --- a/server/resource.go +++ b/server/resource.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/url" + "path" "strconv" "strings" "time" @@ -134,11 +135,27 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { if storage.Type == api.StorageS3 { s3Config := storage.Config.S3Config + t := time.Now() + s3FileKey := s3Config.Path + if s3Config.Path == "" { + s3FileKey = filename + } else if !strings.Contains(s3Config.Path, "{filename}") { + s3FileKey = path.Join(s3Config.Path, filename) + } else { + s3FileKey = strings.ReplaceAll(s3FileKey, "{filename}", filename) + s3FileKey = strings.ReplaceAll(s3FileKey, "{filetype}", filetype) + s3FileKey = strings.ReplaceAll(s3FileKey, "{timestamp}", fmt.Sprintf("%d", t.Unix())) + s3FileKey = strings.ReplaceAll(s3FileKey, "{year}", fmt.Sprintf("%d", t.Year())) + s3FileKey = strings.ReplaceAll(s3FileKey, "{month}", fmt.Sprintf("%02d", t.Month())) + s3FileKey = strings.ReplaceAll(s3FileKey, "{day}", fmt.Sprintf("%02d", t.Day())) + s3FileKey = strings.ReplaceAll(s3FileKey, "{hour}", fmt.Sprintf("%02d", t.Hour())) + s3FileKey = strings.ReplaceAll(s3FileKey, "{minute}", fmt.Sprintf("%02d", t.Minute())) + s3FileKey = strings.ReplaceAll(s3FileKey, "{second}", fmt.Sprintf("%02d", t.Second())) + } s3client, err := s3.NewClient(ctx, &s3.Config{ AccessKey: s3Config.AccessKey, SecretKey: s3Config.SecretKey, EndPoint: s3Config.EndPoint, - Path: s3Config.Path, Region: s3Config.Region, Bucket: s3Config.Bucket, URLPrefix: s3Config.URLPrefix, @@ -147,7 +164,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to new s3 client").SetInternal(err) } - link, err := s3client.UploadFile(ctx, filename, filetype, src) + link, err := s3client.UploadFile(ctx, s3FileKey, filetype, src) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upload via s3 client").SetInternal(err) } diff --git a/web/src/components/CreateStorageServiceDialog.tsx b/web/src/components/CreateStorageServiceDialog.tsx index acf160a7..60b04afe 100644 --- a/web/src/components/CreateStorageServiceDialog.tsx +++ b/web/src/components/CreateStorageServiceDialog.tsx @@ -185,6 +185,11 @@ const CreateStorageServiceDialog: React.FC = (props: Props) => { Path (Storage Path) + +

{"You can use {year}, {month}, {day}, {hour}, {minute}, {second},"}

+

{"{filetype}, {filename}, {timestamp} and any other words."}

+

{"e.g., {year}/{month}/{day}/your/path/{filename}.{filetype}"}

+