我的目标是下载一个PDF文件,我已经保存在我的亚马逊S3桶。

正如您在下面的代码片段中所看到的,每当向/api/download发出GET请求时,我都会将url发回给客户机。
index.js
const express = require("express");
const downloadRoutes = require("./routes/downloadRoutes");
const app = express();
app.use("/api/download", downloadRoutes);
app.listen(5000, () => {
console.log("Server listening on port 5000");
});路由/下载Routes.j
const express = require("express");
const dotenv = require("dotenv");
const AWS = require("aws-sdk");
dotenv.config();
const router = express.Router();
const s3 = new AWS.S3({
accessKeyID: process.env.AMAZON_ACESS_KEY_ID,
secretAccessKey: process.env.AMAZON_SECRET_ACCESS_KEY,
});
router.get("/", (req, res) => {
s3.getSignedUrl(
"getObject",
{
Bucket: "download-hemanta-cv",
ContentType: "application/pdf",
Key: "CV_Hemanta_Sundaray.pdf",
},
(err, url) => {
res.send(url);
}
);
});
module.exports = router;不过,当我以邮递员的身份向上述路线提出要求时,我并没有得到任何回应。我应该从亚马逊S3得到签名的网址。但是,状态代码是200 OK &没有错误。

我找不出问题。帮帮忙吧。
发布于 2021-02-22 20:46:14
我看到两件事:
,
ResponseContentType而不是ContentType,或者我们可以忽略它。res.send(url)是在成功和错误的情况下被调用的。以下是修改后的方法:
router.get("/", (req, res, next) => {
s3.getSignedUrl(
"getObject",
{
Bucket: "download-hemanta-cv",
ResponseContentType: "application/pdf",
Key: "CV_Hemanta_Sundaray.pdf",
},
(err, url) => {
console.log("error", err, "url", url);
if (err) {
next(err);
} else {
res.send(url);
}
}
);
});https://stackoverflow.com/questions/66320844
复制相似问题