2
0

s3connection.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package aws
  2. import (
  3. "context"
  4. "github.com/aws/aws-sdk-go-v2/aws"
  5. "github.com/aws/aws-sdk-go-v2/service/s3"
  6. "github.com/opencost/opencost/pkg/cloud"
  7. )
  8. type S3Connection struct {
  9. S3Configuration
  10. ConnectionStatus cloud.ConnectionStatus
  11. }
  12. func (s3c *S3Connection) GetStatus() cloud.ConnectionStatus {
  13. // initialize status if it has not done so; this can happen if the integration is inactive
  14. if s3c.ConnectionStatus.String() == "" {
  15. s3c.ConnectionStatus = cloud.InitialStatus
  16. }
  17. return s3c.ConnectionStatus
  18. }
  19. func (s3c *S3Connection) Equals(config cloud.Config) bool {
  20. thatConfig, ok := config.(*S3Connection)
  21. if !ok {
  22. return false
  23. }
  24. return s3c.S3Configuration.Equals(&thatConfig.S3Configuration)
  25. }
  26. func (s3c *S3Connection) GetS3Client() (*s3.Client, error) {
  27. cfg, err := s3c.CreateAWSConfig()
  28. if err != nil {
  29. return nil, err
  30. }
  31. return s3.NewFromConfig(cfg), nil
  32. }
  33. func (s3c *S3Connection) ListObjects(cli *s3.Client) (*s3.ListObjectsOutput, error) {
  34. objs, err := cli.ListObjects(context.TODO(), &s3.ListObjectsInput{
  35. Bucket: aws.String(s3c.Bucket),
  36. })
  37. if err != nil {
  38. return nil, err
  39. }
  40. return objs, err
  41. }