-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy paths3_blob_store.go
49 lines (42 loc) · 1.29 KB
/
s3_blob_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package blobstore
import (
"context"
"github.com/Layr-Labs/eigenda/common/aws/s3"
"github.com/Layr-Labs/eigenda/disperser/common"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/pkg/errors"
)
type BlobStore struct {
bucketName string
s3Client s3.Client
logger logging.Logger
}
func NewBlobStore(s3BucketName string, s3Client s3.Client, logger logging.Logger) *BlobStore {
return &BlobStore{
bucketName: s3BucketName,
s3Client: s3Client,
logger: logger,
}
}
// StoreBlob adds a blob to the blob store
func (b *BlobStore) StoreBlob(ctx context.Context, blobKey string, data []byte) error {
err := b.s3Client.UploadObject(ctx, b.bucketName, blobKey, data)
if err != nil {
b.logger.Errorf("failed to upload blob in bucket %s: %v", b.bucketName, err)
return err
}
return nil
}
// GetBlob retrieves a blob from the blob store
func (b *BlobStore) GetBlob(ctx context.Context, blobKey string) ([]byte, error) {
data, err := b.s3Client.DownloadObject(ctx, b.bucketName, blobKey)
if errors.Is(err, s3.ErrObjectNotFound) {
b.logger.Warnf("blob not found in bucket %s: %s", b.bucketName, blobKey)
return nil, common.ErrBlobNotFound
}
if err != nil {
b.logger.Errorf("failed to download blob from bucket %s: %v", b.bucketName, err)
return nil, err
}
return data, nil
}