|
|
@ -223,7 +223,7 @@ class Upload extends AdminBaseController |
|
|
|
public function uploadPart(){ |
|
|
|
try { |
|
|
|
$param = $this->request->param(); |
|
|
|
if (empty($param['uploadId']) || empty($param['key']) || empty($param['partNumber'])) { |
|
|
|
if (empty($param['uploadId']) || empty($param['partNumber'])) { |
|
|
|
return json(['code' => 400, 'message' => '缺少参数']); |
|
|
|
} |
|
|
|
$file = $this->request->file('file'); |
|
|
@ -239,6 +239,11 @@ class Upload extends AdminBaseController |
|
|
|
if (!$openFile) { |
|
|
|
return json(['code' => 400, 'message' => 'Failed to open file']); |
|
|
|
} |
|
|
|
// 检查数据库中是否有对应上传记录 |
|
|
|
$awsS3 = AwsS3Model::where(['upload_id'=>$param['uploadId']])->find(); |
|
|
|
if (empty($awsS3) || empty($awsS3['key'])) { |
|
|
|
return json(['code' => 400, 'message' => 'uploadId对应的数据错误']); |
|
|
|
} |
|
|
|
// 初始化s3客户端 |
|
|
|
$s3Config = Config::get('common.aws_s3'); |
|
|
|
$s3Client = new S3Client([ |
|
|
@ -251,13 +256,30 @@ class Upload extends AdminBaseController |
|
|
|
]); |
|
|
|
$result = $s3Client->uploadPart([ |
|
|
|
'Bucket' => $s3Config['aws_bucket'], |
|
|
|
'Key' => $param['key'], |
|
|
|
'Key' => $awsS3['key'], |
|
|
|
'PartNumber' => $param['partNumber'], |
|
|
|
'UploadId' => $param['uploadId'], |
|
|
|
'Body' => $openFile, |
|
|
|
])->toArray(); |
|
|
|
fclose($openFile); |
|
|
|
Log::info("上传分段结果:".json_encode($result)); |
|
|
|
if (empty($result['partNumber']) || empty($result['ETag'])) { |
|
|
|
return json(['code' => 400, 'message' => '上传分片失败']); |
|
|
|
} |
|
|
|
|
|
|
|
// 更新上传分片记录 |
|
|
|
if (empty($awsS3->parts)) { |
|
|
|
$buildParts = json_encode([ |
|
|
|
['PartNumber'=>trim($param['partNumber']), 'ETag'=>$result['ETag']] |
|
|
|
]); |
|
|
|
$awsS3->parts = $buildParts; |
|
|
|
} else { |
|
|
|
$parsePart = json_decode($awsS3->parts, true); |
|
|
|
$parsePart[] = ['PartNumber'=>trim($param['partNumber']), 'ETag'=>$result['ETag']]; |
|
|
|
$awsS3->parts = json_encode($parsePart); |
|
|
|
} |
|
|
|
$awsS3->save(); |
|
|
|
|
|
|
|
return json([ |
|
|
|
'code' => 200, |
|
|
|
'message' => 'success', |
|
|
@ -279,17 +301,19 @@ class Upload extends AdminBaseController |
|
|
|
public function completeUpload(){ |
|
|
|
try { |
|
|
|
$param = $this->request->param(); |
|
|
|
if (empty($param['uploadId']) || empty($param['key'])) { |
|
|
|
if (empty($param['uploadId'])) { |
|
|
|
return json(['code' => 400, 'message' => '缺少参数']); |
|
|
|
} |
|
|
|
if (empty($param['parts']) || !is_array($param['parts'])) { |
|
|
|
return json(['code' => 400, 'message' => 'parts参数错误']); |
|
|
|
} |
|
|
|
// 注意:$parts 是一个数组,里面需要有每个分段编号,以及每个分段对应的ETag |
|
|
|
// $parts = [ |
|
|
|
// ['PartNumber' => 1, 'ETag' => 'etag-for-part-1'], |
|
|
|
// ['PartNumber' => 2, 'ETag' => 'etag-for-part-2'], |
|
|
|
// ]; |
|
|
|
$awsS3 = AwsS3Model::where(['upload_id'=>trim($param['uploadId'])])->find(); |
|
|
|
if (empty($awsS3) || empty($awsS3['parts']) || empty($awsS3['key'])) { |
|
|
|
return json(['code' => 400, 'message' => 'uploadId对应的数据错误']); |
|
|
|
} |
|
|
|
$parts = json_decode($awsS3['parts']); |
|
|
|
|
|
|
|
// 初始化s3客户端 |
|
|
|
$s3Config = Config::get('common.aws_s3'); |
|
|
@ -303,19 +327,15 @@ class Upload extends AdminBaseController |
|
|
|
]); |
|
|
|
$result = $s3Client->completeMultipartUpload([ |
|
|
|
'Bucket' => $s3Config['aws_bucket'], |
|
|
|
'Key' => $param['key'], |
|
|
|
'Key' => $awsS3['key'], |
|
|
|
'UploadId' => $param['uploadId'], |
|
|
|
'MultipartUpload' => [ |
|
|
|
'Parts' => $param['parts'] |
|
|
|
'Parts' => $parts |
|
|
|
], |
|
|
|
])->toArray(); |
|
|
|
Log::info("完成分段上传结果:".json_encode($result)); |
|
|
|
|
|
|
|
// 更新分片上传记录 |
|
|
|
$awsS3 = AwsS3Model::where(['upload_id'=>$param['uploadId']])->find(); |
|
|
|
if (empty($awsS3)) { |
|
|
|
return json(['code' => 500, 'message' => '数据记录错误']); |
|
|
|
} |
|
|
|
$awsS3->is_complete = 1; |
|
|
|
$awsS3->parts = json_encode($param['parts']); |
|
|
|
$awsS3->s3_url = $result['Location']; |
|
|
@ -324,7 +344,6 @@ class Upload extends AdminBaseController |
|
|
|
return json(['code' => 200, 'message' => '上传成功', 'data'=>[ |
|
|
|
'id' => $awsS3->id, |
|
|
|
'uploadId' => $param['uploadId'], |
|
|
|
'key' => $param['key'], |
|
|
|
'location' => $result['Location'], |
|
|
|
]]); |
|
|
|
} catch (\Exception $exception) { |
|
|
|