开发者社区> 问答> 正文

阿里云OpenAPI 抛ReferenceError怎么处理?

阿里云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)\

展开
收起
你鞋带开了~ 2024-02-06 22:08:20 62 0
1 条回答
写回答
取消 提交回答
  • 你这个是因为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群”

    2024-02-06 22:13:22
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
AutoTalk第七期:自动化工具-OpenAPI在线调试 立即下载
最大化阿里云OpenAPI能力的方法和实践 立即下载
低代码开发师(初级)实战教程 立即下载