flipped-aurora/gin-vue-admin · error

function client.Object.Put() failed, err:

Error message

function client.Object.Put() failed, err:

What it means

Wraps an error from the COS SDK's client.Object.Put() in TencentCOS.UploadFile(). Put() uploads the file stream to the bucket; any transport or COS-side rejection surfaces here.

Source

Thrown at server/utils/upload/tencent_cos.go:34

)

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 {
		logger.WithCtx(ctx).Mod("upload").Err(err).Error("function bucketManager.Delete() failed")
		return errors.New("function bucketManager.Delete() failed, err:" + err.Error())
	}
	return nil
}

// Exists 通过 Object.Head 检查对象是否存在;404 统一降级为 (false, nil)。
func (*TencentCOS) Exists(ctx context.Context, key string) (bool, error) {

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Check the wrapped err suffix for the COS error code (e.g. AccessDenied, SignatureDoesNotMatch)
  2. Verify TencentCOS config keys in config.yaml: Bucket, Region, SecretID, SecretKey, BaseURL, PathPrefix
  3. Confirm outbound network access to the COS regional endpoint
  4. Retry transient 5xx/timeout errors; check COS console for bucket ACL/cors restrictions

Example fix

// before
// config.yaml: tencent-cos: { bucket: "", region: "" }
url, key, err := cos.UploadFile(ctx, fh) // Object.Put fails with signature error
// after
// config.yaml: tencent-cos: { bucket: "my-bucket-1250000000", region: "ap-guangzhou", secret-id: "...", secret-key: "..." }
Defensive patterns

Strategy: retry

Validate before calling

cfg := global.GVA_CONFIG.TencentCOS
if cfg.Bucket == "" || cfg.Region == "" || cfg.SecretID == "" || cfg.SecretKey == "" {
    return errors.New("tencent cos config incomplete")
}

Try / catch

url, key, err := cos.UploadFile(ctx, fh)
if err != nil {
    if isTransientCOS(err) { // 5xx/timeout
        // retry with backoff
    }
    return fmt.Errorf("cos upload failed: %w", err)
}

Prevention

When it happens

Trigger: Calling UploadFile(ctx, file) when Object.Put fails: wrong SecretID/SecretKey, missing bucket or region config, path prefix issues, or network failure during transfer.

Common situations: Misconfigured GVA_CONFIG.TencentCOS (Bucket/Region/SecretID/SecretKey/PathPrefix), expired credentials, request body larger than limits, VPC without COS endpoint access.

Related errors


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