Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(services/s3): Support absolute path and idempotent delete #937

Merged
merged 4 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions services/s3/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,16 @@ func (s *Storage) delete(ctx context.Context, path string, opt pairStorageDelete
if opt.HasMultipartID {
abortInput := s.formatAbortMultipartUploadInput(path, opt)

// S3 AbortMultipartUpload is idempotent, so we don't need to check NoSuchUpload error.
//
// References
// - [GSP-46](https://github.com/beyondstorage/specs/blob/master/rfcs/46-idempotent-delete.md)
// - https://docs.aws.amazon.com/AmazonS3/latest/API/API_AbortMultipartUpload.html
_, err = s.service.AbortMultipartUpload(ctx, abortInput)
if err != nil && checkError(err, responseCodeNoSuchUpload) {
// AbortMultipartUpload is idempotent in s3, but non-idempotent in minio, we need to omit `NoSuchUpload` error for compatibility.
// ref: [GSP-46](https://github.com/beyondstorage/specs/blob/master/rfcs/46-idempotent-delete.md)
err = nil
}
if err != nil {
return
return err
}
return nil
}

input, err := s.formatDeleteObjectInput(path, opt)
Expand Down
28 changes: 26 additions & 2 deletions services/s3/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ const (
StorageClassDeepArchive = s3types.ObjectStorageClassDeepArchive
)

// s3 service response error code
//
// ref: https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html#RESTErrorResponses
const (
// AccessDenied access denied
responseAccessDenied = "AccessDenied"
// NoSuchKey the specified key does not exist.
responseCodeNoSuchKey = "NoSuchKey"
// NoSuchUpload the specified multipart upload dose not exist.
responseCodeNoSuchUpload = "NoSuchUpload"
)

func formatError(err error) error {
if _, ok := err.(services.InternalError); ok {
return err
Expand All @@ -226,15 +238,24 @@ func formatError(err error) error {

switch e.Code {
// AWS SDK will use status code to generate awserr.Error, so "NotFound" should also be supported.
case "NoSuchKey", "NotFound":
case responseCodeNoSuchKey, "NotFound":
return fmt.Errorf("%w: %v", services.ErrObjectNotExist, err)
case "AccessDenied":
case responseAccessDenied:
return fmt.Errorf("%w: %v", services.ErrPermissionDenied, err)
default:
return fmt.Errorf("%w: %v", services.ErrUnexpected, err)
}
}

func checkError(err error, code string) bool {
e := &smithy.OperationError{}
if ok := errors.As(err, &e); !ok {
return false
}

return strings.Contains(e.Error(), code)
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
}

// newStorage will create a new client.
func (s *Service) newStorage(pairs ...typ.Pair) (st *Storage, err error) {
opt, err := parsePairStorageNew(pairs)
Expand Down Expand Up @@ -281,6 +302,9 @@ func (s *Service) formatError(op string, err error, name string) error {

// getAbsPath will calculate object storage's abs path
func (s *Storage) getAbsPath(path string) string {
if strings.HasPrefix(path, s.workDir) {
JinnyYi marked this conversation as resolved.
Show resolved Hide resolved
return strings.TrimPrefix(path, "/")
}
prefix := strings.TrimPrefix(s.workDir, "/")
return prefix + path
}
Expand Down