在根目录,我正在尝试发出从浏览器,通过api到S3存储桶的请求 . 有效载荷包含视频/图像(大文件)和一些相关数据 .
如何在不解析整个有效负载的情况下使用api中的相关数据(因为视频可能非常大),并且只将视频/图像传递给S3?
// html react
const formData = new FormData();
formData.append('file', `${this.state.file}` );
formData.append('userId', 'bob1');
formData.append('filename', 'video.mp4');
formData.append('group', 'animals');
fetch('http://localhost:8282/file', {
method: 'POST',
mode: 'no-cors',
body: formData
})
.catch(error => error);
而这里有点我将api作为一个连续的流
// hapi.js server
const server = Hapi.server({
port: 8282,
host: 'localhost'
});
server.route({
method: 'POST',
path: '/file',
handler: (request, h) => {
const params = {
Body: request.payload,
Bucket: 'bucket1',
Key: `video_${uuid().mp4}`
};
const upload = new AWS.S3().upload(params);
const promisedUpload = upload.promise();
// 1) how do I get just the userId and group information from the payload here to save to a db?
// 2) and how do I pipe the stream along to s3 without that userId and group information tagging along?
return promisedUpload
.then((response) => {
return response;
})
.catch((error) => {
return error;
});
},
options : {
payload: {
allow: 'multipart/form-data',
maxBytes: 204857600,
output: 'stream',
parse: false, // prevents putting a giant video upload into memory
timeout: 20000
}
}
});
Things I've tried:
我已经尝试制作自己的转换流,但传入的流是一件事(缓冲区?或块?)而不是分成几部分 .
class myTransform extends Transform {
constructor() {
super({ objectMode: true, highWaterMark: 1 });
}
_transform(chunk, encoding, callback) {
if(chunk.metadata.fieldname === 'file'){
callback(null, chunk.field);
}
else {
callback(); // callback(null, null); ?
}
}
}
我已经尝试过使用multiparty和busboy,但我认为这两个都依赖于_810630解析请求,因为当我使用其中任何一个时,我都没有触发任何事件 . :(
multipart-upload-stream看起来有点前途,因为我能够像对象一样使用请求,只选择我需要的部分,但当我尝试将数据输出到S3上传时,它最终成为一个空文件 . 我无法将其写入磁盘(也是一个空文件) .
所以我有点迷茫,这种流分裂是否正在尝试做甚至可能?我是否尝试以不被设计使用的方式使用流?