Skip to content

Commit

Permalink
feat(server): support AWS S3 storage (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
yk-eukarya authored May 30, 2023
1 parent 2002db2 commit 8c56c74
Show file tree
Hide file tree
Showing 9 changed files with 435 additions and 30 deletions.
6 changes: 4 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa
github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70=
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
github.com/aws/aws-sdk-go v1.35.5 h1:doSEOxC0UkirPcle20Rc+1kAhJ4Ip+GSEeZ3nKl7Qlk=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.3 h1:jJPgroehGvjrde3XufFIJUZVK5A2L9a3KwSFgKy9n8w=
github.com/aws/aws-sdk-go-v2/service/sso v1.12.10 h1:UBQjaMTCKwyUYwiVnUt6toEJwGXsLBI6al083tpjJzY=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.10 h1:PkHIIJs8qvq0e5QybnZoG1K/9QTrLr9OsqCIo59jOBA=
github.com/aws/aws-sdk-go-v2/service/sts v1.19.0 h1:2DQLAKDteoEDI8zpCzqBMaZlJuoE9iTYD0gFmXVax9E=
github.com/bradfitz/gomemcache v0.0.0-20170208213004-1952afaa557d h1:7IjN4QP3c38xhg6wz8R3YjoU+6S9e7xBc0DAVLLIpHE=
github.com/census-instrumentation/opencensus-proto v0.2.1 h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
Expand Down Expand Up @@ -62,8 +66,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d h1:uGg2frl
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/inconshreveable/log15 v0.0.0-20170622235902-74a0988b5f80 h1:g/SJtZVYc1cxSB8lgrgqeOlIdi4MhqNNHYRAC8y+g4c=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jpillora/opts v1.2.3 h1:Q0YuOM7y0BlunHJ7laR1TUxkUA7xW8A2rciuZ70xs8g=
github.com/jstemmer/go-junit-report v0.9.1 h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfEUpgAwUN0o=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
Expand Down
12 changes: 12 additions & 0 deletions server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ REEARTH_SES_NAME=
#REEARTH_SMTP_PORT=587
#REEARTH_SMTP_SMTPUSERNAME=apikey
#REEARTH_SMTP_PASSWORD=Your_SendGrid_Token

# Available Storage: [FS, S3, GCS]
# if you want to use S3 or GCS, you need to set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, GOOGLE_APPLICATION_CREDENTIALS
# if you want to use local storage (FS), you must keep S3 and GCS empty

# Storage AWS S3
#REEARTH_S3_BUCKETNAME=bucket_name
#REEARTH_S3_PUBLICATIONCACHECONTROL=

# Storage Google GCS
#REEARTH_GCS_BUCKETNAME=bucket_name
#REEARTH_GCS_PUBLICATIONCACHECONTROL=
10 changes: 8 additions & 2 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ require (
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.8.3
github.com/auth0/go-jwt-middleware/v2 v2.0.1
github.com/avast/retry-go/v4 v4.0.4
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/service/s3 v1.33.1
github.com/blang/semver v3.5.1+incompatible
github.com/gavv/httpexpect/v2 v2.3.1
github.com/goccy/go-yaml v1.9.8
Expand Down Expand Up @@ -64,14 +67,17 @@ require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a // indirect
github.com/andybalholm/brotli v1.0.2 // indirect
github.com/aws/aws-sdk-go-v2 v1.18.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.18.25 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.24 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.33 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.27 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.34 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.25 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.11 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.28 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.27 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.2 // indirect
github.com/aws/aws-sdk-go-v2/service/ses v1.15.9 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.12.10 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.10 // indirect
Expand Down
12 changes: 12 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/
github.com/aws/aws-sdk-go v1.35.5/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k=
github.com/aws/aws-sdk-go-v2 v1.18.0 h1:882kkTpSFhdgYRKVZ/VCgf7sd0ru57p2JCxz4/oN5RY=
github.com/aws/aws-sdk-go-v2 v1.18.0/go.mod h1:uzbQtefpm44goOPmdKyAlXSNcwlRgF3ePWVW6EtJvvw=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 h1:dK82zF6kkPeCo8J1e+tGx4JdvDIQzj7ygIoLg8WMuGs=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10/go.mod h1:VeTZetY5KRJLuD/7fkQXMU6Mw7H5m/KP2J5Iy9osMno=
github.com/aws/aws-sdk-go-v2/config v1.18.25 h1:JuYyZcnMPBiFqn87L2cRppo+rNwgah6YwD3VuyvaW6Q=
github.com/aws/aws-sdk-go-v2/config v1.18.25/go.mod h1:dZnYpD5wTW/dQF0rRNLVypB396zWCcPiBIvdvSWHEg4=
github.com/aws/aws-sdk-go-v2/credentials v1.13.24 h1:PjiYyls3QdCrzqUN35jMWtUK1vqVZ+zLfdOa/UPFDp0=
Expand All @@ -116,8 +118,18 @@ github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.27 h1:vFQlirhuM8lLlpI7im
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.27/go.mod h1:UrHnn3QV/d0pBZ6QBAEQcqFLf8FAzLmoUfPVIueOvoM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.34 h1:gGLG7yKaXG02/jBlg210R7VgQIotiQntNhsCFejawx8=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.34/go.mod h1:Etz2dj6UHYuw+Xw830KfzCfWGMzqvUTCjUj5b76GVDc=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.25 h1:AzwRi5OKKwo4QNqPf7TjeO+tK8AyOK3GVSwmRPo7/Cs=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.25/go.mod h1:SUbB4wcbSEyCvqBxv/O/IBf93RbEze7U7OnoTlpPB+g=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.11 h1:y2+VQzC6Zh2ojtV2LoC0MNwHWc6qXv/j2vrQtlftkdA=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.11/go.mod h1:iV4q2hsqtNECrfmlXyord9u4zyuFEJX9eLgLpSPzWA8=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.28 h1:vGWm5vTpMr39tEZfQeDiDAMgk+5qsnvRny3FjLpnH5w=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.28/go.mod h1:spfrICMD6wCAhjhzHuy6DOZZ+LAIY10UxhUmLzpJTTs=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.27 h1:0iKliEXAcCa2qVtRs7Ot5hItA2MsufrphbRFlz1Owxo=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.27/go.mod h1:EOwBD4J4S5qYszS5/3DpkejfuK+Z5/1uzICfPaZLtqw=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.2 h1:NbWkRxEEIRSCqxhsHQuMiTH7yo+JZW1gp8v3elSVMTQ=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.2/go.mod h1:4tfW5l4IAB32VWCDEBxCRtR9T4BWy4I4kr1spr8NgZM=
github.com/aws/aws-sdk-go-v2/service/s3 v1.33.1 h1:O+9nAy9Bb6bJFTpeNFtd9UfHbgxO1o4ZDAM9rQp5NsY=
github.com/aws/aws-sdk-go-v2/service/s3 v1.33.1/go.mod h1:J9kLNzEiHSeGMyN7238EjJmBpCniVzFda75Gxl/NqB8=
github.com/aws/aws-sdk-go-v2/service/ses v1.15.9 h1:A1ZTDmy+2MX6eDuOEGMCfz/nnF2sQQCd6LW6If0zKwU=
github.com/aws/aws-sdk-go-v2/service/ses v1.15.9/go.mod h1:E2M33oyRMZ09o88tPSF0qJpiZTFoKx4x8UWjiSZhRAI=
github.com/aws/aws-sdk-go-v2/service/sso v1.12.10 h1:UBQjaMTCKwyUYwiVnUt6toEJwGXsLBI6al083tpjJzY=
Expand Down
5 changes: 4 additions & 1 deletion server/internal/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type Config struct {
Profiler string
Tracer string
TracerSample float64
GCS GCSConfig
Marketplace MarketplaceConfig
AssetBaseURL string `default:"http://localhost:8080/assets"`
Origins []string
Expand All @@ -45,6 +44,10 @@ type Config struct {
SignupDisabled bool
HTTPSREDIRECT bool

// storage
GCS GCSConfig
S3 S3Config

// auth
Auth AuthConfigs
Auth0 Auth0Config
Expand Down
19 changes: 19 additions & 0 deletions server/internal/app/config/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

type GCSConfig struct {
BucketName string
PublicationCacheControl string
}

func (g GCSConfig) IsConfigured() bool {
return g.BucketName != ""
}

type S3Config struct {
BucketName string
PublicationCacheControl string
}

func (s S3Config) IsConfigured() bool {
return s.BucketName != ""
}
6 changes: 0 additions & 6 deletions server/internal/app/config/gcs.go

This file was deleted.

51 changes: 32 additions & 19 deletions server/internal/app/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/reearth/reearth/server/internal/infrastructure/google"
"github.com/reearth/reearth/server/internal/infrastructure/marketplace"
mongorepo "github.com/reearth/reearth/server/internal/infrastructure/mongo"
"github.com/reearth/reearth/server/internal/infrastructure/s3"
"github.com/reearth/reearth/server/internal/usecase/gateway"
"github.com/reearth/reearth/server/internal/usecase/repo"
"github.com/reearth/reearthx/log"
Expand Down Expand Up @@ -44,25 +45,7 @@ func initReposAndGateways(ctx context.Context, conf *config.Config, debug bool)
}

// File
datafs := afero.NewBasePathFs(afero.NewOsFs(), "data")
var fileRepo gateway.File
if conf.GCS.BucketName == "" {
log.Infoln("file: local storage is used")
fileRepo, err = fs.NewFile(datafs, conf.AssetBaseURL)
} else {
log.Infof("file: GCS storage is used: %s\n", conf.GCS.BucketName)
fileRepo, err = gcs.NewFile(conf.GCS.BucketName, conf.AssetBaseURL, conf.GCS.PublicationCacheControl)
if err != nil {
if debug {
log.Warnf("file: failed to init GCS storage: %s\n", err.Error())
err = nil
}
}
}
if err != nil {
log.Fatalln(fmt.Sprintf("file: init error: %+v", err))
}
gateways.File = fileRepo
gateways.File = initFile(ctx, conf)

// Auth0
gateways.Authenticator = auth0.New(conf.Auth0.Domain, conf.Auth0.ClientID, conf.Auth0.ClientSecret)
Expand All @@ -85,3 +68,33 @@ func initReposAndGateways(ctx context.Context, conf *config.Config, debug bool)

return repos, gateways
}

func initFile(ctx context.Context, conf *config.Config) (fileRepo gateway.File) {
var err error
if conf.GCS.IsConfigured() {
log.Infof("file: GCS storage is used: %s\n", conf.GCS.BucketName)
fileRepo, err = gcs.NewFile(conf.GCS.BucketName, conf.AssetBaseURL, conf.GCS.PublicationCacheControl)
if err != nil {
log.Warnf("file: failed to init GCS storage: %s\n", err.Error())
}
return

}

if conf.S3.IsConfigured() {
log.Infof("file: S3 storage is used: %s\n", conf.S3.BucketName)
fileRepo, err = s3.NewS3(ctx, conf.S3.BucketName, conf.AssetBaseURL, conf.S3.PublicationCacheControl)
if err != nil {
log.Warnf("file: failed to init S3 storage: %s\n", err.Error())
}
return
}

log.Infoln("file: local storage is used")
afs := afero.NewBasePathFs(afero.NewOsFs(), "data")
fileRepo, err = fs.NewFile(afs, conf.AssetBaseURL)
if err != nil {
log.Fatalln(fmt.Sprintf("file: init error: %+v", err))
}
return fileRepo
}
Loading

0 comments on commit 8c56c74

Please sign in to comment.