s3_sessions.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package s3_backend
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws/request"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. "sync"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/credentials"
  9. "github.com/aws/aws-sdk-go/aws/session"
  10. "github.com/aws/aws-sdk-go/service/s3"
  11. "github.com/aws/aws-sdk-go/service/s3/s3iface"
  12. )
  13. var (
  14. s3Sessions = make(map[string]s3iface.S3API)
  15. sessionsLock sync.RWMutex
  16. )
  17. func getSession(region string) (s3iface.S3API, bool) {
  18. sessionsLock.RLock()
  19. defer sessionsLock.RUnlock()
  20. sess, found := s3Sessions[region]
  21. return sess, found
  22. }
  23. func createSession(awsAccessKeyId, awsSecretAccessKey, region, endpoint string) (s3iface.S3API, error) {
  24. sessionsLock.Lock()
  25. defer sessionsLock.Unlock()
  26. if t, found := s3Sessions[region]; found {
  27. return t, nil
  28. }
  29. config := &aws.Config{
  30. Region: aws.String(region),
  31. Endpoint: aws.String(endpoint),
  32. S3ForcePathStyle: aws.Bool(true),
  33. S3DisableContentMD5Validation: aws.Bool(true),
  34. }
  35. if awsAccessKeyId != "" && awsSecretAccessKey != "" {
  36. config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
  37. }
  38. sess, err := session.NewSession(config)
  39. if err != nil {
  40. return nil, fmt.Errorf("create aws session in region %s: %v", region, err)
  41. }
  42. sess.Handlers.Build.PushBack(func(r *request.Request) {
  43. r.HTTPRequest.Header.Set("User-Agent", "SeaweedFS/"+util.VERSION_NUMBER)
  44. })
  45. t := s3.New(sess)
  46. s3Sessions[region] = t
  47. return t, nil
  48. }
  49. func deleteFromS3(sess s3iface.S3API, sourceBucket string, sourceKey string) (err error) {
  50. _, err = sess.DeleteObject(&s3.DeleteObjectInput{
  51. Bucket: aws.String(sourceBucket),
  52. Key: aws.String(sourceKey),
  53. })
  54. return err
  55. }