2018-01-31 02:15:21 +01:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2020-10-15 02:59:12 +02:00
|
|
|
"github.com/lbryio/reflector.go/internal/metrics"
|
2018-02-07 21:21:20 +01:00
|
|
|
|
2018-01-31 02:15:21 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2018-02-07 21:21:20 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2018-01-31 02:15:21 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-10-22 19:12:31 +02:00
|
|
|
// S3Store is an S3 store
|
|
|
|
type S3Store struct {
|
2018-01-31 02:15:21 +01:00
|
|
|
awsID string
|
|
|
|
awsSecret string
|
|
|
|
region string
|
|
|
|
bucket string
|
|
|
|
|
|
|
|
session *session.Session
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:12:31 +02:00
|
|
|
// NewS3Store returns an initialized S3 store pointer.
|
|
|
|
func NewS3Store(awsID, awsSecret, region, bucket string) *S3Store {
|
|
|
|
return &S3Store{
|
2018-01-31 02:15:21 +01:00
|
|
|
awsID: awsID,
|
|
|
|
awsSecret: awsSecret,
|
|
|
|
region: region,
|
|
|
|
bucket: bucket,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:49:02 +02:00
|
|
|
const nameS3 = "s3"
|
2018-01-31 02:15:21 +01:00
|
|
|
|
2020-10-22 19:49:02 +02:00
|
|
|
// Name is the cache type name
|
|
|
|
func (s *S3Store) Name() string { return nameS3 }
|
2018-01-31 02:15:21 +01:00
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Has returns T/F or Error ( from S3 ) if the store contains the blob.
|
2020-10-22 19:12:31 +02:00
|
|
|
func (s *S3Store) Has(hash string) (bool, error) {
|
2018-01-31 02:15:21 +01:00
|
|
|
err := s.initOnce()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s3.New(s.session).HeadObject(&s3.HeadObjectInput{
|
|
|
|
Bucket: aws.String(s.bucket),
|
|
|
|
Key: aws.String(hash),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if reqFail, ok := err.(s3.RequestFailure); ok && reqFail.StatusCode() == http.StatusNotFound {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Get returns the blob slice if present or errors on S3.
|
2020-10-22 19:12:31 +02:00
|
|
|
func (s *S3Store) Get(hash string) (stream.Blob, error) {
|
2018-05-30 03:38:55 +02:00
|
|
|
//Todo-Need to handle error for blob doesn't exist for consistency.
|
2018-01-31 02:15:21 +01:00
|
|
|
err := s.initOnce()
|
|
|
|
if err != nil {
|
2019-10-03 22:34:57 +02:00
|
|
|
return nil, err
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 21:21:20 +01:00
|
|
|
log.Debugf("Getting %s from S3", hash[:8])
|
|
|
|
defer func(t time.Time) {
|
2018-05-13 22:02:46 +02:00
|
|
|
log.Debugf("Getting %s from S3 took %s", hash[:8], time.Since(t).String())
|
2018-02-07 21:21:20 +01:00
|
|
|
}(time.Now())
|
|
|
|
|
2018-01-31 02:15:21 +01:00
|
|
|
buf := &aws.WriteAtBuffer{}
|
|
|
|
_, err = s3manager.NewDownloader(s.session).Download(buf, &s3.GetObjectInput{
|
|
|
|
Bucket: aws.String(s.bucket),
|
|
|
|
Key: aws.String(hash),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-02-07 21:21:20 +01:00
|
|
|
if aerr, ok := err.(awserr.Error); ok {
|
|
|
|
switch aerr.Code() {
|
|
|
|
case s3.ErrCodeNoSuchBucket:
|
2019-10-03 22:34:57 +02:00
|
|
|
return nil, errors.Err("bucket %s does not exist", s.bucket)
|
2018-02-07 21:21:20 +01:00
|
|
|
case s3.ErrCodeNoSuchKey:
|
2019-10-03 22:34:57 +02:00
|
|
|
return nil, errors.Err(ErrBlobNotFound)
|
2018-02-07 21:21:20 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-31 02:15:21 +01:00
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Put stores the blob on S3 or errors if S3 connection errors.
|
2020-10-22 19:12:31 +02:00
|
|
|
func (s *S3Store) Put(hash string, blob stream.Blob) error {
|
2018-01-31 02:15:21 +01:00
|
|
|
err := s.initOnce()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Uploading %s to S3", hash[:8])
|
|
|
|
defer func(t time.Time) {
|
2018-02-07 21:21:20 +01:00
|
|
|
log.Debugf("Uploading %s took %s", hash[:8], time.Since(t).String())
|
2018-01-31 02:15:21 +01:00
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
_, err = s3manager.NewUploader(s.session).Upload(&s3manager.UploadInput{
|
2018-12-26 21:05:32 +01:00
|
|
|
Bucket: aws.String(s.bucket),
|
|
|
|
Key: aws.String(hash),
|
|
|
|
Body: bytes.NewBuffer(blob),
|
|
|
|
StorageClass: aws.String(s3.StorageClassIntelligentTiering),
|
2018-01-31 02:15:21 +01:00
|
|
|
})
|
2020-10-15 02:59:12 +02:00
|
|
|
metrics.MtrOutBytesReflector.Add(float64(blob.Size()))
|
2018-01-31 02:15:21 +01:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2018-02-02 22:49:20 +01:00
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// PutSD stores the sd blob on S3 or errors if S3 connection errors.
|
2020-10-22 19:12:31 +02:00
|
|
|
func (s *S3Store) PutSD(hash string, blob stream.Blob) error {
|
2018-05-30 03:38:55 +02:00
|
|
|
//Todo - handle missing stream for consistency
|
2018-02-02 22:49:20 +01:00
|
|
|
return s.Put(hash, blob)
|
|
|
|
}
|
2018-09-11 13:41:29 +02:00
|
|
|
|
2020-10-22 19:12:31 +02:00
|
|
|
func (s *S3Store) Delete(hash string) error {
|
2018-09-11 13:41:29 +02:00
|
|
|
err := s.initOnce()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Deleting %s from S3", hash[:8])
|
|
|
|
|
|
|
|
_, err = s3.New(s.session).DeleteObject(&s3.DeleteObjectInput{
|
|
|
|
Bucket: aws.String(s.bucket),
|
|
|
|
Key: aws.String(hash),
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 19:49:02 +02:00
|
|
|
|
|
|
|
func (s *S3Store) initOnce() error {
|
|
|
|
if s.session != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sess, err := session.NewSession(&aws.Config{
|
|
|
|
Credentials: credentials.NewStaticCredentials(s.awsID, s.awsSecret, ""),
|
|
|
|
Region: aws.String(s.region),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.session = sess
|
|
|
|
return nil
|
|
|
|
}
|