Skip to content

Commit

Permalink
Reworking s3
Browse files Browse the repository at this point in the history
  • Loading branch information
AlfieJones committed Mar 3, 2024
1 parent 780eb73 commit a14993b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 48 deletions.
51 changes: 33 additions & 18 deletions apps/backend/platform/storage/bucket_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ type BucketClient struct {
// nolint:gochecknoglobals
var globalClient *BucketClient

// nolint:gochecknoglobals
var globalS3Client *s3.Client

func getConfig() (aws.Config, error) {
var endpoint = os.Getenv("S3_ENDPOINT")
func getConfig(endpoint string) (aws.Config, error) {
var accessKeyId = os.Getenv("S3_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("S3_KEY_SECRET")

Expand Down Expand Up @@ -63,34 +59,53 @@ func getConfig() (aws.Config, error) {
return cfg, err
}

func getS3Client() (*s3.Client, error) {
if globalS3Client == nil {
func getS3Clients() (*s3.Client, *s3.Client, error) {

sdkConfig, err := getConfig()
sdkConfig, err := getConfig(os.Getenv("S3_ENDPOINT"))
if err != nil {
return nil, nil, err
}

if err != nil {
return nil, err
s3Client := s3.NewFromConfig(sdkConfig, func(o *s3.Options) {
if os.Getenv("PIXELEYE_HOSTING") != "true" {
o.UsePathStyle = true
}
})

globalS3Client = s3.NewFromConfig(sdkConfig, func(o *s3.Options) {
if os.Getenv("PIXELEYE_HOSTING") != "true" {
o.UsePathStyle = true
}
})
externalEndpoint := os.Getenv("CLIENT_S3_ENDPOINT")
if externalEndpoint == "" {
return s3Client, nil, nil
}
return globalS3Client, nil

sdkExternalConfig, err := getConfig(externalEndpoint)
if err != nil {
return nil, nil, err
}

s3ExternalClient := s3.NewFromConfig(sdkExternalConfig, func(o *s3.Options) {
if os.Getenv("PIXELEYE_HOSTING") != "true" {
o.UsePathStyle = true
}
})

return s3Client, s3ExternalClient, nil
}

func GetClient() (*BucketClient, error) {

if globalClient == nil {
s3Client, err := getS3Client()
s3Client, s3ExternalClient, err := getS3Clients()

if err != nil {
return nil, err
}

presignClient := s3.NewPresignClient(s3Client)
var presignClient *s3.PresignClient
if s3ExternalClient != nil {
presignClient = s3.NewPresignClient(s3ExternalClient)
} else {
presignClient = s3.NewPresignClient(s3Client)
}

globalClient = &BucketClient{
PresignClient: presignClient,
Expand Down
30 changes: 0 additions & 30 deletions apps/backend/platform/storage/bucket_presigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,14 @@ package storage

import (
"context"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/service/s3"
)

// A bit of a hack to ensure when running in docker, external clients can access the S3 endpoint
func replaceS3Endpoint(request *v4.PresignedHTTPRequest) {

if os.Getenv("PIXELEYE_HOSTING") == "true" {
return
}

s3Endpoint := os.Getenv("S3_ENDPOINT")
clientS3Endpoint := os.Getenv("CLIENT_S3_ENDPOINT")

if s3Endpoint == "" || clientS3Endpoint == "" {
return
}

if strings.HasPrefix(request.URL, s3Endpoint) {
if strings.HasPrefix(s3Endpoint, "https") {
request.URL = strings.Replace(request.URL, s3Endpoint, fmt.Sprintf("https://%s", clientS3Endpoint), 1)
} else {
request.URL = strings.Replace(request.URL, s3Endpoint, fmt.Sprintf("http://%s", clientS3Endpoint), 1)
}
}
}

// GetObject makes a presigned request that can be used to get an object from a bucket.
// The presigned request is valid for the specified number of seconds.
func (presigner BucketClient) GetObject(ctx context.Context,
Expand All @@ -51,8 +25,6 @@ func (presigner BucketClient) GetObject(ctx context.Context,
bucketName, objectKey, err)
}

replaceS3Endpoint(request)

return request, err
}

Expand All @@ -72,7 +44,5 @@ func (presigner BucketClient) PutObject(ctx context.Context,
bucketName, objectKey, err)
}

replaceS3Endpoint(request)

return request, err
}

0 comments on commit a14993b

Please sign in to comment.