简体   繁体   English

如何在 React 应用程序中使用 AWS Amplify 环境变量?

[英]How to use AWS Amplify environment variables in React app?

I have the ReactJS app deployed on AWS Amplify.我在 AWS Amplify 上部署了 ReactJS 应用程序。 Also, the environment variables that I need in js code are in Amplify.另外,我在js代码中需要的环境变量在Amplify中。 How can I use them from my code?我如何在我的代码中使用它们? How do access them?如何访问它们?

Assuming you want to access these in the front end application:假设您想在前端应用程序中访问这些:

During the build process, environment variables can be accessed via ${VARIABLE_NAME}.在构建过程中,可以通过 ${VARIABLE_NAME} 访问环境变量。 You can then set a react environment variable during the build process.然后,您可以在构建过程中设置反应环境变量。 If you are developing your app with a frontend framework that supports its own environment variables, it is important to understand that these are not the same as the environment variables you configure in the Amplify console.如果您使用支持自身环境变量的前端框架开发应用程序,请务必了解这些与您在 Amplify 控制台中配置的环境变量不同。 For example, React (prefixed REACT_APP) and Gatsby (prefixed GATSBY), enable you to create runtime environment variables that those frameworks automatically bundle into your frontend production build.例如,React(前缀为 REACT_APP)和 Gatsby(前缀为 GATSBY)使您能够创建运行时环境变量,这些框架会自动将其捆绑到您的前端生产构建中。 To understand the effects of using these environment variables to store values, refer to the documentation for the frontend framework you are using.要了解使用这些环境变量存储值的效果,请参阅您正在使用的前端框架的文档。

Assuming you want to access these in the back end application:假设您想在后端应用程序中访问这些:

You can access environment variables in React through process.env.VARIABLE_NAME and pass these through to the backend.您可以通过process.env.VARIABLE_NAME访问 React 中的环境变量,并将它们传递给后端。

Amazon provides a comprehensive overview of how to store and access Amplify environment variables: Amazon 全面概述了如何存储和访问 Amplify 环境变量:

https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM