use s3Service interface

This commit is contained in:
2021-11-23 20:12:24 +01:00
parent 60817c2249
commit 8d85d645d6
9 changed files with 162 additions and 257 deletions

View File

@@ -5,17 +5,16 @@ import (
"fmt"
"strings"
"github.com/minio/minio-go/v7"
"git.kapelle.org/niklas/s3browser/internal/db"
helper "git.kapelle.org/niklas/s3browser/internal/helper"
"git.kapelle.org/niklas/s3browser/internal/loader"
"git.kapelle.org/niklas/s3browser/internal/s3"
types "git.kapelle.org/niklas/s3browser/internal/types"
log "github.com/sirupsen/logrus"
)
func deleteMutation(ctx context.Context, id types.ID) error {
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
s3Client, ok := ctx.Value("s3Client").(s3.S3Service)
if !ok {
return fmt.Errorf("Failed to get s3Client from context")
@@ -24,7 +23,7 @@ func deleteMutation(ctx context.Context, id types.ID) error {
log.Debug("S3 'RemoveObject': ", id)
// TODO: it is posible to remove multiple objects with a single call.
// Is it better to batch this?
err := s3Client.RemoveObject(ctx, id.Bucket, id.Key, minio.RemoveObjectOptions{})
err := s3Client.RemoveObject(ctx, id)
if err != nil {
return err
@@ -36,7 +35,7 @@ func deleteMutation(ctx context.Context, id types.ID) error {
}
func copyMutation(ctx context.Context, src, dest types.ID) (*types.File, error) {
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
s3Client, ok := ctx.Value("s3Client").(s3.S3Service)
if !ok {
return nil, fmt.Errorf("Failed to get s3Client from context")
@@ -50,35 +49,22 @@ func copyMutation(ctx context.Context, src, dest types.ID) (*types.File, error)
}
log.Debug("S3 'CopyObject': ", src, "-->", dest)
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
Bucket: dest.Bucket,
Object: dest.Key,
}, minio.CopySrcOptions{
Bucket: src.Bucket,
Object: src.Key,
})
err := s3Client.CopyObject(ctx, src, dest)
if err != nil {
return nil, err
}
newID := types.ID{
Bucket: info.Bucket,
Key: info.Key,
}
newID.Normalize()
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, newID)
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, dest)
return &types.File{
ID: newID,
ID: dest,
}, nil
}
func moveDirMutation(ctx context.Context, src, dest types.ID) ([]*types.File, error) {
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
s3Client, ok := ctx.Value("s3Client").(s3.S3Service)
if !ok {
return nil, fmt.Errorf("Failed to get s3Client from context")
@@ -107,13 +93,7 @@ func moveDirMutation(ctx context.Context, src, dest types.ID) ([]*types.File, er
newID.Normalize()
log.Debug("S3 'CopyObject': ", src, "-->", dest)
_, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
Bucket: dest.Bucket,
Object: newID.Key,
}, minio.CopySrcOptions{
Bucket: file.ID.Bucket,
Object: file.ID.Key,
})
err := s3Client.CopyObject(ctx, file.ID, dest)
if err != nil {
// TODO: handle error
@@ -135,7 +115,7 @@ func moveDirMutation(ctx context.Context, src, dest types.ID) ([]*types.File, er
}
func moveFileMutation(ctx context.Context, src, dest types.ID) (*types.File, error) {
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
s3Client, ok := ctx.Value("s3Client").(s3.S3Service)
if !ok {
return nil, fmt.Errorf("Failed to get s3Client from context")
@@ -150,13 +130,7 @@ func moveFileMutation(ctx context.Context, src, dest types.ID) (*types.File, err
log.Debug("S3 'CopyObject': ", src, "-->", dest)
// There is no (spoon) move. Only copy and delete
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
Bucket: dest.Bucket,
Object: dest.Key,
}, minio.CopySrcOptions{
Bucket: src.Bucket,
Object: src.Key,
})
err := s3Client.CopyObject(ctx, src, dest)
if err != nil {
return nil, err
@@ -168,54 +142,37 @@ func moveFileMutation(ctx context.Context, src, dest types.ID) (*types.File, err
return nil, err
}
newId := types.ID{
Bucket: info.Bucket,
Key: info.Key,
}
newId.Normalize()
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, newId)
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, dest)
return &types.File{
ID: newId,
ID: dest,
}, nil
}
func createDirectory(ctx context.Context, id types.ID) (*types.Directory, error) {
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
s3Client, ok := ctx.Value("s3Client").(s3.S3Service)
if !ok {
return nil, fmt.Errorf("Failed to get s3Client from context")
}
log.Debug("S3 'PutObject': ", id)
info, err := s3Client.PutObject(ctx, id.Bucket, id.Key, strings.NewReader(""), 0, minio.PutObjectOptions{
ContentType: "application/x-directory",
})
err := s3Client.PutObject(ctx, id, strings.NewReader(""), 0) // TODO: s3client interface needs content type parameter
if err != nil {
return nil, err
}
newID := types.ID{
Bucket: info.Bucket,
Key: info.Key,
}
newID.Normalize()
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, newID)
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, id)
return &types.Directory{
ID: newID,
ID: id,
}, nil
}
func deleteDirectory(ctx context.Context, id types.ID) error {
s3Client, ok := ctx.Value("s3Client").(*minio.Client)
s3Client, ok := ctx.Value("s3Client").(s3.S3Service)
if !ok {
return fmt.Errorf("Failed to get s3Client from context")
@@ -236,11 +193,9 @@ func deleteDirectory(ctx context.Context, id types.ID) error {
}
// Delete all child files
var keysToDel []string
for _, file := range files {
keysToDel = append(keysToDel, file.ID.Key)
s3Client.RemoveObject(ctx, file.ID)
}
err = helper.DeleteMultiple(ctx, *s3Client, id.Bucket, keysToDel)
if err != nil {
return err
@@ -253,7 +208,7 @@ func deleteDirectory(ctx context.Context, id types.ID) error {
// TODO: check if this is normal behavior when working with s3
if len(files) == 0 {
log.Debug("S3 'RemoveObject': ", id)
err := s3Client.RemoveObject(ctx, id.Bucket, id.Key, minio.RemoveObjectOptions{})
err := s3Client.RemoveObject(ctx, id)
if err != nil {
return err
}