amazon-web-services - 使用 Amplify 集成构建 AWS Amplify React 应用程序时总是缺少 aws-exports.js
问题描述
我的 React 应用程序使用 GraphQL API、存储、身份验证、函数、托管——所有有趣的东西——所以我必须有一个aws-exports.js
可用的文件。使用 Amplify Backend 资源放大 React 前端。
回购基本上设置为:
package.json
src/
- aws-exports.js
- app.js
- ...etc
并ls
在每个目录中执行一个显示没有aws-exports.js
生成文件的构建。
使用许多不同的配置,我遇到了:
[INFO]: # Executing command: yarn run build
[INFO]: yarn run v1.16.0
[INFO]: $ react-scripts build
[INFO]: Creating an optimized production build...
[INFO]: Failed to compile.
[INFO]: ./src/App.js
Cannot find file './aws-exports' in './src'.
2020-04-30T00:52:34.883Z [WARNING]: error Command failed with exit code 1.
当我签入amplify.yml
并.yml
在 Web 控制台中配置时就是这样。
我已经尝试过amplify push;
,但正如预期的那样遇到了
An error occured during the push operation: Current environment cannot be determined
Use 'amplify init' in the root of your app directory to initialize your project with Amplify
也在尝试:amplify pull;
或Executing command: amplify pull --appId abc123abc123 --envName dev
# Starting phase: preBuild
# Executing command: amplify pull
For more information on AWS Profiles, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html
? Do you want to use an AWS profile? (Y/n)
.[43D.[43C
它只是挂起并期待输入。我不认为像这样手动输入信用是解决这个问题的方法。
aws-exports.js
考虑到所有后端集成,似乎 amplify 应该自己处理这一代。ls
不同时。有很多关于这方面的问题都是最新的,但没有真正的答案。谢谢你的时间
解决方案
我的解决方案是在“npm run build”步骤之前通过脚本简单地生成 aws-exports.js。
您只需将 aws-exports.js 内容存储在名为“secretfile”的环境变量中,然后像这样在 amplify.yml 中使用它
version: 1
frontend:
phases:
preBuild:
commands:
- npm ci
build:
commands:
- echo $secretfile > ./src/aws-exports.js
- npm run build
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
原因:
- 将 aws-exports.js 提交到 repo 当然是一个很大的问题,因为它包含 API 密钥和其他机密。
- 我也不想每次都启动后端构建。构建后端会适得其反,因为它会为每个构建创建一个新的后端堆栈,这会花费更多的钱,进一步减慢速度,而且容易出错。
谢谢。
推荐阅读
- ios - 为什么我没有在 xcode 中使用苹果登录
- python - 无法使用 update_idletasks() 使用 Python Tkinter 可视化排序算法
- laravel - 使用更多选项修改 Oracle DB Enum 列 - Laravel 迁移
- android - 在 MainActivity 启动后调用应用内消息的特定活动,而不是在 SplashActivity 中
- ignite - 使用ignite,但是配置一样,2.6可以使用cache rebrance,但是2.8版本总是提示rebrance skip
- angular - 从生成的文件夹中查看时不显示代码覆盖率,但在终端中显示
- android - 将 Native App LTR 反应到 RTL 更改,而无需重新启动应用程序
- javascript - 从反应js中的表单获取数据
- html - 创建菜单栏
- apache-spark - pyspark 中的 dataframe.write.csv 非常慢