严重错误:未捕获的Aws\S3\Exception\InvalidRequestException:AWS错误代码:InvalidRequest,状态码:400,AWS请求ID:B1A28EBE65521DF4,AWS错误类型:客户端,AWS错误消息:您必须指定至少一个部分,User-Agent: aws-sdk-php2/2.7.22 Guzzle/3.9.2 curl/7.40.0 PHP/5.6.6。该错误发生在C:\vhosts********.com\db*****\FileUploader_v2\aws\Aws\Common\Exception\NamespaceExceptionFactory.php的第91行。
每次我使用AWS S3运行我的多部分上传程序时,都会出现上述错误。我的程序应该将文件的一部分切片,然后使用XMLHTTPRequest将其发送到php脚本中。那部分看起来很好用。但是,当调用CompleteMultipartUpload时出现问题。从错误中得出的结论是我的部分太小或为空。 upload.htm:
var command; var file; var ownerName; var totalSize; var partSize = 2 * 1024 * 1024; // 常量 var sendBackData; var totalSize; var sureUploadSize = 0, probableUplaodSize = 0; var numParts; var partsLeft = [];
每次我使用AWS S3运行我的多部分上传程序时,都会出现上述错误。我的程序应该将文件的一部分切片,然后使用XMLHTTPRequest将其发送到php脚本中。那部分看起来很好用。但是,当调用CompleteMultipartUpload时出现问题。从错误中得出的结论是我的部分太小或为空。 upload.htm:
var command; var file; var ownerName; var totalSize; var partSize = 2 * 1024 * 1024; // 常量 var sendBackData; var totalSize; var sureUploadSize = 0, probableUplaodSize = 0; var numParts; var partsLeft = [];
function _(el){
return document.getElementById(el);
}
function calcTotalSize(file){
var size_total = 0;
for(var i = 0; i < file.length; i++){
size_total += file[i].size;
}
return size_total;
}
function uploadFile(){
file = _("file").files[0];
console.log(file);
ownerName = _("name").value;
totalSize = file.size;
command = 'CreateMultipartUpload';
var formdata = new FormData();
formdata.append("command", command);
formdata.append("filename", file.name);
formdata.append("name", ownerName);
var ajax = new XMLHttpRequest();
ajax.open("POST", "FileUploader.php", true);
ajax.send(formdata);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4) {
sendBackData = JSON.parse(ajax.responseText);
numParts = Math.ceil(totalSize / partSize);
uploadPart(1);
}
};
}
function uploadPart(partNum){
console.log("Uploading part " + partNum);
console.log(sendBackData['uploadId']);
command = 'UploadPart';
if (partNum > numParts) {
completeMultipartUpload();
return;
}
var formdata = new FormData();
var start = (partNum - 1) * partSize;
var end = start + partSize;
if (end > totalSize)
end = totalSize;
var length = end - start;
var curBlobPart = file.slice(start, end);
//console.log(sendBackData['uploadId']);
formdata.append("file[]", curBlobPart);
formdata.append("command", command);
formdata.append("uploadId", sendBackData['uploadId']);
formdata.append("key", sendBackData['key']);
formdata.append("partNumber", partNum);
var ajax = new XMLHttpRequest();
ajax.open("POST", "FileUploader.php", true);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.send(formdata);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4) {
uploadPart(partNum + 1);
}
};
}
function completeMultipartUpload() {
command = 'CompleteMultipartUpload';
var formdata = new FormData();
formdata.append("command", command);
formdata.append("uploadId", sendBackData['uploadId']);
formdata.append("key", sendBackData['key']);
var ajax = new XMLHttpRequest();
ajax.open("POST", "FileUploader.php", true);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.send(formdata);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4) {
alert("File uploaded successfully");
}
};
}
function progressHandler(event){
_("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
var percent = (event.loaded / event.total) * 100;
_("progressBar").value = Math.round(percent);
_("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
_("status").innerHTML = event.target.responseText;
_("progressBar").value = 0;
}
function errorHandler(event){
_("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
_("status").innerHTML = "Upload Aborted";
}`
FileUploader.php: `
//require 'SimpleImage.php';
//require 'gifsplit.php';
//require 'functions.php';
require 'config.php';
require 'aws/aws-autoloader.php';
use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\Model\MultipartUpload\UploadBuilder;
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
function sendJson($arr)
{
header('Content-Type: application/json');
die(json_encode($arr));
}
// S3
$s3 = S3Client::factory(array(
'key' => AWS_KEY,
'secret' => AWS_SECRET_KEY
));
//$part = $_POST['part'];
switch ($_POST['command']) {
case 'CreateMultipartUpload':
$key = "other/".$_POST['name']."/".$_POST['filename'];
$response = $s3->createMultipartUpload(array(
'Bucket' => TMP_IMG,
'Key' => $key
));
$uploadId = $response['UploadId'];
sendJson(array(
'uploadId' => $uploadId,
'key' => $key
));
break;
case 'UploadPart':
var_dump($_FILES['file']);
$result = $s3->uploadPart(array(
'Bucket' => TMP_IMG,
'Key' => $_POST['key'],
'UploadId' => $_POST['uploadId'],
'PartNumber'=> $_POST['partNumber'],
'Body' => $_FILES['file']['tmp_name']
));
break;
case 'CompleteMultipartUpload':
$partsModel = $s3->listParts(array(
'Bucket' => TMP_IMG,
'Key' => $_POST['key'],
'UploadId' => $_POST['uploadId']
));
$model = $s3->completeMultipartUpload(array(
'Bucket' => TMP_IMG,
'Key' => $_POST['key'],
'UploadId' => $_POST['uploadId'],
'Parts' => $partsModel['Parts']
));
sendJson(array(
'success' => true
));
break;
case 'AbortMultipartUpload':
# code...
break;
default:
# code...
break;
}
我完成任务的方式是否存在明显的问题?我的想法是,我的零件尺寸可能太小了,无法上传。如果是这种情况,我有一个解决方法,就是使用我正在开发的另一个版本的项目。