阿里云OpenAPI 抛ReferenceError怎么处理?
```const Credential = require('@alicloud/credentials');
const KmsClient = require('./alibabaKMS/index');
const fs = require('fs').promises;
const WalletDb = require('./walletDb');
const MockKmsClient = require('./alibabaKMS/localIndex');
const alibabaKMSFactory = (config, logger) => {
const alibabaKMSConfig = config.get('alibabaKMS')
const walletDb = new WalletDb(config);
const login = async (role_id, secret_id) => {
if (config.get('ENV') === 'local') {
return new MockKmsClient();
}
const uri = alibabaKMSConfig.uri;
const credentialsConfig = new Config({
type: role_id,
roleName: secret_id
})
let credentialClient = new Credential(credentialsConfig);
try {
logger.info(`alibabaKMS login start`);
const config = new credentials_1.Config({
// Seret Type
type: 'ram_role_arn',
accessKeyId: credentialClient.getAccessKeyId(),
accessKeySecret: credentialClient.getAccessKeySecret(),
securityToken: credentialClient.getSecurityToken(),
roleArn: 'acs:ram::xxxx:role/xxxx-xxxx',
roleSessionExpiration: 43200,
});
const client = new KmsClient({
endpoint: uri, // check this from kms console
credential: config
});
logger.info(`alibabaKMS login success`);
return client;
} catch (e) {
logger.error(e);
}
};
```
上来就抛ReferenceError: Config is not defined\n at Object.login (/home/node/filestorage-utils/lib/alibabaKMS.js:20:31)\
你这个是因为config没定义,我发你个完整的 不过是基于TS生成的
```"use strict";
var createBinding = (this && this.createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var setModuleDefault = (this && this.setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var importStar = (this && this.importStar) || function (mod) {
if (mod && mod.esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) createBinding(result, mod, k);
setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "esModule", { value: true });
const credentials_1 = __importStar(require("@alicloud/credentials"));
async function main() {
// 使用RamRoleArn初始化Credentials Client。
const credentialsConfig = new Config({
// 凭证类型。
type: 'ecs_ram_role',
// 选填,该ECS角色的角色名称,不填会自动获取,但是建议加上以减少请求次数
roleName: 'roleName',
})
let credentialClient = new Credential(credentialsConfig);
const config = new credentials_1.Config({
// 凭证类型。
type: 'ram_role_arn',
accessKeyId: credentialClient.getAccessKeyId(),
accessKeySecret: credentialClient.getAccessKeySecret(),
securityToken: credentialClient.getSecurityToken(),
// 要扮演的RAM角色ARN,示例值:acs:ram::123456789012:role/adminrole
roleArn: 'acs:ram::123456789012:role/adminrole',
roleSessionExpiration: 3600,
});
let client = new credentials_1.default(config);
console.log(await client.getAccessKeyId());
}
main().catch(console.error);
```import Credential, {Config} from '@alicloud/credentials';
async function main() {
// 使用RamRoleArn初始化Credentials Client。
const credentialsConfig = new Config({
// 凭证类型。
type: 'ecs_ram_role',
// 选填,该ECS角色的角色名称,不填会自动获取,但是建议加上以减少请求次数
roleName: 'roleName',
})
let credentialClient = new Credential(credentialsConfig);
const config = new Config({
// 凭证类型。
type: 'ram_role_arn',
// 设置为AccessKey ID值。
accessKeyId: credentialClient.getAccessKeyId(),
// 设置为AccessKey Secret值。
accessKeySecret: credentialClient.getAccessKeySecret(),
// 设置为SecurityToken值。
securityToken: credentialClient.getSecurityToken(),
// 要扮演的RAM角色ARN,示例值:acs:ram::123456789012****:role/adminrole
roleArn: 'acs:ram::123456789012****:role/adminrole',
roleSessionExpiration: 3600,
})
let client = new Credential(config);
console.log(client.getAccessKeyId())
}
main().catch(console.error);
--此回答整理自钉群“OpenAPI 开发者门户 - 值班服务群2群”
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。