使用配置文件以编程方式设置 AWS 区域

Programatically set AWS region with a profile

我在 .aws/credentials.aws/config

中定义了多个配置文件
[profile dev]
region = us-east-1

[profile staging]
region = eu-west-1

使用 NodeJS AWS SDKv3 我可以从本地配置加载凭证:

const {S3Client} = require('@aws-sdk/client-s3');
const {fromIni} = require('@aws-sdk/credential-providers');

new S3Client({
    credentials: fromIni({
        profile: 'dev'
    })
});

如何对区域执行相同的操作?(类似于以下内容)

const {S3Client} = require('@aws-sdk/client-s3');
const {fromIni} = require('@aws-sdk/credential-providers');
const {fromIni: regionFromIni} = require('@aws-sdk/region-providers');

new S3Client({
    credentials: fromIni({
        profile: 'dev'
    }),
    region: regionFromIni({
        profile: 'dev'
    })
});

@fedonev 向我指出了一个有效的解决方案:

const {loadSharedConfigFiles} = require('@aws-sdk/shared-ini-file-loader');

const sharedConfig = await loadSharedConfigFiles();
console.log(sharedConfig.configFile.dev?.region);