-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
62 lines (53 loc) · 3.05 KB
/
client.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
50
51
52
53
54
55
56
57
58
59
60
61
62
package s3batchstore
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
// Client is the client used to store and fetch object to/from s3.
// K represents the type of IDs for the objects that will be uploaded
// To create a new file, first call NewTempFile, then append objects to it, and finally call UploadFile.
// After the file is uploaded, you can save the object indexes to a database, and use them to fetch the objects later.
// To fetch the contents of a single object, call Fetch with the ObjectIndex that you had stored.
//
//go:generate mockgen -source=./client.go -destination=./mock/client/mock_client.go -package=mocks3batchstore Client
type Client[K comparable] interface {
// NewTempFile creates a new file in a temp folder.
// tags can be used to store information about this file in S3, like retention days
// The file itself is not thread safe, if you expect to make concurrent calls to Append, you should protect it.
// Once all the objects are appended, you can call UploadFile to upload the file to s3.
NewTempFile(tags map[string]string) (*TempFile[K], error)
// UploadFile will take a TempFile that already has all the objects in it, and upload it to a s3 file,
// in one single operation.
// withMetaFile indicates whether the metadata will be also uploaded to the file.MetaFileKey() location,
// with the index information for each object, or not.
UploadFile(ctx context.Context, file *TempFile[K], withMetaFile bool) error
// DeleteFile allows to try to delete any files that may have been uploaded to s3 based on the provided file.
// This is provided in case of any error when calling UploadFile, callers have the possibility to clean up the files.
DeleteFile(ctx context.Context, file *TempFile[K]) error
// Fetch downloads the payload from s3 given the ObjectIndex, fetching only the needed bytes, and returning
// the payload as a byte array.
// The caller is responsible for decompressing/unmarshalling or any operation needed to parse it to the proper struct.
Fetch(ctx context.Context, ind ObjectIndex) ([]byte, error)
}
// S3Client is used to mock the aws s3 functions used in this module.
//
//go:generate mockgen -destination=./mock/aws/mock_s3client.go -package=mocks3 . S3Client
type S3Client interface {
PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
DeleteObjects(ctx context.Context, params *s3.DeleteObjectsInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectsOutput, error)
GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
}
type client[K comparable] struct {
s3Client S3Client
s3Bucket string
}
// NewClient creates a new client that can be used to upload and download objects to s3.
// K represents the type of IDs for the objects that will be uploaded and fetched.
func NewClient[K comparable](awsConfig aws.Config, s3Bucket string) Client[K] {
s3Client := s3.NewFromConfig(awsConfig)
return &client[K]{
s3Client: s3Client,
s3Bucket: s3Bucket,
}
}