2019-11-19 03:24:34 +00:00
|
|
|
package s3_backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"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/service/s3"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3/s3iface"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-22 22:51:09 +00:00
|
|
|
s3Sessions = make(map[string]s3iface.S3API)
|
2019-11-19 03:24:34 +00:00
|
|
|
sessionsLock sync.RWMutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func getSession(region string) (s3iface.S3API, bool) {
|
|
|
|
sessionsLock.RLock()
|
|
|
|
defer sessionsLock.RUnlock()
|
|
|
|
|
|
|
|
sess, found := s3Sessions[region]
|
|
|
|
return sess, found
|
|
|
|
}
|
|
|
|
|
2020-03-20 04:13:56 +00:00
|
|
|
func createSession(awsAccessKeyId, awsSecretAccessKey, region, endpoint string) (s3iface.S3API, error) {
|
2019-11-19 03:24:34 +00:00
|
|
|
|
|
|
|
sessionsLock.Lock()
|
|
|
|
defer sessionsLock.Unlock()
|
|
|
|
|
|
|
|
if t, found := s3Sessions[region]; found {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &aws.Config{
|
2021-09-01 09:45:42 +00:00
|
|
|
Region: aws.String(region),
|
|
|
|
Endpoint: aws.String(endpoint),
|
|
|
|
S3ForcePathStyle: aws.Bool(true),
|
2021-08-26 00:34:29 +00:00
|
|
|
S3DisableContentMD5Validation: aws.Bool(true),
|
2019-11-19 03:24:34 +00:00
|
|
|
}
|
|
|
|
if awsAccessKeyId != "" && awsSecretAccessKey != "" {
|
|
|
|
config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
sess, err := session.NewSession(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create aws session in region %s: %v", region, err)
|
|
|
|
}
|
|
|
|
|
2019-11-22 22:51:09 +00:00
|
|
|
t := s3.New(sess)
|
2019-11-19 03:24:34 +00:00
|
|
|
|
|
|
|
s3Sessions[region] = t
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
|
2019-11-22 22:51:09 +00:00
|
|
|
}
|
2019-12-25 17:53:13 +00:00
|
|
|
|
|
|
|
func deleteFromS3(sess s3iface.S3API, sourceBucket string, sourceKey string) (err error) {
|
|
|
|
_, err = sess.DeleteObject(&s3.DeleteObjectInput{
|
|
|
|
Bucket: aws.String(sourceBucket),
|
|
|
|
Key: aws.String(sourceKey),
|
|
|
|
})
|
|
|
|
return err
|
2019-12-25 17:56:42 +00:00
|
|
|
}
|