cloudreve/cloudreve · error

failed to get uploaded file size: %w

Error message

failed to get uploaded file size: %w

What it means

During CompleteUpload, the HEAD request used to verify the uploaded object's size failed outright (distinct from the size-mismatch error that follows a successful HEAD). The wrapped cause is usually a 404 (object not present at session SavePath), 403 (no HeadObject right), or a network failure at completion time.

Source

Thrown at pkg/filemanager/driver/cos/cos.go:558

		ChunkSize:   handler.chunkSize,
	}, nil
}

// 取消上传凭证
func (handler *Driver) CancelToken(ctx context.Context, uploadSession *fs.UploadSession) error {
	_, err := handler.client.Object.AbortMultipartUpload(ctx, uploadSession.Props.SavePath, uploadSession.UploadID)
	return err
}

func (handler *Driver) CompleteUpload(ctx context.Context, session *fs.UploadSession) error {
	if session.SentinelTaskID == 0 {
		return nil
	}

	// Make sure uploaded file size is correct
	res, err := handler.client.Object.Head(ctx, session.Props.SavePath, &cossdk.ObjectHeadOptions{})
	if err != nil {
		return fmt.Errorf("failed to get uploaded file size: %w", err)
	}

	if res.ContentLength != session.Props.Size {
		return serializer.NewError(
			serializer.CodeMetaMismatch,
			fmt.Sprintf("File size not match, expected: %d, actual: %d", session.Props.Size, res.ContentLength),
			nil,
		)
	}
	return nil
}

func (handler *Driver) Capabilities() *driver.Capabilities {
	mediaMetaExts := handler.policy.Settings.MediaMetaExts
	if !handler.policy.Settings.NativeMediaProcessing {
		mediaMetaExts = nil
	}
	return &driver.Capabilities{

View on GitHub (pinned to 20c95ad73f)

Solutions

  1. Confirm the key the client actually uploaded to matches session.Props.SavePath byte-for-byte (leading slashes, URL encoding)
  2. Unwrap the error to distinguish 404 (wrong key/absent), 403 (permissions), and transport failures
  3. Ensure the CAM policy allows GetObject (HEAD) on the bucket
  4. Retry CompleteUpload after a short delay; HEAD is idempotent and transient 404s from eventual consistency resolve
Defensive patterns

Strategy: retry

Try / catch

err := driver.CompleteUpload(ctx, session)
if err != nil && strings.Contains(err.Error(), "failed to get uploaded file size") {
    // HEAD at completion can fail transiently; CompleteUpload is idempotent for this driver
    time.Sleep(2 * time.Second)
    err = driver.CompleteUpload(ctx, session)
}

Prevention

When it happens

Trigger: Client uploaded parts to a different key/bucket than session.Props.SavePath; the object was deleted between upload finish and completion; credentials lacking cos:GetObject; a proxy stripping HEAD requests; transient cross-region consistency delay.

Common situations: Browser/CORS direct-upload flows where the frontend SDK normalizes the key differently (leading slash, encoding) than the server's SavePath; reverse proxies blocking HEAD; policies switched right before completion.

Related errors


AI-assisted analysis of cloudreve/cloudreve@20c95ad73f (2026-08-16). Data as JSON: /api/errors/e0f40e7503aac1cc. Report an issue: GitHub.