s3connection.go 1.2 KB

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