Skip to content

Commit

Permalink
detect servers S3 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
stv0g committed Jun 4, 2022
1 parent 4a9c7b5 commit ca67d0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
}
}
2 changes: 2 additions & 0 deletions pkg/server/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit ca67d0e

Please sign in to comment.