node.js - 读取文件数组并一一读取Nodejs
问题描述
首先我想说我对 JS 比较陌生,所以我欢迎任何有用的建议。
这是它应该做的:
- 读取目录文件
- 循环遍历文件数组
- 应该:读取文件并将其上传到 s3
它能做什么:
- 读取目录文件✅</li>
- 循环遍历文件数组✅</li>
- 永远不要通过这个:
文件大小为:1 到 2 mb,12 个文件长度或 6 个,无论哪种方式都不起作用。
if (content.length < 1) return console.log("Content < 1")
代码:
async s3() {
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_KEY,
})
const s3 = new AWS.S3()
const destination = path.join(Utils.getRootPath(), 'uploads', this.email)
try {
const files = fs.readdirSync(destination)
if (!files || files.length == 0) return console.log(`Provided folder '${destination}' is empty or does not exist.`);
for (const fileName of files) {
const filePath = path.join(destination, fileName)
let content = fs.readFileSync(filePath)
if (content.length < 1) return console.log("Content < 1")
s3.upload({
ACL: 'public-read',
Bucket: process.env.AWS_BUCKET_NAME,
Key: fileName,
Body: fileContent,
}).promise().then(async (uploadData) => {
try {
const headData = await s3.headObject({
Bucket: process.env.AWS_BUCKET_NAME,
Key: fileName,
}).promise();
return console.log(headData);
}
catch (err) {
console.log(err);
}
})
console.log(`${fileName} uploaded.`)
}
} catch (error) {
throw new Error(error)
}
呼叫者:
async container() {
if (this.email === null) throw new Error('Constructor of Job class is null')
try {
await this.placeOrder();
await Utils.downloadFile(this.email);
await this.s3();
return console.log("DONE!")
}
catch (err) {
// return new Error(err);
// OBRADA OVDE!
console.log(err)
}
}
解决方案
假设我理解正确,有几个问题。首先,不要从for-of
循环中返回 when content.length < 1
,而应该只是continue
. 否则您将不会继续处理剩余的文件。其次,将 promise 和async/await
. 您可以await
使用s3.upload
.
class FileManager {
async s3() {
console.log('ok')
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_KEY,
});
const s3 = new AWS.S3();
const destination = path.join(Utils.getRootPath(), "uploads", this.email);
try {
const files = fs.readdirSync(destination);
if (!files || files.length == 0)
return console.log(`Provided folder '${destination}' is empty or does not exist.`);
for (const fileName of files) {
const filePath = path.join(destination, fileName);
let content = fs.readFileSync(filePath);
if (content.length < 1) {
console.log("Content < 1");
continue;
}
await s3.upload({
ACL: "public-read",
Bucket: process.env.AWS_BUCKET_NAME,
Key: fileName,
Body: fileContent,
});
try {
const headData = await s3
.headObject({
Bucket: process.env.AWS_BUCKET_NAME,
Key: fileName,
})
.promise();
console.log(headData);
} catch (err) {
console.log(err);
}
console.log(`${fileName} uploaded.`);
}
} catch (error) {
throw new Error(error);
}
}
}
推荐阅读
- mybatis - mybatis 支持检查属性是否存在?
- html - 嵌入在对象标签中的 txt 文件在应用程序运行时会出现编码错误?
- bash - AWK 找到一种方法来打印包含逗号分隔字符串中的单词的行
- javascript - 如何获取用户正在玩的游戏?不和谐.js
- input - 我怎样才能打破循环?真时:
- vue.js - 在vue js中将模板文本作为html元素注入
- c - 在c中查找数组的平均值,最大值,最小值
- javascript - 如何使用输入标签字段而不是默认django?
- google-apps-script - GAS 高级幻灯片 API 批量更新 updateParagraphStyle
- python-3.x - Selenium 的 .click() 的神秘不当行为