TypeError: Cannot read property 'AwsCredentialsProvider' of undefined

TypeError: Cannot read property 'AwsCredentialsProvider' of undefined

一直在尝试测试 aws-iot-device-sdk-v2 库。我目前正在尝试测试 AWS 开发团队提供的 sample app。我正在尝试逐步测试系统。这是我到目前为止测试过的代码:

import { mqtt, auth, http, io, iot } from 'aws-iot-device-sdk-v2';

const client_bootstrap = new io.ClientBootstrap();
        
let config_builder = iot.AwsIotMqttConnectionConfigBuilder.new_with_websockets({
    region: 'us-west-2',
    credentials_provider: auth.AwsCredentialsProvider.newDefault(client_bootstrap)
});

config_builder.with_clean_session(false);
config_builder.with_endpoint('example.com');
config_builder.with_client_id(1);
        
const config = config_builder.build();
const client = new mqtt.MqttClient(client_bootstrap);
const connection = client.new_connection(config);

await connection.connect();

当 运行 在 AWS 控制台上执行此操作时,出现以下错误:

TypeError: Cannot read property 'AwsCredentialsProvider' of undefined

知道我做错了什么吗?

无法确定我无法按预期使用 AwsCredentialsProvider 的原因,但找到了解决方法。相反,我能够使用 const config_builder = iot.AwsIotMqttConnectionConfigBuilder.new_with_websockets(); 初始化构建器。无论如何,没有弄清楚为什么我不能按预期使用 AwsCredentialsProvider 。如果开发团队有时间,可能需要研究一下。

我在浏览器中运行使用这个例子时也遇到了同样的问题,并找到了原因:

'aws-iot-device-sdk-v2' 直接从 aws-crt

导入这 5 个 类

https://github.com/aws/aws-iot-device-sdk-js-v2/blob/main/lib/index.ts

在 aws-crt 中,浏览器的实现在子文件夹中, https://github.com/awslabs/aws-crt-nodejs/tree/main/lib/browserm,不包括 'auth' .

因此,如果您 运行 在浏览器中使用这些示例,则需要导入 aws-crt 的子文件夹,并跳过 'auth':

import { mqtt, http, io, iot } from 'aws-crt/dist.browser/browser';