s3connection.go 1010 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. return s3c.ConnectionStatus
  15. }
  16. func (s3c *S3Connection) Equals(config config.Config) bool {
  17. thatConfig, ok := config.(*S3Connection)
  18. if !ok {
  19. return false
  20. }
  21. return s3c.S3Configuration.Equals(&thatConfig.S3Configuration)
  22. }
  23. func (s3c *S3Connection) GetS3Client() (*s3.Client, error) {
  24. cfg, err := s3c.CreateAWSConfig()
  25. if err != nil {
  26. return nil, err
  27. }
  28. return s3.NewFromConfig(cfg), nil
  29. }
  30. func (s3c *S3Connection) ListObjects(cli *s3.Client) (*s3.ListObjectsOutput, error) {
  31. objs, err := cli.ListObjects(context.TODO(), &s3.ListObjectsInput{
  32. Bucket: aws.String(s3c.Bucket),
  33. })
  34. if err != nil {
  35. return nil, err
  36. }
  37. return objs, err
  38. }