Skip to content

Commit

Permalink
storage: handle errors in batch objects delete action
Browse files Browse the repository at this point in the history
  • Loading branch information
kobajagi committed Aug 5, 2024
1 parent 3852047 commit b2ff514
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/storage_delete.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"errors"
"fmt"
"os"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -86,6 +88,18 @@ argument with "/":

deleted, err := storage.DeleteObjects(gContext, bucket, prefix, recursive)
if err != nil {
e := sos.NewBatchErrorList()
if errors.As(err, e) {
if verbose {
for _, o := range deleted {
fmt.Println(aws.ToString(o.Key))
}
}

for _, e := range e.List {
fmt.Fprintln(os.Stderr, e)
}
}
return fmt.Errorf("unable to delete objects: %w", err)
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/storage/sos/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ import (
"github.com/exoscale/cli/utils"
)

// BatchErrorList contains list of errors parsed from AWS SDK types.DeletedObject Error attribute.
// Only Message attribute of each types.Error object is taken and converted to std Error.
type BatchErrorList struct {
List []error
}

func NewBatchErrorList() *BatchErrorList {
return &BatchErrorList{
[]error{},
}
}

func (l BatchErrorList) Error() string {
return fmt.Sprintf("batch job returned %d errors", len(l.List))
}

func (l *BatchErrorList) AddErrors(errs []types.Error) {
for _, err := range errs {
l.List = append(l.List, fmt.Errorf("%s", err.Message))

Check failure on line 52 in pkg/storage/sos/object.go

View workflow job for this annotation

GitHub Actions / build

fmt.Errorf format %s has arg err.Message of wrong type *string
}
}

func (c *Client) DeleteObjects(ctx context.Context, bucket, prefix string, recursive bool) ([]types.DeletedObject, error) {
deleteList := make([]types.ObjectIdentifier, 0)
err := c.ForEachObject(ctx, bucket, prefix, recursive, func(o *types.Object) error {
Expand All @@ -45,6 +67,7 @@ func (c *Client) DeleteObjects(ctx context.Context, bucket, prefix string, recur
// precaution we're batching deletes.
maxKeys := 1000
deleted := make([]types.DeletedObject, 0)
errs := NewBatchErrorList()

for i := 0; i < len(deleteList); i += maxKeys {
j := i + maxKeys
Expand All @@ -61,6 +84,11 @@ func (c *Client) DeleteObjects(ctx context.Context, bucket, prefix string, recur
}

deleted = append(deleted, res.Deleted...)
errs.AddErrors(res.Errors)
}

if len(errs.List) > 0 {
return deleted, errs
}

return deleted, nil
Expand Down

0 comments on commit b2ff514

Please sign in to comment.