check with HEAD before upload

This commit is contained in:
Dong Chen
2026-02-27 14:29:17 -08:00
parent 17dc6918cf
commit 241ac6e003

View File

@@ -150,14 +150,19 @@ func (u *uploader) uploadOnce(ctx context.Context, blob Blob) (int64, error) {
u.logger.Debug("uploading blob", "digest", blob.Digest, "size", blob.Size)
}
// Check if blob already exists (may have been uploaded by another
// concurrent process since the initial HEAD check)
if exists, err := u.exists(ctx, blob); err != nil {
return 0, err
} else if exists {
return 0, nil
}
// Init upload
uploadURL, err := u.initUpload(ctx, blob)
if err != nil {
return 0, err
}
if uploadURL == "" {
return 0, nil // blob already exists
}
// Open file
f, err := os.Open(filepath.Join(u.srcDir, digestToPath(blob.Digest)))
@@ -220,10 +225,6 @@ func (u *uploader) initUpload(ctx context.Context, blob Blob) (string, error) {
return u.initUpload(ctx, blob)
}
if resp.StatusCode == http.StatusCreated {
return "", nil // blob already exists
}
if resp.StatusCode != http.StatusAccepted {
return "", fmt.Errorf("init: status %d", resp.StatusCode)
}