无法将pdf文件上传到AWS s3存储桶 - PHP

时间:2018-03-05 12:17:25

标签: php amazon-s3

我正在尝试将文件从文件上传网页上传到AWS S3存储桶。我能够连接到我的s3存储桶,我可以通过naking PHP调用创建文件夹,但我似乎无法上传文件。我得到的错误是:

document = Document(path_to_your_docx)
tables = document.tables
for table in tables:
    for row in table.rows:
        for cell in row.cells:
            for paragraph in cell.paragraphs:
                print(paragraph.text)

HTML文件上传代码:

 1) 'Undefined index: file1 in '
 2) 'Error:Found 2 errors while validating the input provided for the PutObject operation:[Key] is missing and is a required parameter [Key] expected string length to be >= 1, but found string length of 0'

我的PHP代码:

 <form id="uploadDoc" enctype="multipart/form-data" method="post">
        <input type="file" name="fileSelect" id="fileSelect">
        <input type="button" value="uploadValue" onclick="upld.uploadFile()">
        <progress id="progressBar" value="0" max="100" style="width: 300px;"></progress>
        <div id="uploadStatus">status</div>
        <div id="loaded_n_total">error msessage</div>

    </form>

JS:

 $bucketName = 'testlabbookbucket1';


try{
    $tempPath =  $_FILES["file1"]['tmp_name'];
    $folder = 'lseMember';
    $fileName = $_FILES["file1"]['name'];

    $result = $client -> putObject(array(
        'Bucket' => $bucketName,
        'Key' => $fileName,
        'SourceFile' => $tempPath
    ));


}catch (S3Exception $e) {
    die('Error:' . $e->getMessage());
} catch (Exception $e) {
    die('Error:' . $e->getMessage());
}

}

以下是此AWS s3的存储桶政策:

 // Onclick upload file
 upld.uploadFile = function(){

// Get selected file
var file = upld.el('fileSelect').files[0];
console.log(file.name);
console.log(file.size);
console.log(file.type);


var formdata = new FormData();
formdata.append('file1', file);

var xmh = new XMLHttpRequest;
xmh.onreadystatechange = function(){
    if(xmh.readyState == 4 && xmh.status == 200){

        var response = xmh.responseText;

        console.log('response: '+response);
    }
}
// Event listeners
xmh.upload.addEventListener('progress', upld.progressHandler, false);
xmh.addEventListener('load', upld.completeHandler, false);
xmh.addEventListener('error', upld.errorHandler, false);
xmh.addEventListener('abort', upld.abortHandler, false);

xmh.open('POST', 's3Test.php');
xmh.send();

}

1 个答案:

答案 0 :(得分:1)

您正在初始化要在XMLHttpRequestformdata发送的数据,但您实际上并未发送该数据。将uploadFile函数中的最后一行更改为:

xmh.send(formdata);