{"record":{"id":"d33101d3785049e1","repo":"Tencent/WeKnora","slug":"failed-to-delete-file-from-oss-w","errorCode":null,"errorMessage":"failed to delete file from OSS: %w","messagePattern":"failed to delete file from OSS: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/service/file/oss.go","lineNumber":332,"sourceCode":"\t\treturn err\n\t}\n\tif err := utils.SafeObjectKey(objectName); err != nil {\n\t\treturn fmt.Errorf(\"invalid file path: %w\", err)\n\t}\n\n\tvar client *oss.Client\n\tif bucketName == s.tempBucketName && s.tempClient != nil {\n\t\tclient = s.tempClient\n\t} else {\n\t\tclient = s.client\n\t}\n\n\t_, err = client.DeleteObject(ctx, &oss.DeleteObjectRequest{\n\t\tBucket: oss.Ptr(bucketName),\n\t\tKey:    oss.Ptr(objectName),\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to delete file from OSS: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// GetFileURL returns a presigned download URL for the file.\nfunc (s *ossFileService) GetFileURL(ctx context.Context, filePath string) (string, error) {\n\tbucketName, objectName, err := parseOssFilePath(filePath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif err := utils.SafeObjectKey(objectName); err != nil {\n\t\treturn \"\", fmt.Errorf(\"invalid file path: %w\", err)\n\t}\n\n\t// Determine which client to use\n\tvar client *oss.Client\n\tif bucketName == s.tempBucketName && s.tempClient != nil {","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/service/file/oss.go#L314-L350","documentation":"This error wraps the underlying error returned by the Alibaba Cloud OSS SDK's DeleteObject call inside ossFileService.DeleteFile. It means the delete request failed at the OSS level (network, permissions, missing object, or invalid request). The original SDK error is preserved via %w so callers can errors.As/Is on it.","triggerScenarios":"Calling DeleteFile with a bucket/object the credentials lack oss:DeleteObject permission for, an objectName that does not exist (or wrong bucket), or network/region/endpoint misconfiguration reaching client.DeleteObject.","commonSituations":"Rotated or insufficient RAM/STS credentials, temp-bucket vs main-bucket client mixups, objects already deleted by lifecycle rules or concurrent deletes, private-link/VPC endpoint misconfiguration.","solutions":["Log the wrapped error with errors.Unwrap to see the OSS error code (NoSuchKey, AccessDenied, etc.) and fix the root cause","Verify the OSS credentials/policy grant DeleteObject on the target bucket","Check bucketName/objectName resolution (parseOssFilePath) and that the correct client (temp vs main) is used","Treat deletion of a missing object as idempotent success in the caller if that is acceptable"],"exampleFix":"// before\n_, err = client.DeleteObject(ctx, &oss.DeleteObjectRequest{Bucket: oss.Ptr(bucketName), Key: oss.Ptr(objectName)})\nif err != nil { return fmt.Errorf(\"failed to delete file from OSS: %w\", err) }\n// after\n_, err = client.DeleteObject(ctx, &oss.DeleteObjectRequest{Bucket: oss.Ptr(bucketName), Key: oss.Ptr(objectName)})\nif err != nil {\n    var apiErr smithy.APIError\n    if errors.As(err, &apiErr) && apiErr.ErrorCode() == \"NoSuchKey\" { return nil }\n    return fmt.Errorf(\"failed to delete file from OSS: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"var exists bool\n_, err := svc.GetFileURL(ctx, path)\nexists = err == nil // optionally pre-check object exists before delete","typeGuard":"func isNoSuchKey(err error) bool {\n    var apiErr interface{ ErrorCode() string }\n    return errors.As(err, &apiErr) && apiErr.ErrorCode() == \"NoSuchKey\"\n}","tryCatchPattern":"err := svc.DeleteFile(ctx, path)\nif err != nil {\n    var wrapped *fmt.WrapError\n    if isNoSuchKey(err) { /* treat as deleted */ } else { return fmt.Errorf(\"delete failed: %w\", err) }\n}","preventionTips":["Grant DeleteObject permission to the configured credentials","Treat NoSuchKey as idempotent success in deletion flows","Use stable file references produced by Save*, not user-typed paths","Monitor OSS error codes in logs for permission drift"],"tags":["oss","storage","delete","wrap"],"backgroundTag":"oss-delete-object-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}