Skip to content

Commit

Permalink
fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
y-miyazaki committed Jun 13, 2023
1 parent 423b27a commit cca9128
Show file tree
Hide file tree
Showing 14 changed files with 381 additions and 30 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ build-postgres:
GOOS=linux GOARCH=amd64 go build example/postgres/main.go
build-s3:
GOOS=linux GOARCH=amd64 go build example/s3/main.go
build-s3-v2:
GOOS=linux GOARCH=amd64 go build example/s3_v2/main.go
build-s3-v2-darwin:
GOOS=linux GOARCH=amd64 go build example/s3_v2/main.go
build-gin1:
GOOS=linux GOARCH=amd64 go build example/gin1/main.go
build-gin2:
Expand Down
6 changes: 3 additions & 3 deletions example/gin1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func main() {
s3Secret := os.Getenv("S3_SECRET")
s3Token := os.Getenv("S3_TOKEN")

s3Config := infrastructure.GetS3Config(log, s3ID, s3Secret, s3Token, s3Region, s3Endpoint, true)
sess := infrastructure.NewS3Session(&session.Options{
s3Config := infrastructure.GetAWSS3Config(log, s3ID, s3Secret, s3Token, s3Region, s3Endpoint, true)
sess := infrastructure.NewAWSS3Session(&session.Options{
SharedConfigState: session.SharedConfigEnable,
})
awsS3Repository := repository.NewAWSS3Repository(s3.New(sess, s3Config), sess)
Expand All @@ -138,7 +138,7 @@ func main() {
o := &redis.Options{
Addr: redisAddr,
Username: redisUsername,
Password: redisPassword,
Password: redisPassword, // pragma: allowlist secret
}
r := infrastructure.NewRedis(o)
redisRepository := repository.NewRedisRepository(r)
Expand Down
4 changes: 2 additions & 2 deletions example/s3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func main() {
s3Secret := os.Getenv("S3_SECRET")
s3Token := os.Getenv("S3_TOKEN")

s3Config := infrastructure.GetS3Config(l, s3ID, s3Secret, s3Token, s3Region, s3Endpoint, true)
sess := infrastructure.NewS3Session(&session.Options{
s3Config := infrastructure.GetAWSS3Config(l, s3ID, s3Secret, s3Token, s3Region, s3Endpoint, true)
sess := infrastructure.NewAWSS3Session(&session.Options{
SharedConfigState: session.SharedConfigEnable,
})

Expand Down
108 changes: 108 additions & 0 deletions example/s3_v2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"os"

"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/sirupsen/logrus"
"github.com/y-miyazaki/go-common/pkg/infrastructure"
"github.com/y-miyazaki/go-common/pkg/logger"
"github.com/y-miyazaki/go-common/pkg/repository"
"github.com/y-miyazaki/go-common/pkg/utils"
)

func main() {
// --------------------------------------------------------------
// logrus
// --------------------------------------------------------------
logrusLogger := &logrus.Logger{}
logrusLogger.Formatter = &logrus.JSONFormatter{}
logrusLogger.Out = os.Stdout
logrusLogger.Level, _ = logrus.ParseLevel("Info")
l := logger.NewLogger(logrusLogger)

// --------------------------------------------------------------
// S3(minio)
// --------------------------------------------------------------
s3Region := os.Getenv("S3_REGION")
s3Endpoint := os.Getenv("S3_ENDPOINT")
s3ID := os.Getenv("S3_ID")
s3Secret := os.Getenv("S3_SECRET")
s3Token := os.Getenv("S3_TOKEN")

s3Config, err := infrastructure.GetAWSV2S3Config(l, s3ID, s3Secret, s3Token, s3Region, s3Endpoint, true)
if err != nil {
panic(err)
}

// --------------------------------------------------------------
// example: S3
// --------------------------------------------------------------
awsS3Repository := repository.NewAWSV2S3Repository(s3.NewFromConfig(s3Config, func(o *s3.Options) {
o.UsePathStyle = true
}))
text := "abc"
bucket := "test"

// Create Bucket
_, err = awsS3Repository.CreateBucket(bucket)
if err != nil {
l.WithError(err).Errorf("can't create s3 bucket")
}

// ListBuckets
listBuckets, err := awsS3Repository.ListBuckets()
if err == nil {
for _, b := range listBuckets.Buckets {
l.Infof("bucket = %s(%s)", *b.Name, *b.CreationDate)
}
} else {
l.WithError(err).Errorf("can't list of s3 bucket")
}

// Put Object
_, err = awsS3Repository.PutObjectText(bucket, "test.txt", &text)
if err != nil {
l.WithError(err).Errorf("can't put s3 object")
}

// Get Object
object, err := awsS3Repository.GetObject(bucket, "test.txt")
if err != nil {
l.WithError(err).Errorf("can't get s3 object")
}
rc := object.Body
defer func() {
err = rc.Close()
if err != nil {
l.WithError(err).Errorf("can't close body")
}
}()
text, err = utils.GetStringFromReadCloser(rc)
if err != nil {
l.WithError(err).Errorf("can't get text")
}
l.Infof("text.txt = %s", text)

// ListObjectV2
listObjects, err := awsS3Repository.ListObjectsV2(bucket, "")
if err == nil {
for _, o := range listObjects.Contents {
l.Infof("Object key = %s", *o.Key)
}
} else {
l.WithError(err).Errorf("can't list of s3 object")
}

// Delete Object
_, err = awsS3Repository.DeleteObject(bucket, "test.txt")
if err != nil {
l.WithError(err).Errorf("can't delete s3 object")
}

// Delete Bucket
_, err = awsS3Repository.DeleteBucket(bucket)
if err != nil {
l.WithError(err).Errorf("can't delete s3 bucket")
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/aws/aws-sdk-go v1.44.242
github.com/aws/aws-sdk-go-v2 v1.18.0
github.com/aws/aws-sdk-go-v2/config v1.18.25
github.com/aws/aws-sdk-go-v2/credentials v1.13.24
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.67
github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider v1.22.10
github.com/aws/aws-sdk-go-v2/service/s3 v1.33.1
Expand Down
32 changes: 16 additions & 16 deletions pkg/infrastructure/aws_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ import (
"go.uber.org/zap"
)

// NewS3Session returns Session.
func NewS3Session(o *session.Options) *session.Session {
// NewAWSS3Session returns Session.
func NewAWSS3Session(o *session.Options) *session.Session {
return session.Must(session.NewSessionWithOptions(*o))
}

// NewS3 returns S3.
func NewS3(o *session.Options, c *aws.Config) *s3.S3 {
// NewAWSS3 returns S3.
func NewAWSS3(o *session.Options, c *aws.Config) *s3.S3 {
s := session.Must(session.NewSessionWithOptions(*o))
return s3.New(s, c)
}

// NewDownloader returns Downloader.
func NewDownloader(s *session.Session) *s3manager.Downloader {
// NewAWSS3Downloader returns Downloader.
func NewAWSS3Downloader(s *session.Session) *s3manager.Downloader {
return s3manager.NewDownloader(s)
}

// NewUploader returns Uploader.
func NewUploader(s *session.Session) *s3manager.Uploader {
// NewAWSS3Uploader returns Uploader.
func NewAWSS3Uploader(s *session.Session) *s3manager.Uploader {
return s3manager.NewUploader(s)
}

// GetS3Config get config.
func GetS3Config(l *logger.Logger, id, secret, token, region, endpoint string, isMinio bool) *aws.Config {
// GetAWSS3Config get config.
func GetAWSS3Config(l *logger.Logger, id, secret, token, region, endpoint string, isMinio bool) *aws.Config {
var httpClient *http.Client
if l != nil {
httpClient = &http.Client{
Expand All @@ -58,9 +58,9 @@ func GetS3Config(l *logger.Logger, id, secret, token, region, endpoint string, i
}
}

// GetS3ConfigNoCredentials get no credentials config.
// GetAWSS3ConfigNoCredentials get no credentials config.
// If AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are environment variables and are in the execution environment, Credentials is not required.
func GetS3ConfigNoCredentials(l *logger.Logger, region, endpoint string, isMinio bool) *aws.Config {
func GetAWSS3ConfigNoCredentials(l *logger.Logger, region, endpoint string, isMinio bool) *aws.Config {
var httpClient *http.Client
if l != nil {
httpClient = &http.Client{
Expand All @@ -79,8 +79,8 @@ func GetS3ConfigNoCredentials(l *logger.Logger, region, endpoint string, isMinio
}
}

// GetS3ConfigZap get config.
func GetS3ConfigZap(l *logger.ZapLogger, id, secret, token, region, endpoint string, isMinio bool) *aws.Config {
// GetAWSS3ConfigZap get config.
func GetAWSS3ConfigZap(l *logger.ZapLogger, id, secret, token, region, endpoint string, isMinio bool) *aws.Config {
var httpClient *http.Client
if l != nil {
httpClient = &http.Client{
Expand All @@ -103,9 +103,9 @@ func GetS3ConfigZap(l *logger.ZapLogger, id, secret, token, region, endpoint str
}
}

// GetS3ConfigNoCredentialsZap get no credentials config.
// GetAWSS3ConfigNoCredentialsZap get no credentials config.
// If AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are environment variables and are in the execution environment, Credentials is not required.
func GetS3ConfigNoCredentialsZap(l *logger.ZapLogger, region, endpoint string, isMinio bool) *aws.Config {
func GetAWSS3ConfigNoCredentialsZap(l *logger.ZapLogger, region, endpoint string, isMinio bool) *aws.Config {
var httpClient *http.Client
if l != nil {
httpClient = &http.Client{
Expand Down
18 changes: 9 additions & 9 deletions pkg/infrastructure/aws_ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
"go.uber.org/zap"
)

// NewSES returns ses instance.
func NewSES(
// NewAWSSES returns ses instance.
func NewAWSSES(
o *session.Options,
c *aws.Config,
) *ses.SES {
s := session.Must(session.NewSessionWithOptions(*o))
return ses.New(s, c)
}

// GetSESConfig get config.
func GetSESConfig(l *logger.Logger, id, secret, token, region, endpoint string) *aws.Config {
// GetAWSSESConfig get config.
func GetAWSSESConfig(l *logger.Logger, id, secret, token, region, endpoint string) *aws.Config {
var httpClient *http.Client
if l != nil {
httpClient = &http.Client{
Expand All @@ -42,9 +42,9 @@ func GetSESConfig(l *logger.Logger, id, secret, token, region, endpoint string)
}
}

// GetSESConfigNoCredentials get no credentials config.
// GetAWSSESConfigNoCredentials get no credentials config.
// If AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are environment variables and are in the execution environment, Credentials is not required.
func GetSESConfigNoCredentials(l *logger.Logger, region, endpoint string) *aws.Config {
func GetAWSSESConfigNoCredentials(l *logger.Logger, region, endpoint string) *aws.Config {
var httpClient *http.Client
if l != nil {
httpClient = &http.Client{
Expand All @@ -60,8 +60,8 @@ func GetSESConfigNoCredentials(l *logger.Logger, region, endpoint string) *aws.C
}
}

// GetSESConfigZap get config.
func GetSESConfigZap(l *logger.ZapLogger, id, secret, token, region, endpoint string) *aws.Config {
// GetAWSSESConfigZap get config.
func GetAWSSESConfigZap(l *logger.ZapLogger, id, secret, token, region, endpoint string) *aws.Config {
var httpClient *http.Client
if l != nil {
httpClient = &http.Client{
Expand All @@ -81,7 +81,7 @@ func GetSESConfigZap(l *logger.ZapLogger, id, secret, token, region, endpoint st
}
}

// GetSESConfigNoCredentialsZap get no credentials config.
// GetAWSSESConfigNoCredentialsZap get no credentials config.
// If AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are environment variables and are in the execution environment, Credentials is not required.
func GetSESConfigNoCredentialsZap(l *logger.ZapLogger, region, endpoint string) *aws.Config {
var httpClient *http.Client
Expand Down
Loading

0 comments on commit cca9128

Please sign in to comment.