Skip to content

Commit e69ce85

Browse files
authored
Merge pull request #414 from qiniu/fix/resume-upload-fail-fast
fix resume upload can not fail fast when init upload id failed
2 parents 6a6da4c + da3ab8a commit e69ce85

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 7.10.0 (2023-06-20)
44
* 对象存储,新增请求中间件逻辑,方便拓展请求逻辑
55
* 对象存储,新增备用 UC 域名用于查询区域域名
6+
* 对象存储,修复分片上传初始化失败无法快速失败
67

78
## 7.9.0 (2023-03-31)
89
* 对象存储,修复无法对 key 为空字符串的对象进行操作

src/Qiniu/Storage/ResumeUploader.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,10 @@ private function uploadV2($fname, $blkputRets = null)
283283
$uploaded = 0;
284284
$partNumber = 1;
285285
$encodedObjectName = $this->key ? \Qiniu\base64_urlSafeEncode($this->key) : '~';
286-
287286
$isResumeUpload = $blkputRets !== null;
287+
288+
// 初始化 upload id
289+
$err = null;
288290
if ($blkputRets) {
289291
if (isset($blkputRets["etags"]) && isset($blkputRets["uploadId"]) &&
290292
isset($blkputRets["expiredAt"]) && $blkputRets["expiredAt"] > time() &&
@@ -298,12 +300,16 @@ private function uploadV2($fname, $blkputRets = null)
298300
$uploaded = $blkputRets["uploaded"];
299301
$partNumber = count($this->finishedEtags["etags"]) + 1;
300302
} else {
301-
$this->makeInitReq($encodedObjectName);
303+
$err = $this->makeInitReq($encodedObjectName);
302304
}
303305
} else {
304-
$this->makeInitReq($encodedObjectName);
306+
$err = $this->makeInitReq($encodedObjectName);
307+
}
308+
if ($err != null) {
309+
return array(null, $err);
305310
}
306311

312+
// 上传分片
307313
fseek($this->inputStream, $uploaded);
308314
while ($uploaded < $this->size) {
309315
$blockSize = $this->blockSize($uploaded);
@@ -452,9 +458,15 @@ private function blockSize($uploaded)
452458

453459
private function makeInitReq($encodedObjectName)
454460
{
455-
$res = $this->initReq($encodedObjectName);
456-
$this->finishedEtags["uploadId"] = $res['uploadId'];
457-
$this->finishedEtags["expiredAt"] = $res['expireAt'];
461+
list($ret, $err) = $this->initReq($encodedObjectName);
462+
463+
if ($ret == null) {
464+
return $err;
465+
}
466+
467+
$this->finishedEtags["uploadId"] = $ret['uploadId'];
468+
$this->finishedEtags["expiredAt"] = $ret['expireAt'];
469+
return $err;
458470
}
459471

460472
/**
@@ -468,7 +480,12 @@ private function initReq($encodedObjectName)
468480
'Content-Type' => 'application/json'
469481
);
470482
$response = $this->postWithHeaders($url, null, $headers);
471-
return $response->json();
483+
$ret = $response->json();
484+
if ($response->ok() && $ret != null) {
485+
return array($ret, null);
486+
}
487+
488+
return array(null, new Error($url, $response));
472489
}
473490

474491
/**

0 commit comments

Comments
 (0)