flipped-aurora/gin-vue-admin · error
function file.Open() failed, err:
Error message
function file.Open() failed, err:
What it means
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.
Source
Thrown at server/utils/upload/tencent_cos.go:26
"net/http"
"net/url"
"time"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/utils/logger"
"github.com/tencentyun/cos-go-sdk-v5"
)
type TencentCOS struct{}
// UploadFile upload file to COS
func (*TencentCOS) UploadFile(ctx context.Context, file *multipart.FileHeader) (string, string, error) {
client := NewClient()
f, openError := file.Open()
if openError != nil {
logger.WithCtx(ctx).Mod("upload").Err(openError).Error("function file.Open() failed")
return "", "", errors.New("function file.Open() failed, err:" + openError.Error())
}
defer f.Close() // 创建文件 defer 关闭
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename)
_, err := client.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+"/"+fileKey, f, nil)
if err != nil {
logger.WithCtx(ctx).Mod("upload").Err(err).Error("function client.Object.Put() failed")
return "", "", errors.New("function client.Object.Put() failed, err:" + err.Error())
}
return global.GVA_CONFIG.TencentCOS.BaseURL + "/" + global.GVA_CONFIG.TencentCOS.PathPrefix + "/" + fileKey, fileKey, nil
}
// DeleteFile delete file form COS
func (*TencentCOS) DeleteFile(ctx context.Context, key string) error {
client := NewClient()
name := global.GVA_CONFIG.TencentCOS.PathPrefix + "/" + key
_, err := client.Object.Delete(context.Background(), name)
if err != nil {View on GitHub (pinned to 3136500ef3)
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)
Example fix
// before
fh, _ := c.FormFile("file")
filePath, _ := saveLocal(fh) // consumes temp file
return cos.UploadFile(ctx, fh) // Open() fails
// after
fh, _ := c.FormFile("file")
url, key, err := cos.UploadFile(ctx, fh) // upload first
if err == nil {
saveLocal(fh)
} Defensive patterns
Strategy: validation
Validate before calling
if file == nil || file.Size == 0 {
return errors.New("invalid multipart file")
} Try / catch
url, key, err := cos.UploadFile(ctx, fh)
if err != nil && strings.Contains(err.Error(), "file.Open() failed") {
// multipart temp file gone: re-parse form or reject the request
return fmt.Errorf("upload source unavailable: %w", err)
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- 构造 FileHeader 失败
- function file.Open() failed, err:
- function file.Open() failed, err:
- function file.Open() failed, err:
- function client.Object.Put() failed, err:
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/f2463dffdaf5904e.
Report an issue: GitHub.