我正在使用React和Node进行文件上传和下载服务。我在使用AWS S3的Digital Ocean上托管文件。以下代码就像冠军一样(直接来自其文档)。
`// OG代码 app.get(“ / test-download /:id”,(req,res)=> { var params = { 值区:secretBucketName, 密钥:secretKeyStuff };
s3.getObject(params, function(err, data) {
//
console.log(data);
//
if (!err) {
res.send({ data, key: params.Key });
} else {
// an error occurred
console.log({ err });
}
});
});`
但是,无论何时我尝试下载先前已上传的文件,其大小超过100mb,要么服务器崩溃,要么JavaScript堆内存不足,要么出现其他错误。
因此,我尝试流式传输getObject请求并可以成功运行该请求。但是,我在前端收到一个奇怪的响应,并且不确定是否需要在流传输之前转换数据还是在转换之后...
app.get("/test-download/:id", (req, res) => {
var params = {
Bucket: secretBucketName,
Key: secretKeyStuff
};
// This Streams
s3.getObject(params)
.createReadStream()
.pipe(res)
.on("finish", () => {
console.log("** done");
});
});
前端代码。
downloadFile = (id, name, type) => {
axios
.get(
`/test-download/${id}`,
this.props.handleSnackBar("Your download has been started.")
)
.then(res => {
download(
// Stream doesn't recognize res.data.data.Body.data
// I'm assuming b/c of the data format in which is being returned
new Blob([new Uint8Array(res.data.data)], { type: "octet/stream" }),
`${name}.${type}`
);
console.log(res.data);
})
.catch(err => console.log(err));
};
带有节点流的前端响应。
Front end response with node stream
有关此图像传达什么的任何想法?如果您能指出正确的方向,那您将是最大的。enter code here