1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| @api.route('/upload/<string:chunk_number>', methods=['POST']) def upload(chunk_number): """处理大文件上传""" chunk_number = int(chunk_number) + 1 first = chunk_number == 1 file_id = request.args.get('fileId') file_name = request.args.get('fileName') obs_upload_id = request.args.get('uploadId') total_chunks = int(request.args.get('totalChunks')) chunk = request.data
try: if first: resp = obs.initiateMultipartUpload( bucketName=current_app.config['HUAWEI_OBS_BUCKET'], objectKey=file_name ) current_app.logger.info('InitiateMultipartUpload resp', resp) obs_upload_id = resp.body.uploadId
resp = obs.uploadPart( bucketName=current_app.config['HUAWEI_OBS_BUCKET'], objectKey=file_name, partNumber=chunk_number, uploadId=obs_upload_id, object=io.BytesIO(chunk) ) current_app.logger.info('UploadPart resp', resp)
if chunk_number == total_chunks: resp = obs.listParts( bucketName=current_app.config['HUAWEI_OBS_BUCKET'], objectKey=file_name, uploadId=obs_upload_id ) current_app.logger.info('ListParts resp', resp)
etags = [] for part in resp.body.parts: etags.append(CompletePart(partNum=part.partNumber, etag=part.etag))
resp = obs.completeMultipartUpload( bucketName=current_app.config['HUAWEI_OBS_BUCKET'], objectKey=file_name, uploadId=obs_upload_id, completeMultipartUploadRequest=CompleteMultipartUploadRequest(parts=etags) ) current_app.logger.info('CompleteMultipartUpload resp', resp)
if first: return jsonify({'uploadId': obs_upload_id}) else: return Success()
except Exception as e: current_app.logger.error(e) raise ServerError(msg='华为云OBS错误,文件上传失败')
|