flipped-aurora/gin-vue-admin · error

function formUploader.Put() failed, err:

Error message

function formUploader.Put() failed, err:

What it means

The Qiniu formUploader.Put call failed while uploading the opened file to the Qiniu Kodo bucket with the upload token. This wraps the qiniu-sdk-go error — which may be token invalid/expired, bucket mismatch, network failure, or a Qiniu API rejection. The returned message embeds putErr.Error() after the prefix.

Source

Thrown at server/utils/upload/qiniu.go:49

	mac := qbox.NewMac(global.GVA_CONFIG.Qiniu.AccessKey, global.GVA_CONFIG.Qiniu.SecretKey)
	upToken := putPolicy.UploadToken(mac)
	cfg := qiniuConfig()
	formUploader := storage.NewFormUploader(cfg)
	ret := storage.PutRet{}
	putExtra := storage.PutExtra{Params: map[string]string{"x:name": "github logo"}}

	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) // 文件名格式 自己可以改 建议保证唯一性
	putErr := formUploader.Put(context.Background(), &ret, upToken, fileKey, f, file.Size, &putExtra)
	if putErr != nil {
		logger.WithCtx(ctx).Mod("upload").Err(putErr).Error("function formUploader.Put() failed")
		return "", "", errors.New("function formUploader.Put() failed, err:" + putErr.Error())
	}
	return global.GVA_CONFIG.Qiniu.ImgPath + "/" + ret.Key, ret.Key, nil
}

//@author: [piexlmax](https://github.com/piexlmax)
//@author: [ccfish86](https://github.com/ccfish86)
//@author: [SliverHorn](https://github.com/SliverHorn)
//@object: *Qiniu
//@function: DeleteFile
//@description: 删除文件
//@param: key string
//@return: error

func (*Qiniu) DeleteFile(ctx context.Context, key string) error {
	bucketManager := newBucketManager()
	if err := bucketManager.Delete(global.GVA_CONFIG.Qiniu.Bucket, key); err != nil {
		logger.WithCtx(ctx).Mod("upload").Err(err).Error("function bucketManager.Delete() failed")
		return errors.New("function bucketManager.Delete() failed, err:" + err.Error())

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Check the embedded putErr text — Qiniu errors carry codes like 'bad token' or 'no such bucket' that pinpoint the cause.
  2. Verify qiniu access-key, secret-key, bucket, and zone settings in config.yaml against the Qiniu console.
  3. Regenerate keys if rotated; ensure server time is NTP-synced for token validity.
  4. Confirm egress network access from the server to Qiniu upload endpoints.
  5. Ensure the bucket name in Put matches the token's putPolicy scope exactly.

Example fix

// before
putErr := formUploader.Put(context.Background(), &ret, upToken, fileKey, f, file.Size, &putExtra)
// after — make the zone explicit so Put hits the right region
zone := &storage.ZoneHuadong
cfg := storage.Config{Zone: zone, UseHTTPS: false, UseCdnDomains: false}
formUploader := storage.NewFormUploader(&cfg)
Defensive patterns

Strategy: try-catch

Validate before calling

if global.GVA_CONFIG.Qiniu.AccessKey == "" || global.GVA_CONFIG.Qiniu.SecretKey == "" || global.GVA_CONFIG.Qiniu.Bucket == "" {
    return errors.New("qiniu config incomplete")
}

Try / catch

path, key, err := q.UploadFile(ctx, fileHeader)
if err != nil {
    if strings.Contains(err.Error(), "bad token") || strings.Contains(err.Error(), "no such bucket") {
        logger.Error("qiniu misconfigured: %v", err) // config problem, alert ops
    } else {
        logger.Error("qiniu transient failure: %v", err) // candidate for retry
    }
    c.JSON(500, gin.H{"code": 7, "msg": "上传失败,请稍后重试"})
}

Prevention

When it happens

Trigger: Calling Qiniu.UploadFile when formUploader.Put returns an error: invalid or expired upload token (bad AccessKey/SecretKey), bucket name mismatch, key policy violation, network/DNS failure to Qiniu upload hosts, or file size/policy limits exceeded.

Common situations: Wrong qiniu access-key/secret-key in config.yaml, bucket renamed or deleted, server clock skew invalidating tokens, firewall egress blocking Qiniu upload endpoints, putting to a bucket in a different zone than the configured Zone.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/87fb7468d4dc6417. Report an issue: GitHub.