AWS.config.loadFromPath('./models/a.json');读取配置文件一直报错 [egg-core] load file: E:\HLWechat-API\app\middleware\aws.js, error: ENOENT: no such file or directory, open './models/config.json'
Q
[eggjs/egg]AWS S3凭证文件问题
0
A
回答
4
Hello @Dilmoon2016. Please provide a reproducible example following the instruction.
Issues labeled by Need Reproduce
will be closed if no activities in 7 days.
@Dilmoon2016,请根据这个说明提供最小可复现代码。
如果在 7 天内没有进展会被自动关闭。
1
中间件
// middleware
async function gzip(ctx, next) {
await next();
// example
const configPath = path.join(ctx.config.baseDir, 'app/models/a.json');
const s3 = AWS.config.loadFromPath('./models/a.json');
// recommend
const awsConfig = ctx.config.aws;
const s3 = new AWS.S3(awsConfig);
}
推荐配置
// config/config.{env}.js
{
aws: {
// YOUR_CONFIG
}
}
5
@thonatos s3的链接还是失败的能不能加个联系方式QQ:302950703
3
不清楚使用场景,但根据以往经验:
- client 初始化不应该放进中间件
- s3 如何使用可以查看 aws 文档
- 提问建议加上复现仓库或者提供示例代码,就你提供的错误信息,是文件加载路径不对
5
@thonatos 代码如下 var fs = require('fs'); var AWS = require('aws-sdk'); AWS.config.loadFromPath('./config.json'); aws的文档地址:https://docs.aws.amazon.com/zh_cn/sdk-for-javascript/v2/developer-guide/loading-node-credentials-json-file.html 报错:Error: [egg-core] load file: E:\HLWechat-API\app\controller\uploadimg.js, error: ENOENT: no such file or directory, open './config.json'; 路径已经放到同级目录下了
9
@Dilmoon2016
// 同级目录
const configPath = path.join(__dirname, 'config.json');
AWS.config.loadFromPath(configPath);
// 从项目目录查找
const configPath = path.join(ctx.config.baseDir, 'app/models/a.json');
AWS.config.loadFromPath(configPath);
错误提示很明显了,加载文件路径有问题,node 基础知识请自行补充。
9
非常感谢,问题解决了