javascript - 无法从 Node.js (express) 服务返回 xlsx 文件,该服务从 S3 存储桶获取该文件以供浏览器应用程序使用
问题描述
在 Excel 中创建了一个 xlsx 文件,通过控制台从桌面手动上传到 AWS S3。现在想要向 Node.js 快速服务添加一个端点,该服务将从 S3 检索该文件并将其传递回 javascript 浏览器应用程序。
纯文本文件似乎很好,但不断为 xlsx 文件(甚至在 Excel 中创建的 csv)获取 HTTP 状态代码 500。
尝试了以下变化:
res.send(s3.getObject().toString()); //this works with plain text file
res.send(s3.getObject().toString('utf-8'));
res.send(s3.getObject().toString('binary'));
这些都没有运气,调用立即失败,出现 500 内部服务器错误。
解决方案
就像是
s3.headObject(params,
(err, data)=>{
if (err) {
return res.status(500).end(err.message);
}
// Add the content type to the response (it's not propagated from the S3 SDK)
res.set({
'Content-Type': data.ContentType,
'Content-Length': data.ContentLength,
'Last-Modified': data.LastModified,
'ETag': data.ETag
});
s3.getObject(params).createReadStream().pipe(res)
}
);
推荐阅读
- mysql - 使用 GRANT 分配 CREATE USER、CREATE ROLE、SHOW DB 和动态权限等权限
- sql - 如何在 DB2 中使用 BASE64DECODE 函数
- python - 将多个网页抓取到单个 csv 中
- unity3d - 如何修复“interstitialAd.IsLoaded() 离线返回 true”(统一 C#)
- php - 在mysql中更新日期和时间时如何修复laravel不正确的日期和时间
- javascript - 在 firefox 扩展中使用 javascript 和 jQuery 从页面下载多个文件
- regex - 正则表达式从字符串中获取三个数字并将它们放入 bash 中的变量中
- sql - 如何获取两个日期戳之间的时间差并按降序排列
- gremlin - 计算路径上连续顶点之间的连接
- java - Java 11 或 12 是否支持 Struts 1.3?