我正在编写将文件写入 s3 的服务,但偶尔会遇到此MODULE_NOT_FOUND
错误。消息是"Could not find API configuration s3-2006-03-01"
。它似乎发生在集群中(多次调用这个函数都会出错,然后另一个请求不会出错),我在本地从未遇到过这个错误。
我没有将明确的配置引用传递给new AWS.S3()
,但这似乎不是问题……如果您遇到/解决了这个问题,很想收到您的来信。
错误和堆栈如下:
{
"message":"Could not find API configuration s3-2006-03-01",
"name":"MODULE_NOT_FOUND",
"stack":"MODULE_NOT_FOUND: Could not find API configuration s3-2006-03-01
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.get [as 2006-03-01] (.../node_modules/aws-sdk/clients/s3.js:11:17)
at Object.apiLoader (.../node_modules/aws-sdk/lib/api_loader.js:5:33)
at Function.defineServiceApi (.../node_modules/aws-sdk/lib/service.js:575:22)
at features.constructor.getLatestServiceClass (.../node_modules/aws-sdk/lib/service.js:91:19)
at features.constructor.loadServiceClass (.../node_modules/aws-sdk/lib/service.js:81:19)
at features.constructor.Service [as constructor] (.../node_modules/aws-sdk/lib/service.js:27:29)
at new features.constructor (.../node_modules/aws-sdk/lib/util.js:602:24)
at .../app/async/worker.js:122:20
at Promise._execute (.../node_modules/bluebird/js/release/debuggability.js:303:9)
at Promise._resolveFromExecutor (.../node_modules/bluebird/js/release/promise.js:483:18)
at new Promise (.../node_modules/bluebird/js/release/promise.js:79:10)
at s3upload (.../app/async/worker.js:121:12)",
"code":"MODULE_NOT_FOUND"
}
.../node_modules/aws-sdk/clients/s3.js 代码如下(引用的文件都在,而且好像没有一直报错):
require('../lib/node_loader');
var AWS = require('../lib/core');
var Service = AWS.Service;
var apiLoader = AWS.apiLoader;
apiLoader.services['s3'] = {};
AWS.S3 = Service.defineService('s3', ['2006-03-01']);
require('../lib/services/s3');
Object.defineProperty(apiLoader.services['s3'], '2006-03-01', {
get: function get() {
var model = require('../apis/s3-2006-03-01.min.json');
model.paginators = require('../apis/s3-2006-03-01.paginators.json').pagination;
model.waiters = require('../apis/s3-2006-03-01.waiters2.json').waiters;
return model;
},
enumerable: true,
configurable: true
});
module.exports = AWS.S3;
我的代码(/app/async/worker.js
)调用了这个:
const AWS = require('aws-sdk');
function s3upload(file_location, upload_name, bucket, public = false) {
return new BPromise(function(resolve, reject) {
const s3 = new AWS.S3(); // STACK TRACE REFERENCES THIS LINE
let body = fs.createReadStream(file_location);
let upload_options = {
Bucket: bucket,
Key: upload_name,
Body: body
};
if (public) {
upload_options.ACL = 'public-read';
}
s3.upload(upload_options, function(err, data) {
if (err) {
return reject(err);
}
resolve(data.Location);
});
});
}