未找到 AWS API 网关

Posted

技术标签:

【中文标题】未找到 AWS API 网关【英文标题】:AWS API Gateway not found 【发布时间】:2021-05-05 19:45:01 【问题描述】:

我是 AWS API 网关和 Lambda 的新手。我正在尝试获得基本的“来自 Lambda 的你好!”功能正常工作。

我可以成功登录如下:

const user = await Auth.signIn(email, password);

我得到了一个正确的令牌,因为我在 AWS API 控制台上对其进行了测试并得到了响应

//token works as tested on AWS Console (API Tester)
        const token = user2.signInUserSession.idToken.jwtToken

我假设请求信息是正确的,因为令牌是正确的:

  const requestInfo = 
    headers: 
      Authorization: token
    
   

我认为错误就在这里,因为每当我尝试对“数据”做任何事情时,我都没有得到回应

 const data = await API.get('API_NAME','/hello',requestInfo)
 console.log('This is the Data: ', data  )

我在 try/catch Xcode 中遇到的错误是:

'API API_NAME does not exist'

有什么建议吗?

我在运行 Amplify Push 时遇到错误:

Packaging lambda failed function failed with the error 
Command failed with exit code 1: npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: APP_NAME@1.0.1
npm ERR! Found: react@16.8.6
npm ERR! node_modules/react
npm ERR!   react@"16.8.6" from the root project
npm ERR!   peer react@"^16.7.0" from @aws-amplify/ui-react@0.2.36
npm ERR!   node_modules/@aws-amplify/ui-react
npm ERR!     @aws-amplify/ui-react@"^0.2.35" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.14.0" from react-dom@16.14.0
npm ERR! node_modules/react-dom
npm ERR!   peer react-dom@"^16.7.0" from @aws-amplify/ui-react@0.2.36
npm ERR!   node_modules/@aws-amplify/ui-react
npm ERR!     @aws-amplify/ui-react@"^0.2.35" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /Users/name/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/name/.npm/_logs/2021-02-02T09_36_01_091Z-debug.log
An error occurred during the push operation: Packaging lambda failed function failed with the error 
Command failed with exit code 1: npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: APP_NAME@1.0.1
npm ERR! Found: react@16.8.6
npm ERR! node_modules/react
npm ERR!   react@"16.8.6" from the root project
npm ERR!   peer react@"^16.7.0" from @aws-amplify/ui-react@0.2.36
npm ERR!   node_modules/@aws-amplify/ui-react
npm ERR!     @aws-amplify/ui-react@"^0.2.35" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.14.0" from react-dom@16.14.0
npm ERR! node_modules/react-dom
npm ERR!   peer react-dom@"^16.7.0" from @aws-amplify/ui-react@0.2.36
npm ERR!   node_modules/@aws-amplify/ui-react
npm ERR!     @aws-amplify/ui-react@"^0.2.35" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /Users/name/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/name/.npm/_logs/2021-02-02T09_36_01_091Z-debug.log

【问题讨论】:

【参考方案1】:

由于您可以成功登录,我想您已经配置了像 this 这样指定 AWS poolId、clientId 等的 Amplify。

同样,您也必须为 API 做一些事情,比如记录在案的 here

这是格式,假设 API 名称是 account

Amplify.configure(
  Auth:  ... ,

  API: 
    endpoints: [
      
        name: 'account', // name of API like 'account', no '/'
        endpoint: BASE_URL, // like 'https://api_name.com', don't put '/' at the end
        region: 'us-east-1', // your region like 'us-east-1', 'ap-southeast-1' etc.
      ,
      ... // Other APIs
    ]
  
);

那么当你调用 API 时,这就是格式

const accountRes: AccountData = await API.get('account', '/account', requestInfo);

注意:- 如果 API 名称相同但请求类型不同(GET、POST、PUT 等),您不需要执行多个条目,您可以简单地为请求调用相应的方法API.get()、@987654329 @、API.put()

【讨论】:

您好!谢谢你的帮助,但不幸的是我正在做这一切,但它仍然不起作用。我尝试运行放大推送,但在更新问题时出现新错误 在 react-native 中有时会出现此错误,不知道修复它的确切步骤。尝试 this 或 this 或删除 node_modules 然后 npm install 不得不重新启动项目。头坏了!你介意看看我的新问题吗:***.com/questions/66014223/…

以上是关于未找到 AWS API 网关的主要内容,如果未能解决你的问题,请参考以下文章

AWS API网关指向Elastic:访问密钥PHP

AWS:使用 cloudformation 模板将 WAF 附加到 api 网关

使用 AWS CDK 为 AWS API 网关启用 CORS

AWS SOC 2 API 网关 / CloudFront

AWS API 网关和 EC2 服务代理

配置 aws lambda 和 api 网关以返回 image/avif 内容类型的二进制数据