无法从 @aws-sdk/credential-providers 导出 fromIni

bei*_*les 1 amazon-web-services aws-sdk-js aws-credentials

我正在开发 React/Node.js 应用程序,并尝试从 ~/.aws/credentials 文件读取我的 IAM 用户凭证。我正在尝试使用 @aws-sdk/credential-providers 节点包中的 fromIni 。根据AWS SDK v3文档,我可以执行以下操作:

\n
import { fromIni } from "@aws-sdk/credential-providers"; // ES6 import\n// const { fromIni } = require("@aws-sdk/credential-providers"); // CommonJS import\n\nconst client = new FooClient({\n  credentials: fromIni({\n    // Optional. The configuration profile to use. If not specified, the provider will use the value\n    // in the `AWS_PROFILE` environment variable or a default of `default`.\n    profile: "profile",\n    // Optional. The path to the shared credentials file. If not specified, the provider will use\n    // the value in the `AWS_SHARED_CREDENTIALS_FILE` environment variable or a default of\n    // `~/.aws/credentials`.\n    filepath: "~/.aws/credentials",\n    // Optional. The path to the shared config file. If not specified, the provider will use the\n    // value in the `AWS_CONFIG_FILE` environment variable or a default of `~/.aws/config`.\n    configFilepath: "~/.aws/config",\n    // Optional. A function that returns a a promise fulfilled with an MFA token code for the\n    // provided MFA Serial code. If a profile requires an MFA code and `mfaCodeProvider` is not a\n    // valid function, the credential provider promise will be rejected.\n    mfaCodeProvider: async (mfaSerial) => {\n      return "token";\n    },\n    // Optional. Custom STS client configurations overriding the default ones.\n    clientConfig: { region },\n  }),\n});\n
Run Code Online (Sandbox Code Playgroud)\n

但是当我在我的 index.js 文件中尝试这个时:

\n
import { fromIni } from \'@aws-sdk/credential-providers\';\n\nconst createLink = {\n  url: config.aws_appsync_graphqlEndpoint,\n  region: config.aws_appsync_region,\n  auth: {\n    type: config.aws_appsync_authenticationType,\n    credentials: fromIni()\n  }\n};\n
Run Code Online (Sandbox Code Playgroud)\n

然后运行npm start,出现以下错误:

\n
export \'fromIni\' (imported as \'fromIni\') was not found in \'@aws-sdk/credential-providers\' (possible exports: fromCognitoIdentity, fromCognitoIdentityPool, fromTemporaryCredentials, fromWebToken)\n
Run Code Online (Sandbox Code Playgroud)\n

看来我想要的功能没有从包中导出,但文档另有说明。

\n

编辑:\n输出到@aws-sdk/credential-providers @aws-sdk/credential-provider-ini

\n
port-dashboard@0.1.0 C:\\Users\\kshang\\Documents\\pov-ui       \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\xac @aws-sdk/client-cognito-identity-provider@3.79.0        \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\xac @aws-sdk/client-sts@3.79.0\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/credential-provider-node@3.79.0\n\xe2\x94\x82 \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 @aws-sdk/credential-provider-ini@3.79.0\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/credential-provider-node@3.79.0\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 @aws-sdk/credential-provider-ini@3.79.0\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\xac @aws-sdk/credential-providers@3.79.0\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\xac @aws-sdk/client-cognito-identity@3.79.0\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/credential-provider-node@3.79.0\n\xe2\x94\x82 \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 @aws-sdk/credential-provider-ini@3.79.0 deduped   \n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 @aws-sdk/credential-provider-ini@3.79.0\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\xac aws-amplify@4.3.20\n  \xe2\x94\x9c\xe2\x94\x80\xe2\x94\xac @aws-amplify/analytics@5.2.5\n  \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/client-firehose@3.6.1\n  \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/credential-provider-node@3.6.1\n  \xe2\x94\x82     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 @aws-sdk/credential-provider-ini@3.6.1\n  \xe2\x94\x82     \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/credential-provider-process@3.6.1      \n  \xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 @aws-sdk/credential-provider-ini@3.6.1 deduped\n  \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-amplify/geo@1.3.1\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/client-location@3.48.0\n      \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/credential-provider-node@3.48.0\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 @aws-sdk/credential-provider-ini@3.48.0\n
Run Code Online (Sandbox Code Playgroud)\n

Mig*_*ado 5

更新:经过更多研究并与一些 AWS 专家交谈后,我们发现我们需要使用 Amazon Cognito 才能获取基于浏览器的应用程序的凭证。

\n
\n

我尝试使用CloudFrontClient进行fromIni()简单操作时遇到相同的错误。CreateInvalidationCommand

\n

credentials尝试在没有密钥的情况下配置客户端会导致Error: Credential is missing

\n

我当前的本地开发解决方法是使用 .env.local 文件,并使用我的配置的accessKeyIdSecretAccessKey属性:

\n
const cloudfront = new CloudFrontClient({\n  credentials: {\n    accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY_ID,\n    secretAccessKey: process.env.REACT_APP_SECRET_ACCESS_KEY,\n  },\n  region: "us-east-1",\n});\n
Run Code Online (Sandbox Code Playgroud)\n

这显然并不理想,但它可以在本地进行测试和开发。

\n

我还想指出,导出中也缺少fromEnv() 。这会导致 EC2 实例中的此解决方法出现问题。

\n

不管怎样,希望很快就能在更新中看到这个问题的解决。

\n
cache-invalidator@0.1.0 /Users/miguelmaldonado/Desktop/projects/prototypes/cache-invalidator\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\xac @aws-sdk/client-cloudfront@3.85.0\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/credential-provider-node@3.85.0\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 @aws-sdk/credential-provider-ini@3.85.0 deduped\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\xac @aws-sdk/credential-providers@3.85.0\n  \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 @aws-sdk/credential-provider-ini@3.85.0\n
Run Code Online (Sandbox Code Playgroud)\n