0

尝试将图像 Openshift node/express/multer 上传到 AWS 时出现问题。控制台告诉我,同一图像有多次上传,AWS 上的结果文件被截断和损坏。我把所有的客户端ajax都拿出来了,只是使用标准的表单上传。表达 3.2.5。感谢您的任何想法。

控制台输出:

in FileUploadData
in FileUploadData
in s3.put
Successfully uploaded data to myBucket/myKey atest.jpg
in s3.put
Successfully uploaded data to myBucket/myKey atest.jpg
in FileUploadData
in FileUploadData
in s3.put
Successfully uploaded data to myBucket/myKey atest.jpg
in FileUploadData
in s3.put
Successfully uploaded data to myBucket/myKey atest.jpg
in FileUploadData
in no req.files
in s3.put
Successfully uploaded data to myBucket/myKey atest.jpg
in s3.put
Successfully uploaded data to myBucket/myKey atest.jpg

这并不一致,但总是 FileUploadData 的多个触发器。从 stackoverflow 线程剪切和粘贴的节点代码。

app.use(multer({
    limits : { fileSize:100000 },
    rename: function (fieldname, filename) {
        return filename.replace(/\W+/g, '-').toLowerCase();
    },
    onFileUploadData: function (file, data, req, res) {
    // file : { fieldname, originalname, name, encoding, mimetype, path, extension, size, truncated, buffer }
        console.log("in FileUploadData");
        var params = {
            Bucket: 'your20images',
            Key: file.name,
            Body: data,
            ContentType: 'image/jpeg'

        };
        s3.putObject(params, function (perr, pres) {
            console.log("in s3.put");
            if (perr) {
                console.log("Error uploading data: ", perr);
            } else {
                console.log("Successfully uploaded data to myBucket/myKey "+file.name);
            }
        });
    }
}));

客户端表单

<form method="post" enctype="multipart/form-data" action="/upload" id="upform">
    <input type="file" name="imgupload"><br /><br /><input type="submit">
</form>

- - - - - - - - - - - 编辑 - - - - - - - - - - - - - -

下面接受的答案中解释了多个控制台日志。我找到的暂定解决方案是在 onFileUploadComplete 中发送到 AWS。怀疑有更好的方法,但如果有人得到这么远的波纹管为我工作。

onFileUploadComplete: function (file, data, req, res) {
        console.log("complete");
        fs.readFile(data.files.imgupload[0].path, function(err, data){
        if(err) { console.log("image rename error: "+err) }
        var params = {
            Bucket: 'myBucket',
            Key: file.name,
            Body: data,
            ContentType: 'image/jpeg'
        };
        s3.putObject(params, function (err, res) {
            if (err) {
                console.log("Error uploading data: ", err);
            } else {
                console.log("Successfully uploaded data to myBucket/myKey");
            }
        });
    });
}
4

1 回答 1

2

正如文档所述,为每个文件的每个onFileUploadData调用。因此可以很容易地为同一个文件多次调用它。

于 2015-05-10T23:00:42.490 回答