Add support for custom endpoints and keys for S3.

This commit is contained in:
Gabriel Simmer 2021-05-29 19:37:45 +01:00
parent 550e722a53
commit 8fff6da76b
3 changed files with 42 additions and 19 deletions

View file

@ -57,7 +57,7 @@ func (f FileProvider) GetDirectory(path string) Directory {
return Directory{} return Directory{}
} }
// RemoteFile will bypass http.ServeContent() and instead write directly to the response. // SendFile returns a filestream, a valid MIME type for the file and an error.
func (f FileProvider) SendFile(path string) (stream io.Reader, contenttype string, err error) { func (f FileProvider) SendFile(path string) (stream io.Reader, contenttype string, err error) {
return return
} }

View file

@ -19,7 +19,24 @@ func TranslateProvider(codename string, i *FileProviderInterface) {
} }
if provider.Provider == "s3" { if provider.Provider == "s3" {
s3Prov := &S3Provider{provider, provider.Config["region"], provider.Config["bucket"]} s3Prov := &S3Provider{
FileProvider: provider,
Region: provider.Config["region"],
Bucket: provider.Config["bucket"],
Endpoint: "",
KeyID: "",
KeySecret: "",
}
if _, ok := provider.Config["endpoint"]; ok {
s3Prov.Endpoint = provider.Config["endpoint"]
}
if _, ok := provider.Config["keyid"]; ok {
s3Prov.KeyID = provider.Config["keyid"]
}
if _, ok := provider.Config["keysecret"]; ok {
s3Prov.KeySecret = provider.Config["keysecret"]
}
*i = s3Prov *i = s3Prov
return return
} }
@ -40,4 +57,4 @@ func SetupProviders() {
fmt.Printf("%s initialized successfully\n", name) fmt.Printf("%s initialized successfully\n", name)
} }
} }
} }

View file

@ -4,11 +4,12 @@ import (
"fmt" "fmt"
"io" "io"
"mime" "mime"
"path/filepath"
"net/http" "net/http"
"path/filepath"
// I _really_ don't want to deal with AWS API stuff by hand. // I _really_ don't want to deal with AWS API stuff by hand.
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3"
) )
@ -18,16 +19,26 @@ var sess *session.Session
type S3Provider struct { type S3Provider struct {
FileProvider FileProvider
Region string Region string
Bucket string Bucket string
Endpoint string
KeyID string
KeySecret string
} }
// Setup runs when the application starts up, and allows for things like authentication. // Setup runs when the application starts up, and allows for things like authentication.
func (s *S3Provider) Setup(args map[string]string) bool { func (s *S3Provider) Setup(args map[string]string) bool {
sess, err := session.NewSession(&aws.Config{ config := &aws.Config{Region: aws.String(s.Region)}
Region: aws.String(s.Region)}, if s.KeyID != "" && s.KeySecret != "" {
) config = &aws.Config{
Region: aws.String(s.Region),
Credentials: credentials.NewStaticCredentials(s.KeyID, s.KeySecret, ""),
}
}
if s.Endpoint != "" {
config.Endpoint = &s.Endpoint
}
sess, err := session.NewSession(config)
if err != nil { if err != nil {
return false return false
} }
@ -40,7 +51,7 @@ func (s *S3Provider) GetDirectory(path string) Directory {
resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(s.Bucket)}) resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(s.Bucket)})
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return Directory{} return Directory{}
} }
dir := Directory{} dir := Directory{}
@ -56,7 +67,7 @@ func (s *S3Provider) GetDirectory(path string) Directory {
} }
file := FileInfo{ file := FileInfo{
IsDirectory: false, IsDirectory: false,
Name: *item.Key, Name: *item.Key,
} }
dir.Files = append(dir.Files, file) dir.Files = append(dir.Files, file)
} }
@ -64,11 +75,10 @@ func (s *S3Provider) GetDirectory(path string) Directory {
return dir return dir
} }
// RemoteFile will bypass http.ServeContent() and instead write directly to the response.
func (s *S3Provider) SendFile(path string) (stream io.Reader, contenttype string, err error) { func (s *S3Provider) SendFile(path string) (stream io.Reader, contenttype string, err error) {
req, err := svc.GetObject(&s3.GetObjectInput{ req, err := svc.GetObject(&s3.GetObjectInput{
Bucket: &s.Bucket, Bucket: &s.Bucket,
Key: &path, Key: &path,
}) })
if err != nil { if err != nil {
return stream, contenttype, err return stream, contenttype, err
@ -84,14 +94,10 @@ func (s *S3Provider) SendFile(path string) (stream io.Reader, contenttype string
return req.Body, contenttype, err return req.Body, contenttype, err
} }
// SaveFile will save a file with the contents of the io.Reader at the path specified.
func (s *S3Provider) SaveFile(file io.Reader, filename string, path string) bool { func (s *S3Provider) SaveFile(file io.Reader, filename string, path string) bool {
return false return false
} }
// ObjectInfo will return the info for an object given a path to if the file exists and location.
// Should return whether the path exists, if the path is a directory, and if it lives on disk.
// (see constants defined: `FILE_IS_REMOTE` and `FILE_IS_LOCAL`)
func (s *S3Provider) ObjectInfo(path string) (bool, bool, string) { func (s *S3Provider) ObjectInfo(path string) (bool, bool, string) {
if path == "" { if path == "" {
return true, true, "" return true, true, ""
@ -99,7 +105,7 @@ func (s *S3Provider) ObjectInfo(path string) (bool, bool, string) {
_, err := svc.GetObject(&s3.GetObjectInput{ _, err := svc.GetObject(&s3.GetObjectInput{
Bucket: &s.Bucket, Bucket: &s.Bucket,
Key: &path, Key: &path,
}) })
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)