Skip to content

Commit

Permalink
Merge pull request #116 from nabbar/aws_obj_website
Browse files Browse the repository at this point in the history
# Package AWS : 
- add method for website object (update redirection & acl)
- add method to update metadata (copy object)
- fix bug with missing content-type (all defined as application/octet-stream)

# Other: 
- Bump dependancies
- fix formating import
  • Loading branch information
Nicolas JUHEL authored Jan 17, 2022
2 parents 74be9a7 + 7fbcf39 commit d094a0d
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 56 deletions.
4 changes: 2 additions & 2 deletions aws/object/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ package object
import (
"regexp"

"github.com/nabbar/golib/errors"
liberr "github.com/nabbar/golib/errors"
)

func (cli *client) Find(pattern string) ([]string, errors.Error) {
func (cli *client) Find(pattern string) ([]string, liberr.Error) {
var (
result = make([]string, 0)
token = ""
Expand Down
29 changes: 16 additions & 13 deletions aws/object/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,36 @@ import (
sdkiam "github.com/aws/aws-sdk-go-v2/service/iam"
sdksss "github.com/aws/aws-sdk-go-v2/service/s3"
sdktps "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/nabbar/golib/aws/helper"
"github.com/nabbar/golib/errors"
libhlp "github.com/nabbar/golib/aws/helper"
liberr "github.com/nabbar/golib/errors"
)

type client struct {
helper.Helper
libhlp.Helper
iam *sdkiam.Client
s3 *sdksss.Client
}

type Object interface {
Find(pattern string) ([]string, errors.Error)
Size(object string) (size int64, err errors.Error)
Find(pattern string) ([]string, liberr.Error)
Size(object string) (size int64, err liberr.Error)

List(continuationToken string) ([]sdktps.Object, string, int64, errors.Error)
Head(object string) (*sdksss.HeadObjectOutput, errors.Error)
Get(object string) (*sdksss.GetObjectOutput, errors.Error)
Put(object string, body io.Reader) errors.Error
Delete(object string) errors.Error
List(continuationToken string) ([]sdktps.Object, string, int64, liberr.Error)
Head(object string) (*sdksss.HeadObjectOutput, liberr.Error)
Get(object string) (*sdksss.GetObjectOutput, liberr.Error)
Put(object string, body io.Reader) liberr.Error
Delete(object string) liberr.Error

MultipartPut(object string, body io.Reader) errors.Error
MultipartPutCustom(partSize helper.PartSize, object string, body io.Reader) errors.Error
MultipartPut(object string, body io.Reader) liberr.Error
MultipartPutCustom(partSize libhlp.PartSize, object string, body io.Reader) liberr.Error

UpdateMetadata(meta *sdksss.CopyObjectInput) liberr.Error
SetWebsite(object, redirect string) liberr.Error
}

func New(ctx context.Context, bucket, region string, iam *sdkiam.Client, s3 *sdksss.Client) Object {
return &client{
Helper: helper.New(ctx, bucket, region),
Helper: libhlp.New(ctx, bucket, region),
iam: iam,
s3: s3,
}
Expand Down
14 changes: 12 additions & 2 deletions aws/object/multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"crypto/md5"
"encoding/base64"
"io"
"mime"
"path/filepath"

//nolint #gci
"os"
Expand All @@ -55,6 +57,7 @@ func (cli *client) MultipartPutCustom(partSize libhlp.PartSize, object string, b
rio libhlp.ReaderPartSize
upl *sdksss.CreateMultipartUploadOutput
err error
tpe *string
)

defer func() {
Expand All @@ -63,9 +66,16 @@ func (cli *client) MultipartPutCustom(partSize libhlp.PartSize, object string, b
}
}()

if t := mime.TypeByExtension(filepath.Ext(object)); t == "" {
tpe = sdkaws.String("application/octet-stream")
} else {
tpe = sdkaws.String(t)
}

upl, err = cli.s3.CreateMultipartUpload(cli.GetContext(), &sdksss.CreateMultipartUploadInput{
Key: sdkaws.String(object),
Bucket: sdkaws.String(cli.GetBucketName()),
Key: sdkaws.String(object),
Bucket: sdkaws.String(cli.GetBucketName()),
ContentType: tpe,
})

if err != nil {
Expand Down
72 changes: 58 additions & 14 deletions aws/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ package object

import (
"io"
"mime"
"path/filepath"

sdkaws "github.com/aws/aws-sdk-go-v2/aws"
sdksss "github.com/aws/aws-sdk-go-v2/service/s3"
sdktps "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/nabbar/golib/aws/helper"
"github.com/nabbar/golib/errors"
libhlp "github.com/nabbar/golib/aws/helper"
liberr "github.com/nabbar/golib/errors"
)

func (cli *client) List(continuationToken string) ([]sdktps.Object, string, int64, errors.Error) {
func (cli *client) List(continuationToken string) ([]sdktps.Object, string, int64, liberr.Error) {
in := sdksss.ListObjectsV2Input{
Bucket: cli.GetBucketAws(),
}
Expand All @@ -55,7 +57,7 @@ func (cli *client) List(continuationToken string) ([]sdktps.Object, string, int6
}
}

func (cli *client) Get(object string) (*sdksss.GetObjectOutput, errors.Error) {
func (cli *client) Get(object string) (*sdksss.GetObjectOutput, liberr.Error) {
out, err := cli.s3.GetObject(cli.GetContext(), &sdksss.GetObjectInput{
Bucket: cli.GetBucketAws(),
Key: sdkaws.String(object),
Expand All @@ -69,13 +71,13 @@ func (cli *client) Get(object string) (*sdksss.GetObjectOutput, errors.Error) {
}()
return nil, cli.GetError(err)
} else if out.Body == nil {
return nil, helper.ErrorResponse.Error(nil)
return nil, libhlp.ErrorResponse.Error(nil)
} else {
return out, nil
}
}

func (cli *client) Head(object string) (*sdksss.HeadObjectOutput, errors.Error) {
func (cli *client) Head(object string) (*sdksss.HeadObjectOutput, liberr.Error) {
out, e := cli.s3.HeadObject(cli.GetContext(), &sdksss.HeadObjectInput{
Bucket: cli.GetBucketAws(),
Key: sdkaws.String(object),
Expand All @@ -84,13 +86,13 @@ func (cli *client) Head(object string) (*sdksss.HeadObjectOutput, errors.Error)
if e != nil {
return nil, cli.GetError(e)
} else if out.ETag == nil {
return nil, helper.ErrorResponse.Error(nil)
return nil, libhlp.ErrorResponse.Error(nil)
} else {
return out, nil
}
}

func (cli *client) Size(object string) (size int64, err errors.Error) {
func (cli *client) Size(object string) (size int64, err liberr.Error) {
var (
h *sdksss.HeadObjectOutput
)
Expand All @@ -102,23 +104,32 @@ func (cli *client) Size(object string) (size int64, err errors.Error) {
}
}

func (cli *client) Put(object string, body io.Reader) errors.Error {
func (cli *client) Put(object string, body io.Reader) liberr.Error {
var tpe *string

if t := mime.TypeByExtension(filepath.Ext(object)); t == "" {
tpe = sdkaws.String("application/octet-stream")
} else {
tpe = sdkaws.String(t)
}

out, err := cli.s3.PutObject(cli.GetContext(), &sdksss.PutObjectInput{
Bucket: cli.GetBucketAws(),
Key: sdkaws.String(object),
Body: body,
Bucket: cli.GetBucketAws(),
Key: sdkaws.String(object),
Body: body,
ContentType: tpe,
})

if err != nil {
return cli.GetError(err)
} else if out.ETag == nil {
return helper.ErrorResponse.Error(nil)
return libhlp.ErrorResponse.Error(nil)
}

return nil
}

func (cli *client) Delete(object string) errors.Error {
func (cli *client) Delete(object string) liberr.Error {
if _, err := cli.Head(object); err != nil {
return err
}
Expand All @@ -130,3 +141,36 @@ func (cli *client) Delete(object string) errors.Error {

return cli.GetError(err)
}

func (cli *client) UpdateMetadata(meta *sdksss.CopyObjectInput) liberr.Error {
_, err := cli.s3.CopyObject(cli.GetContext(), meta)

return cli.GetError(err)
}

func (cli *client) SetWebsite(object, redirect string) liberr.Error {
var err error

_, err = cli.s3.PutObjectAcl(cli.GetContext(), &sdksss.PutObjectAclInput{
Bucket: cli.GetBucketAws(),
Key: sdkaws.String(object),
ACL: sdktps.ObjectCannedACLPublicRead,
})

if err != nil {
return cli.GetError(err)
}

if redirect == "" {
return nil
}

meta := &sdksss.CopyObjectInput{
Bucket: cli.GetBucketAws(),
CopySource: sdkaws.String(cli.GetBucketName() + "/" + object),
Key: sdkaws.String(object),
WebsiteRedirectLocation: sdkaws.String(redirect),
}

return cli.UpdateMetadata(meta)
}
42 changes: 21 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/nabbar/golib
go 1.17

require (
github.com/aws/aws-sdk-go-v2 v1.12.0
github.com/aws/aws-sdk-go-v2/config v1.12.0
github.com/aws/aws-sdk-go-v2/credentials v1.7.0
github.com/aws/aws-sdk-go-v2/service/iam v1.15.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.23.0
github.com/aws/aws-sdk-go-v2 v1.13.0
github.com/aws/aws-sdk-go-v2/config v1.13.0
github.com/aws/aws-sdk-go-v2/credentials v1.8.0
github.com/aws/aws-sdk-go-v2/service/iam v1.16.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.24.0
github.com/c-bata/go-prompt v0.2.6
github.com/fatih/color v1.13.0
github.com/fxamacker/cbor/v2 v2.4.0
Expand All @@ -20,11 +20,11 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/go-version v1.4.0
github.com/lni/dragonboat/v3 v3.3.4
github.com/lni/dragonboat/v3 v3.3.5
github.com/matcornic/hermes/v2 v2.1.0
github.com/mattn/go-colorable v0.1.12
github.com/nats-io/jwt/v2 v2.2.0
github.com/nats-io/nats-server/v2 v2.6.6
github.com/nats-io/jwt/v2 v2.2.1-0.20220113022732-58e87895b296
github.com/nats-io/nats-server/v2 v2.7.0
github.com/nats-io/nats.go v1.13.1-0.20211122170419-d7c1d78a50fc
github.com/onsi/ginkgo/v2 v2.0.0
github.com/onsi/gomega v1.17.0
Expand All @@ -36,10 +36,10 @@ require (
github.com/xhit/go-simple-mail v2.2.2+incompatible
github.com/xujiajun/nutsdb v0.6.0
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b
golang.org/x/net v0.0.0-20220111093109-d55c255bac03
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220111092808-5a964db01320
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
)

Expand All @@ -55,17 +55,17 @@ require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/aokoli/goutils v1.1.1 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.1.0 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.9.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.1.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.6.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.6.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.10.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.13.0 // indirect
github.com/aws/smithy-go v1.9.1 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.2.0 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.10.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.4 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.2.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.7.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.7.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.11.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.9.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.14.0 // indirect
github.com/aws/smithy-go v1.10.0 // indirect
github.com/bwmarrin/snowflake v0.3.0 // indirect
github.com/cockroachdb/errors v1.7.5 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
Expand Down
9 changes: 5 additions & 4 deletions mail/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ package mail

import (
"fmt"
"net/textproto"

libval "github.com/go-playground/validator/v10"
liberr "github.com/nabbar/golib/errors"
"github.com/nabbar/golib/ioutils"
"net/textproto"
libiot "github.com/nabbar/golib/ioutils"
)

type Config struct {
Expand Down Expand Up @@ -140,7 +141,7 @@ func (c Config) NewMailer() (Mail, liberr.Error) {

if len(c.Attach) > 0 {
for _, f := range c.Attach {
if h, e := ioutils.NewFileProgressPathRead(f.Path, 0); e != nil {
if h, e := libiot.NewFileProgressPathRead(f.Path, 0); e != nil {
return nil, e
} else {
m.AddAttachment(f.Name, f.Mime, h, false)
Expand All @@ -150,7 +151,7 @@ func (c Config) NewMailer() (Mail, liberr.Error) {

if len(c.Inline) > 0 {
for _, f := range c.Inline {
if h, e := ioutils.NewFileProgressPathRead(f.Path, 0); e != nil {
if h, e := libiot.NewFileProgressPathRead(f.Path, 0); e != nil {
return nil, e
} else {
m.AddAttachment(f.Name, f.Mime, h, true)
Expand Down

0 comments on commit d094a0d

Please sign in to comment.