// Package audiostore keeps the review recordings in an S3 bucket, so they // can be played back next to their feedback. In production that is a // private bucket on web_server's Garage. // // It is optional. Git is the site's store and a recording is about a // megabyte a minute, too big to commit, so without the S3_* settings the // audio is transcribed and thrown away and only the review is kept. package audiostore import ( "bytes" "context" "errors" "fmt" "io" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/credentials" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" ) // ErrNotFound is a key with no object. var ErrNotFound = errors.New("no such recording") // Config is the S3_* block of the environment. type Config struct { Endpoint string // e.g. http://garage:3900 Region string // must match garage.toml's s3_region Bucket string AccessKeyID string SecretAccessKey string } // Store is one bucket. type Store struct { client *s3.Client bucket string } // New returns nil, and the audio is thrown away, unless every setting is // given: half a config is more likely a typo than a wish to store nothing. func New(cfg Config) (*Store, error) { set := 0 for _, v := range []string{cfg.Endpoint, cfg.Region, cfg.Bucket, cfg.AccessKeyID, cfg.SecretAccessKey} { if v != "" { set++ } } switch set { case 0: return nil, nil case 5: default: return nil, errors.New("S3_ENDPOINT, S3_REGION, S3_BUCKET, S3_ACCESS_KEY_ID and S3_SECRET_ACCESS_KEY must be set together") } // The same options as web_server's backend uses for Garage. client := s3.New(s3.Options{ BaseEndpoint: aws.String(cfg.Endpoint), Region: cfg.Region, Credentials: credentials.NewStaticCredentialsProvider(cfg.AccessKeyID, cfg.SecretAccessKey, ""), // Garage addresses buckets by path (/bucket/key); virtual-host style // would need a DNS name per bucket. UsePathStyle: true, // Recent SDKs add CRC32 checksums to every PUT by default. Only send // them where S3 requires them, which is what third-party servers // handle reliably. RequestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired, ResponseChecksumValidation: aws.ResponseChecksumValidationWhenRequired, }) return &Store{client: client, bucket: cfg.Bucket}, nil } // Bucket is where everything goes. func (s *Store) Bucket() string { return s.bucket } // Garage is on the same Docker network, so anything near this is a hang. const timeout = 2 * time.Minute // Put stores one recording. func (s *Store) Put(ctx context.Context, key, contentType string, b []byte) error { ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() _, err := s.client.PutObject(ctx, &s3.PutObjectInput{ Bucket: aws.String(s.bucket), Key: aws.String(key), Body: bytes.NewReader(b), ContentLength: aws.Int64(int64(len(b))), ContentType: aws.String(contentType), }) if err != nil { return fmt.Errorf("s3 put %s/%s: %w", s.bucket, key, err) } return nil } // Object is a recording being read; the caller closes Body. type Object struct { Body io.ReadCloser ContentType string Size int64 } // Get opens one recording. func (s *Store) Get(ctx context.Context, key string) (*Object, error) { out, err := s.client.GetObject(ctx, &s3.GetObjectInput{Bucket: aws.String(s.bucket), Key: aws.String(key)}) if err != nil { var nk *types.NoSuchKey if errors.As(err, &nk) { return nil, ErrNotFound } return nil, fmt.Errorf("s3 get %s/%s: %w", s.bucket, key, err) } return &Object{Body: out.Body, ContentType: aws.ToString(out.ContentType), Size: aws.ToInt64(out.ContentLength)}, nil } // Delete removes one recording. A missing one is not an error: S3 does not // report it either. func (s *Store) Delete(ctx context.Context, key string) error { ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() if _, err := s.client.DeleteObject(ctx, &s3.DeleteObjectInput{Bucket: aws.String(s.bucket), Key: aws.String(key)}); err != nil { return fmt.Errorf("s3 delete %s/%s: %w", s.bucket, key, err) } return nil }