Skip to content

Commit

Permalink
feat: add optional prefix and region fields to S3 configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
pteich committed Feb 18, 2025
1 parent c65c4f3 commit 7e3290e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type S3Config struct {
AccessKey string `cli:"accesskey" env:"ACCESS_KEY"`
SecretKey string `cli:"secretkey" env:"SECRET_KEY"`
Bucket string `cli:"bucket" env:"BUCKET"`
Prefix string `cli:"prefix" env:"PREFIX"`
Region string `cli:"region" env:"REGION"`
UseSSL bool `cli:"usessl" env:"USE_SSL"`
}

Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func main() {
secretKeyEntry.Password = true
bucketEntry := widget.NewEntry()
bucketEntry.SetText(cfg.Bucket)
prefixEntry := widget.NewEntry()
prefixEntry.SetPlaceHolder("Optional Prefix")
regionEntry := widget.NewEntry()
regionEntry.SetPlaceHolder("Optional Region")
sslCheck := widget.NewCheck("Use SSL (HTTPS)", nil)
sslCheck.SetChecked(cfg.UseSSL)

Expand All @@ -57,13 +61,17 @@ func main() {
{Text: "Access Key", Widget: accessKeyEntry},
{Text: "Secret Key", Widget: secretKeyEntry},
{Text: "Bucket Name", Widget: bucketEntry},
{Text: "Region", Widget: regionEntry},
{Text: "Prefix", Widget: prefixEntry},
},
OnSubmit: func() {
cfg.Endpoint = endpointEntry.Text
cfg.AccessKey = accessKeyEntry.Text
cfg.SecretKey = secretKeyEntry.Text
cfg.Bucket = bucketEntry.Text
cfg.UseSSL = sslCheck.Checked
cfg.Prefix = prefixEntry.Text
cfg.Region = regionEntry.Text

s3svc, err := s3.New(cfg)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions s3/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ import (
type Service struct {
client *minio.Client
bucketName string
prefix string
}

func New(cfg config.S3Config) (*Service, error) {
client, err := minio.New(cfg.Endpoint, &minio.Options{
Region: cfg.Region,
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
Secure: cfg.UseSSL,
})
if err != nil {
return nil, fmt.Errorf("failed to connect to server: %w", err)
}
return &Service{client, cfg.Bucket}, nil
return &Service{client: client, bucketName: cfg.Bucket, prefix: cfg.Prefix}, nil
}

func (s *Service) ListObjects(ctx context.Context) ([]minio.ObjectInfo, error) {
Expand All @@ -50,6 +52,7 @@ func (s *Service) DeleteObject(ctx context.Context, objectName string) error {
func (s *Service) UploadObject(filePath string, data []byte) error {
ctx := context.Background()
objectName := filepath.Base(filePath)

_, err := s.client.PutObject(ctx, s.bucketName, objectName,
bytes.NewReader(data),
int64(len(data)),
Expand All @@ -65,7 +68,7 @@ func (s *Service) DownloadObject(ctx context.Context, objectName string) (io.Rea

func (s *Service) ListObjectsBatch(ctx context.Context, startAfter string, batchSize int) ([]minio.ObjectInfo, error) {
opts := minio.ListObjectsOptions{
Prefix: "",
Prefix: s.prefix,
Recursive: true,
StartAfter: startAfter,
}
Expand Down

0 comments on commit 7e3290e

Please sign in to comment.