Skip to content

Commit

Permalink
Merge pull request #77 from 88labs/feat/aess3-Presign
Browse files Browse the repository at this point in the history
feat: s3 presign: add option
  • Loading branch information
bushiyama authored Feb 13, 2023
2 parents e9287f3 + 64faa77 commit fa94fdb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
16 changes: 11 additions & 5 deletions aws/awss3/awss3.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,13 @@ func Presign(ctx context.Context, region awsconfig.Region, bucketName BucketName
if err != nil {
return "", err
}
// @todo: not been able to test with and without option. create a separate function for input settings.
input := &s3.GetObjectInput{
Bucket: bucketName.AWSString(),
Key: key.AWSString(),
}
if c.PresignFileName != "" {
input.ResponseContentDisposition = aws.String(ResponseContentDisposition(c.PresignFileName))
input.ResponseContentDisposition = aws.String(ResponseContentDisposition(c.ContentDispositionType, c.PresignFileName))
}
ps := s3.NewPresignClient(client)
resp, err := ps.PresignGetObject(ctx, input, func(o *s3.PresignOptions) {
Expand All @@ -268,10 +269,15 @@ func Presign(ctx context.Context, region awsconfig.Region, bucketName BucketName
return resp.URL, nil
}

// ResponseContentDisposition
// Setting ResponseContentDisposition to support file names with multibyte characters
func ResponseContentDisposition(fileName string) string {
return fmt.Sprintf(`attachment; filename*=UTF-8''%s`, url.PathEscape(fileName))
func ResponseContentDisposition(tp s3presigned.ContentDispositionType, fileName string) string {
var dispositionType string
switch tp {
case s3presigned.ContentDispositionTypeAttachment:
dispositionType = "attachment"
case s3presigned.ContentDispositionTypeInline:
dispositionType = "inline"
}
return fmt.Sprintf(`%s; filename*=UTF-8''%s`, dispositionType, url.PathEscape(fileName))
}

// Copy copies an Amazon S3 object from one bucket to same.
Expand Down
3 changes: 2 additions & 1 deletion aws/awss3/awss3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/88labs/go-utils/aws/awsconfig"
"github.com/88labs/go-utils/aws/awss3"
"github.com/88labs/go-utils/aws/awss3/options/s3download"
"github.com/88labs/go-utils/aws/awss3/options/s3presigned"
"github.com/88labs/go-utils/aws/ctxawslocal"
"github.com/88labs/go-utils/ulid"
)
Expand Down Expand Up @@ -288,7 +289,7 @@ func TestPresign(t *testing.T) {
func TestResponseContentDisposition(t *testing.T) {
const fileName = ",あいうえお 牡蠣喰家来 サシスセソ@+$_-^|+{}"
t.Run("success", func(t *testing.T) {
actual := awss3.ResponseContentDisposition(fileName)
actual := awss3.ResponseContentDisposition(s3presigned.ContentDispositionTypeAttachment, fileName)
assert.NotEmpty(t, actual)
})
}
Expand Down
21 changes: 19 additions & 2 deletions aws/awss3/options/s3presigned/s3_presigned.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ type OptionS3Presigned interface {
}

type confS3Presigned struct {
PresignFileName string
PresignExpires time.Duration
ContentDispositionType ContentDispositionType
PresignExpires time.Duration
PresignFileName string
}
type ContentDispositionType int

const (
ContentDispositionTypeAttachment ContentDispositionType = iota
ContentDispositionTypeInline
)

// nolint:revive
func GetS3PresignedConf(opts ...OptionS3Presigned) confS3Presigned {
Expand Down Expand Up @@ -44,3 +51,13 @@ func (o OptionPresignExpires) Apply(c *confS3Presigned) {
func WithPresignExpires(presignExpires time.Duration) OptionPresignExpires {
return OptionPresignExpires(presignExpires)
}

type OptionContentDispositionType ContentDispositionType

func (o OptionContentDispositionType) Apply(c *confS3Presigned) {
c.ContentDispositionType = ContentDispositionType(o)
}

func WithContentDispositionType(tp ContentDispositionType) OptionContentDispositionType {
return OptionContentDispositionType(tp)
}

0 comments on commit fa94fdb

Please sign in to comment.