From 9d4bb5b3af0d619f49bae8f94b5de100e521e19b Mon Sep 17 00:00:00 2001 From: Alex Zhao Date: Sat, 4 Mar 2023 07:59:44 +0800 Subject: [PATCH] feat: add support for s3 path (#1233) * add support for path * fix typo and switch positions with Path and Bucket * using path method instead of string concatenation --- api/storage.go | 1 + plugin/storage/s3/s3.go | 4 +++- server/resource.go | 1 + web/src/components/CreateStorageServiceDialog.tsx | 12 ++++++++++++ web/src/types/modules/storage.d.ts | 1 + 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/api/storage.go b/api/storage.go index d4518354..1acedc86 100644 --- a/api/storage.go +++ b/api/storage.go @@ -12,6 +12,7 @@ type StorageConfig struct { type StorageS3Config struct { EndPoint string `json:"endPoint"` + Path string `json:"path"` Region string `json:"region"` AccessKey string `json:"accessKey"` SecretKey string `json:"secretKey"` diff --git a/plugin/storage/s3/s3.go b/plugin/storage/s3/s3.go index d7b93697..e00e9605 100644 --- a/plugin/storage/s3/s3.go +++ b/plugin/storage/s3/s3.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "path" "github.com/aws/aws-sdk-go-v2/aws" s3config "github.com/aws/aws-sdk-go-v2/config" @@ -18,6 +19,7 @@ type Config struct { SecretKey string Bucket string EndPoint string + Path string Region string URLPrefix string } @@ -55,7 +57,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(filename), + Key: aws.String(path.Join(client.Config.Path, 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 2e16def3..83157928 100644 --- a/server/resource.go +++ b/server/resource.go @@ -139,6 +139,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { AccessKey: s3Config.AccessKey, SecretKey: s3Config.SecretKey, EndPoint: s3Config.EndPoint, + Path: s3Config.Path, Region: s3Config.Region, Bucket: s3Config.Bucket, URLPrefix: s3Config.URLPrefix, diff --git a/web/src/components/CreateStorageServiceDialog.tsx b/web/src/components/CreateStorageServiceDialog.tsx index 92cd2b96..196293fa 100644 --- a/web/src/components/CreateStorageServiceDialog.tsx +++ b/web/src/components/CreateStorageServiceDialog.tsx @@ -21,6 +21,7 @@ const CreateStorageServiceDialog: React.FC = (props: Props) => { region: "", accessKey: "", secretKey: "", + path: "", bucket: "", urlPrefix: "", }); @@ -181,6 +182,17 @@ const CreateStorageServiceDialog: React.FC = (props: Props) => { onChange={(e) => setPartialS3Config({ bucket: e.target.value })} fullWidth /> + + Path + (Storage Path) + + setPartialS3Config({ path: e.target.value })} + fullWidth + /> URLPrefix (Custom URL prefix; Optional) diff --git a/web/src/types/modules/storage.d.ts b/web/src/types/modules/storage.d.ts index 2221678f..22e0d447 100644 --- a/web/src/types/modules/storage.d.ts +++ b/web/src/types/modules/storage.d.ts @@ -7,6 +7,7 @@ interface StorageS3Config { region: string; accessKey: string; secretKey: string; + path: string; bucket: string; urlPrefix: string; }