You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

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