1
將我的反應前端中的文件發佈到節點後端。如何將圖像上傳到節點中的s3
request
.post('/api/upload')
.field('fileName', res.body.text)
.field('filePath', `/${this.s3DirName}`) // set dynamically
.attach('file', data.file)
.end((err2, res2) => {
if (err2){
console.log('err2', err2);
this.setState({ error: true, sending: false, success: true });
}else{
console.log('res2', res2);
this.setState({ error: false, sending: false, success: true });
}
});
然後在我的節點後端我想上傳到s3。我使用busboy能夠獲取發佈的多部分文件,然後將aws sdk發送到我的s3存儲桶。
var AWS = require('aws-sdk');
const s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: 'bucketName'}
});
static upload(req, res) {
req.pipe(req.busboy);
req.busboy.on('file', (fieldname, file, filename) => {
console.log("Uploading: " + filename);
console.log("file: ", file);
var params = {
Bucket: 'bucketName',
Key: filename,
Body: file
};
s3.putObject(params, function (perr, pres) {
if (perr) {
console.log("Error uploading data: ", perr);
res.send('err')
} else {
console.log("Successfully uploaded data to myBucket/myKey");
res.send('success')
}
});
});
}
,但我得到的錯誤
Error uploading data: { Error: Cannot determine length of [object Object]
上午我直接上傳文件對象糾正或做我需要解析的呢?也許我應該使用uploadFile而不是putObject?
如果有幫助,這是我的console.logs,我日誌的文件和文件名
Uploading: 31032017919Chairs.jpg
file: FileStream {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: { end: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
truncated: false,
_read: [Function] }
完美。這工作。謝謝弗拉德 – Stretch0