s3browser-backend/internal/loader/batch.go
2021-10-14 19:00:11 +02:00

136 lines
3.1 KiB
Go

package loader
import (
"context"
"fmt"
types "git.kapelle.org/niklas/s3browser/internal/types"
"github.com/graph-gophers/dataloader"
"github.com/minio/minio-go/v7"
log "github.com/sirupsen/logrus"
)
// listObjectsBatch batch func for calling s3.ListObjects()
func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
log.Debug("listObjectsBatch: ", k.Keys())
var results []*dataloader.Result
s3Client, ok := c.Value("s3Client").(*minio.Client)
if !ok {
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
}
for _, v := range k {
id := v.Raw().(types.ID)
results = append(results, &dataloader.Result{
Data: listObjects(s3Client, id, false),
Error: nil,
})
}
return results
}
// listObjectsRecursiveBatch just like listObjectsBatch but with recursive set to true
func listObjectsRecursiveBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
log.Debug("listObjectsRecursiveBatch: ", k.Keys())
var results []*dataloader.Result
s3Client, ok := c.Value("s3Client").(*minio.Client)
if !ok {
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
}
for _, v := range k {
id := v.Raw().(types.ID)
results = append(results, &dataloader.Result{
Data: listObjects(s3Client, id, true),
Error: nil,
})
}
return results
}
// listObjects helper func for listObjectsBatch
func listObjects(s3Client *minio.Client, id types.ID, recursive bool) []minio.ObjectInfo {
log.Debug("S3 call 'ListObjects': ", id)
objectCh := s3Client.ListObjects(context.Background(), id.Bucket, minio.ListObjectsOptions{
Prefix: id.Key,
Recursive: recursive,
})
result := make([]minio.ObjectInfo, 0)
for obj := range objectCh {
result = append(result, obj)
}
return result
}
func listBucketsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
log.Debug("listBucketsBatch")
var results []*dataloader.Result
s3Client, ok := c.Value("s3Client").(*minio.Client)
if !ok {
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
}
buckets, err := s3Client.ListBuckets(c)
if err != nil {
return handleLoaderError(k, err)
}
result := &dataloader.Result{
Data: buckets,
Error: nil,
}
for range k {
results = append(results, result)
}
return results
}
func statObjectBatch(ctx context.Context, k dataloader.Keys) []*dataloader.Result {
log.Debug("statObjectBatch")
var results []*dataloader.Result
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
if !ok {
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
}
for _, v := range k {
id := v.Raw().(types.ID)
stat, err := s3Client.StatObject(ctx, id.Bucket, id.Key, minio.GetObjectOptions{})
results = append(results, &dataloader.Result{
Data: stat,
Error: err,
})
}
return results
}
// handleLoaderError helper func when the whole batch failed
func handleLoaderError(k dataloader.Keys, err error) []*dataloader.Result {
log.Error(err.Error())
var results []*dataloader.Result
for range k {
results = append(results, &dataloader.Result{
Data: nil,
Error: err,
})
}
return results
}