{"record":{"id":"f2463dffdaf5904e","repo":"flipped-aurora/gin-vue-admin","slug":"function-file-open-failed-err-f2463d","errorCode":null,"errorMessage":"function file.Open() failed, err:","messagePattern":"function file\\.Open\\(\\) failed, err:","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/upload/tencent_cos.go","lineNumber":26,"sourceCode":"\t\"net/http\"\n\t\"net/url\"\n\t\"time\"\n\n\t\"github.com/flipped-aurora/gin-vue-admin/server/global\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/utils/logger\"\n\n\t\"github.com/tencentyun/cos-go-sdk-v5\"\n)\n\ntype TencentCOS struct{}\n\n// UploadFile upload file to COS\nfunc (*TencentCOS) UploadFile(ctx context.Context, file *multipart.FileHeader) (string, string, error) {\n\tclient := NewClient()\n\tf, openError := file.Open()\n\tif openError != nil {\n\t\tlogger.WithCtx(ctx).Mod(\"upload\").Err(openError).Error(\"function file.Open() failed\")\n\t\treturn \"\", \"\", errors.New(\"function file.Open() failed, err:\" + openError.Error())\n\t}\n\tdefer f.Close() // 创建文件 defer 关闭\n\tfileKey := fmt.Sprintf(\"%d%s\", time.Now().Unix(), file.Filename)\n\n\t_, err := client.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+\"/\"+fileKey, f, nil)\n\tif err != nil {\n\t\tlogger.WithCtx(ctx).Mod(\"upload\").Err(err).Error(\"function client.Object.Put() failed\")\n\t\treturn \"\", \"\", errors.New(\"function client.Object.Put() failed, err:\" + err.Error())\n\t}\n\treturn global.GVA_CONFIG.TencentCOS.BaseURL + \"/\" + global.GVA_CONFIG.TencentCOS.PathPrefix + \"/\" + fileKey, fileKey, nil\n}\n\n// DeleteFile delete file form COS\nfunc (*TencentCOS) DeleteFile(ctx context.Context, key string) error {\n\tclient := NewClient()\n\tname := global.GVA_CONFIG.TencentCOS.PathPrefix + \"/\" + key\n\t_, err := client.Object.Delete(context.Background(), name)\n\tif err != nil {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/upload/tencent_cos.go#L8-L44","documentation":"Wraps the error from multipart.FileHeader.Open() in TencentCOS.UploadFile(). Open() opens the uploaded multipart file for reading; failure means the in-memory/temp file backing the upload cannot be opened.","triggerScenarios":"Calling UploadFile(ctx, file) when file.Open() fails — usually the multipart temp file was moved/cleaned before upload, or the request memory limit discarded the file.","commonSituations":"Handler consumed/saved the multipart form before the upload service ran (temp file already consumed), server temp dir permissions changed, or the *multipart.FileHeader was constructed manually and invalid.","solutions":["Ensure UploadFile is called before anything else consumes the multipart form","Pass the original *multipart.FileHeader from c.File/FormFile directly","Check OS temp directory (TMPDIR) exists and is writable by the server process","Inspect the wrapped openError suffix for the underlying OS error (e.g. no such file, permission denied)"],"exampleFix":"// before\nfh, _ := c.FormFile(\"file\")\nfilePath, _ := saveLocal(fh) // consumes temp file\nreturn cos.UploadFile(ctx, fh) // Open() fails\n// after\nfh, _ := c.FormFile(\"file\")\nurl, key, err := cos.UploadFile(ctx, fh) // upload first\nif err == nil {\n    saveLocal(fh)\n}","handlingStrategy":"validation","validationCode":"if file == nil || file.Size == 0 {\n    return errors.New(\"invalid multipart file\")\n}","typeGuard":null,"tryCatchPattern":"url, key, err := cos.UploadFile(ctx, fh)\nif err != nil && strings.Contains(err.Error(), \"file.Open() failed\") {\n    // multipart temp file gone: re-parse form or reject the request\n    return fmt.Errorf(\"upload source unavailable: %w\", err)\n}","preventionTips":["Upload before any other consumer touches the multipart form","Pass the original *multipart.FileHeader from the Gin context","Ensure the server temp dir is writable and not cleaned mid-request"],"tags":["tencent-cos","upload","multipart","file-io"],"backgroundTag":"multipart-file-open-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}