我在Nodejs中实现这一点,将文件(照片,mp3,pdf)上传到亚马逊网络服务s3。目前我正在尝试上传一个mp3文件,但我一直得到错误:“TypeError:无法读取未定义的属性'路径'”。我正在使用Multipart中间件。
var fs = require('fs');
//S3FS instance
var S3FS = require('s3fs')
//multiparty middleware (to upload our file)----> S3FS will read it's object and stream it to AWS
//nmp install connect-multiparty
var multiparty = require('connect-multiparty'),
multipartyMiddleware = multiparty();
var express = require('express');
var route = express.Router();
module.exports = route;
route.use(multipartyMiddleware);
//create new filesystem
var s3fsImpl = new S3FS('unasbeatz', {
accessKeyId: 'xxxxxxx',
secretAccessKey: 'xxxxx'
});
//create bucket ----> unasbeatz
s3fsImpl.create();
route.post('/', function (req, resp) {
//get the file from our request object (from multiparty object, it sets the request object)
var file = req.files.file;
//create new readstream
var stream = fs.createReadStream(file.path);
//write to AWS using stream
return s3fsImpl.writeFile(file.originalFileName, stream).then(function () {
fs.unlink(file, function (err) {
if (err)
console.error(err);
})
//else we write uploaded
console.log('uploaded');
})
})
route.get('/', function (req, resp) {
resp.json({ SecretData: 'abcde' })
});
var stream:直接抓取每个上传文件的一部分。但我不知道为什么会出现错误
错误:
TypeError: Cannot read property 'path' of undefined
at /Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/app/routes/mp3_route.js:34:42
at Layer.handle [as handle_request] (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/layer.js:95:5)
at /Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:330:12)
at next (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:271:10)
at multipart (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/connect-multiparty/index.js:50:53)
at Layer.handle [as handle_request] (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:312:13)
at /Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:330:12)
at next (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:176:3)
at router (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:46:12)
答案 0 :(得分:1)
使用busboy接收路线中的文件。
答案 1 :(得分:0)
我最终实现的是在AWS中更新我的文件并将其保存在MongoDB中,然后在我的Web应用程序中重新获取这些URL以在浏览器中显示这些文件