From 599db20a2068a86584ab1ddca1122286f460272e Mon Sep 17 00:00:00 2001 From: fanlv Date: Wed, 10 Sep 2025 10:50:52 +0800 Subject: [PATCH] feat(infra): HeadObject return object not found error if object not exist --- backend/infra/contract/storage/storage.go | 5 +++++ backend/infra/impl/storage/minio/minio.go | 2 +- backend/infra/impl/storage/s3/s3.go | 2 +- backend/infra/impl/storage/tos/tos.go | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/backend/infra/contract/storage/storage.go b/backend/infra/contract/storage/storage.go index eb62c3660..315cea40d 100644 --- a/backend/infra/contract/storage/storage.go +++ b/backend/infra/contract/storage/storage.go @@ -18,10 +18,15 @@ package storage import ( "context" + "errors" "io" "time" ) +var ( + ErrObjectNotFound = errors.New("object not found") +) + //go:generate mockgen -destination ../../../internal/mock/infra/contract/storage/storage_mock.go -package mock -source storage.go Factory type Storage interface { // PutObject puts the object with the specified key. diff --git a/backend/infra/impl/storage/minio/minio.go b/backend/infra/impl/storage/minio/minio.go index 60882d4a1..6fd9c57fe 100644 --- a/backend/infra/impl/storage/minio/minio.go +++ b/backend/infra/impl/storage/minio/minio.go @@ -310,7 +310,7 @@ func (m *minioClient) HeadObject(ctx context.Context, objectKey string, opts ... stat, err := m.client.StatObject(ctx, m.bucketName, objectKey, minio.StatObjectOptions{}) if err != nil { if minio.ToErrorResponse(err).Code == "NoSuchKey" { - return nil, nil + return nil, storage.ErrObjectNotFound } return nil, fmt.Errorf("HeadObject failed for key %s: %w", objectKey, err) diff --git a/backend/infra/impl/storage/s3/s3.go b/backend/infra/impl/storage/s3/s3.go index 98bdb640c..ea8b6fd8e 100644 --- a/backend/infra/impl/storage/s3/s3.go +++ b/backend/infra/impl/storage/s3/s3.go @@ -375,7 +375,7 @@ func (t *s3Client) HeadObject(ctx context.Context, objectKey string, opts ...sto if err != nil { var nsk *types.NotFound if errors.As(err, &nsk) { - return nil, nil + return nil, storage.ErrObjectNotFound } return nil, err } diff --git a/backend/infra/impl/storage/tos/tos.go b/backend/infra/impl/storage/tos/tos.go index adc4c727e..0ca9d18cd 100644 --- a/backend/infra/impl/storage/tos/tos.go +++ b/backend/infra/impl/storage/tos/tos.go @@ -383,7 +383,7 @@ func (t *tosClient) HeadObject(ctx context.Context, objectKey string, opts ...st if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { if serverErr.StatusCode == http.StatusNotFound { - return nil, nil + return nil, storage.ErrObjectNotFound } } return nil, err