mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-09-20 13:30:46 +02:00
* chore(weed/storage/backend/s3_backend): remove unused function * fix(s3_backend): cache session under the composite region|endpoint key createSession looked up sessions by region|endpoint but stored them by region alone, so the cache never hit and a new session was built every call. With getSession gone the lock can also drop to a plain Mutex. --------- Co-authored-by: Chris Lu <chris.lu@gmail.com>
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
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/request"
|
|
"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"
|
|
"github.com/seaweedfs/seaweedfs/weed/util/version"
|
|
)
|
|
|
|
var (
|
|
s3Sessions = make(map[string]s3iface.S3API)
|
|
sessionsLock sync.Mutex
|
|
)
|
|
|
|
func createSession(awsAccessKeyId, awsSecretAccessKey, region, endpoint string, forcePathStyle bool) (s3iface.S3API, error) {
|
|
|
|
sessionsLock.Lock()
|
|
defer sessionsLock.Unlock()
|
|
|
|
cacheKey := fmt.Sprintf("%s|%s", region, endpoint)
|
|
if t, found := s3Sessions[cacheKey]; found {
|
|
return t, nil
|
|
}
|
|
|
|
config := &aws.Config{
|
|
Region: aws.String(region),
|
|
Endpoint: aws.String(endpoint),
|
|
S3ForcePathStyle: aws.Bool(forcePathStyle),
|
|
S3DisableContentMD5Validation: aws.Bool(true),
|
|
}
|
|
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)
|
|
}
|
|
sess.Handlers.Build.PushBack(func(r *request.Request) {
|
|
r.HTTPRequest.Header.Set("User-Agent", "SeaweedFS/"+version.VERSION_NUMBER)
|
|
})
|
|
|
|
t := s3.New(sess)
|
|
|
|
s3Sessions[cacheKey] = t
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
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
|
|
}
|