【发布时间】:2020-06-02 13:50:57
【问题描述】:
我正在尝试将多个 pdf 文件从外部源临时下载到我的 nodejs 服务器(在 Heroku 中)并将其上传到 AWS S3 存储桶。
我尝试了多种方法,所有这些方法在我的本地机器上都可以正常工作,但在 Heroku Dyno NodeJS Server 中却不行。我什至无法在 Heroku 中创建文件夹。我猜是因为权限有限。
在节点中
1) 使用 var download = require('download-file') (目前在下面的代码中使用)
2) axios
3) res.download()
下载文件代码
const downloadFiles = async (unique_files) => {
for (let index = 0; index < unique_files.length; index++) {
let file_ext = unique_files[index].substr(unique_files[index].length - 4);
if(file_ext == ".pdf") {
await downloadzz(unique_files[index])
}
}
}
function downloadzz(link) {
download(link, function(err){
if (err) throw err
console.log("DOWNLOAD Complete");
});
}
上传文件代码
const uploadFiles = async (unique_files) => {
for (let index = 0; index < unique_files.length; index++) {
let file_ext = unique_files[index].substr(unique_files[index].length - 4);
if(file_ext == ".pdf") {
await uploadzz(unique_files[index])
}
}
}
function uploadzz(link) {
fs.readFile(require('path').resolve(__dirname+'/../external-pdfs/', link.slice(link.lastIndexOf('/') + 1)), function (err, data) {
params = {Bucket: pdfBucket, Key: link.slice(link.lastIndexOf('/') + 1), Body: data, ACL: "public-read" };
s3.putObject(params, function(err, data) {
if (err) {
console.log("Failed Upload", err);
} else {
console.log("Successfully uploaded data to bucket", data);
}
});
});
}
我没有收到任何错误,但在 heroku 服务器上似乎不存在名称为 external-pdfs 的文件夹。
我愿意寻求更好的解决方案:例如,直接将文件从外部 url 上传到 s3... 如何从外部 url 读取文件并直接上传到 AWS S3 存储桶?
【问题讨论】:
标签: node.js amazon-web-services heroku amazon-s3