2021-10-14 17:00:11 +00:00
|
|
|
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 {
|
2021-11-04 18:41:50 +00:00
|
|
|
log.Debug("S3 'ListObjects': ", id)
|
2021-10-14 17:00:11 +00:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:41:50 +00:00
|
|
|
log.Debug("S3 'ListBuckets'")
|
2021-10-14 17:00:11 +00:00
|
|
|
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)
|
2021-11-04 18:41:50 +00:00
|
|
|
log.Debug("S3 'StatObject': ", id)
|
2021-10-14 17:00:11 +00:00
|
|
|
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
|
|
|
|
}
|