135 lines
3.1 KiB
Go
135 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 {
|
|
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 {
|
|
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 '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 {
|
|
var results []*dataloader.Result
|
|
|
|
s3Client, ok := c.Value("s3Client").(*minio.Client)
|
|
|
|
if !ok {
|
|
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
|
|
}
|
|
|
|
log.Debug("S3 'ListBuckets'")
|
|
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)
|
|
log.Debug("S3 'StatObject': ", 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
|
|
}
|