From ca67d0e6f5aa765a17d8b2b9e8d697f9be365782 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 4 Jun 2022 22:22:27 +0200 Subject: [PATCH] detect servers S3 implementation --- pkg/server/server.go | 34 ++++++++++++++++++++++++++++++++++ pkg/server/setup.go | 2 ++ 2 files changed, 36 insertions(+) diff --git a/pkg/server/server.go b/pkg/server/server.go index 7a8feab..c0c80d1 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -1,17 +1,31 @@ package server import ( + "net/http" "net/url" + "strings" "github.com/aws/aws-sdk-go/service/s3" "github.com/stv0g/gose/pkg/config" ) +type Implementation string + +const ( + ImplementationAWS = "AmazonS3" + ImplementationMinio = "MinIO" + ImplementationGoogleCloudStorage = "UploadServer" + ImplementationDigitalOceanSpaces = "DigitalOceanSpaces" + ImplementationUnknown = "Unknown" +) + // Server is a abstraction of an S3 server/bucket type Server struct { *s3.S3 Config *config.S3Server + + Implementation Implementation } // GetURL returns the full endpoint URL of the S3 server @@ -53,3 +67,23 @@ func (s *Server) GetExpirationClass(cls string) *config.Expiration { return nil } + +func (s *Server) DetectImplementation() Implementation { + if strings.Contains(s.Config.Endpoint, "digitaloceanspaces.com") { + return ImplementationDigitalOceanSpaces + } else if strings.Contains(s.Config.Endpoint, "storage.googleapis.com") { + return ImplementationGoogleCloudStorage + } else { + u := s.GetObjectURL("not-existing") + + if r, err := http.Get(u.String()); err != nil { + return ImplementationUnknown + } else { + if svr := r.Header.Get("Server"); svr == "" { + return ImplementationUnknown + } else { + return Implementation(svr) + } + } + } +} diff --git a/pkg/server/setup.go b/pkg/server/setup.go index 3196155..6588be9 100644 --- a/pkg/server/setup.go +++ b/pkg/server/setup.go @@ -9,6 +9,8 @@ import ( // Setup initializes the S3 bucket (life-cycle rules & CORS) func (s *Server) Setup() error { + s.Implementation = s.DetectImplementation() + // Create bucket if it does not exist yet if _, err := s.GetBucketPolicy(&s3.GetBucketPolicyInput{ Bucket: aws.String(s.Config.Bucket),