Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package s3
import (
"bytes"
"context"
"io"
"mime"
"net/http"
"path/filepath"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/pkg/errors"
"github.com/ulule/gostorages"
"io"
"mime"
"net/http"
"path/filepath"
)

type CustomAPIHTTPClient interface {
Expand All @@ -31,6 +32,7 @@ type Storage struct {
bucket string
s3 *s3.Client
uploader *manager.Uploader
acl types.ObjectCannedACL
}

// NewStorage returns a new Storage.
Expand All @@ -49,13 +51,18 @@ func NewStorage(cfg Config) (*Storage, error) {
}
})

if cfg.ACL == "" {
cfg.ACL = types.ObjectCannedACLPublicRead
}

if cfg.UploadConcurrency != nil {
uploaderopts = append(uploaderopts, withUploaderConcurrency(*cfg.UploadConcurrency))
}
return &Storage{
bucket: cfg.Bucket,
s3: client,
uploader: manager.NewUploader(client, uploaderopts...),
acl: cfg.ACL,
}, nil
}

Expand All @@ -66,6 +73,7 @@ type Config struct {
Endpoint string
Region string
SecretAccessKey string
ACL types.ObjectCannedACL

UploadConcurrency *int64

Expand All @@ -75,10 +83,10 @@ type Config struct {
// Save saves content to path.
func (s *Storage) Save(ctx context.Context, content io.Reader, path string) error {
input := &s3.PutObjectInput{
ACL: types.ObjectCannedACLPublicRead,
Body: content,
Bucket: aws.String(s.bucket),
Key: aws.String(path),
ACL: s.acl,
}

contenttype := mime.TypeByExtension(filepath.Ext(path)) // first, detect content type from extension
Expand Down