Skip to content

Commit

Permalink
feat: add s3 path-style switch for the storage
Browse files Browse the repository at this point in the history
Signed-off-by: saltbo <saltbo@foxmail.com>
  • Loading branch information
saltbo committed Jul 30, 2023
1 parent 98357f0 commit 0a30c6a
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions internal/app/entity/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Storage struct {
AccessKey string `json:"access_key" gorm:"size:64;not null"`
SecretKey string `json:"secret_key" gorm:"size:64;not null"`
CustomHost string `json:"custom_host" gorm:"size:128;not null"`
PathStyle bool `json:"path_style" gorm:"not null;default:false"`
RootPath string `json:"root_path" gorm:"size:64;not null"`
FilePath string `json:"file_path" gorm:"size:1024;not null"`
Status int8 `json:"status" gorm:"size:1;default:1;not null"`
Expand Down
8 changes: 4 additions & 4 deletions internal/app/repo/query/zp_matter.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion internal/app/repo/query/zp_storage.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions internal/app/repo/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/saltbo/zpan/internal/app/entity"
"github.com/saltbo/zpan/internal/app/repo/query"
Expand Down Expand Up @@ -46,18 +47,18 @@ func (s *StorageDBQuery) Create(ctx context.Context, storage *entity.Storage) er
}

func (s *StorageDBQuery) Update(ctx context.Context, id int64, storage *entity.Storage) error {
existStorage := new(entity.Storage)
if _, err := s.Find(ctx, id); errors.Is(err, gorm.ErrRecordNotFound) {
existStorage, err := s.Find(ctx, id)
if errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("storage not found")
}

storage.Id = id
// 如果SK没有发生改变则不允许更新SK,避免改错SK
if storage.SecretKey == existStorage.SKAsterisk() {
// 如果SK是掩码则忽略
if strings.HasPrefix(storage.SecretKey, "***") {
storage.SecretKey = existStorage.SecretKey
}

return s.q.Storage.WithContext(ctx).Save(storage)
_, err = s.q.Storage.WithContext(ctx).Where(s.q.Storage.Id.Eq(id)).Updates(storage)
return err
}

func (s *StorageDBQuery) Delete(ctx context.Context, id int64) error {
Expand Down
1 change: 1 addition & 0 deletions internal/app/usecase/storage/cloud_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ func (s *CloudStorage) buildConfig(storage *entity.Storage) *provider.Config {
CustomHost: storage.CustomHost,
AccessKey: storage.AccessKey,
AccessSecret: storage.SecretKey,
PathStyle: storage.PathStyle,
}
}
2 changes: 2 additions & 0 deletions internal/pkg/bind/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type StorageBody struct {
Region string `json:"region"`
AccessKey string `json:"access_key" binding:"required"`
SecretKey string `json:"secret_key" binding:"required"`
PathStyle bool `json:"path_style"`
Title string `json:"title"`
Status int8 `json:"status"`
IDirs string `json:"idirs"` // internal dirs
Expand All @@ -39,6 +40,7 @@ func (b *StorageBody) Model() *entity.Storage {
IDirs: ts(b.IDirs),
Bucket: ts(b.Bucket),
Provider: b.Provider,
PathStyle: b.PathStyle,
Endpoint: ts(b.Endpoint),
Region: ts(b.Region),
Status: b.Status,
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Config struct {
CustomHost string
AccessKey string
AccessSecret string
PathStyle bool
}

func (c *Config) Clone() *Config {
Expand All @@ -72,8 +73,8 @@ var supportProviders = map[string]Constructor{
"S3": NewS3Provider,
"US3": NewUS3Provider,
"USS": NewUSSProvider,
//"od": NewODProvider,
//"gd": NewGDProvider,
// "od": NewODProvider,
// "gd": NewGDProvider,
}

func New(conf *Config) (Provider, error) {
Expand Down
5 changes: 4 additions & 1 deletion internal/pkg/provider/provider_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func NewS3Provider(conf *Config) (Provider, error) {

func newS3Provider(conf *Config) (*S3Provider, error) {
cfg := aws.NewConfig().WithCredentials(credentials.NewStaticCredentials(conf.AccessKey, conf.AccessSecret, ""))
if conf.PathStyle {
cfg.WithS3ForcePathStyle(true)
}
s, err := session.NewSession(cfg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -149,7 +152,7 @@ func (p *S3Provider) Move(object, newObject string) error {
}

func (p *S3Provider) SignedPutURL(key, filetype string, filesize int64, public bool) (string, http.Header, error) {
acl := s3.ObjectCannedACLAuthenticatedRead
acl := s3.ObjectCannedACLPrivate
if public {
acl = s3.ObjectCannedACLPublicRead
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestSignedPutURL(t *testing.T) {
_, headers, err := disk.SignedPutURL(key, "text/plain", 0, false)
assert.NoError(t, err)

assert.Equal(t, s3.ObjectCannedACLAuthenticatedRead, headers.Get("x-amz-acl"))
assert.Equal(t, s3.ObjectCannedACLPrivate, headers.Get("x-amz-acl"))
assert.Equal(t, "text/plain", headers.Get("content-type"))
}

Expand Down

0 comments on commit 0a30c6a

Please sign in to comment.