简体   繁体   English

将信息注入从 AWS CloudFront 中的 S3 源返回的请求中

[英]Injecting information into requests returned from S3 origins in AWS CloudFront

I have a React SPA that I would like to deploy for different customers each with their own separate backend API.我有一个 React SPA,我想为不同的客户部署它,每个客户都有自己独立的后端 API。

To that end, I'd like to pass the backend api endpoint into the SPA.为此,我想将后端 api 端点传递给 SPA。

What I'd like to do is, if the user sends a get request https://myapp.mydomain.com , he gets back a document, but the api endpoint has been added to the request in some way I can parse and use in React, eg his URL has been modified to read https://myapp.mydomain.com?api=myapi.mydomain.com我想做的是,如果用户发送 get 请求https://myapp.mydomain.com ,他会返回一个文档,但是 api 端点已以某种我可以解析和使用的方式添加到请求中在 React 中,例如他的 URL 已被修改为https://myapp.mydomain.com?api=myapi.mydomain.com

I'm using AWS CloudFront with an S3 origin, so AWS CloudFront functions look promising, but I've not had much luck so far.我正在使用具有 S3 源的 AWS CloudFront,因此 AWS CloudFront 功能看起来很有希望,但到目前为止我运气不佳。

I'm open to suggestions on how and where to inject the information and how to retrieve it in my React SPA, including entirely different approaches.我愿意接受有关如何以及在何处注入信息以及如何在我的 React SPA 中检索信息的建议,包括完全不同的方法。 The only parts I'm pretty much committed on is CloudFront and using a single S3 bucket to serve all customers.我几乎致力于的唯一部分是 CloudFront 并使用单个 S3 存储桶为所有客户提供服务。

The solution I ended up implementing is based on the runtime-env-cra library.我最终实现的解决方案是基于 runtime-env-cra 库。 That basically loads an additional script from the server containing some javascript code which installs environment variables in the window object.这基本上从服务器加载了一个附加脚本,其中包含一些在窗口对象中安装环境变量的 javascript 代码。

The necessary javascript file is generated by an AWS Lambda function which in turn is dynamically generated for each customer in the CloudFormation template from the input ressources.必要的 javascript 文件由 AWS Lambda 函数生成,该函数又从输入资源为 CloudFormation 模板中的每个客户动态生成。

This unfortunately cannot be done with CloudFront functions, as you have no access to the body of the request there.不幸的是,这无法通过 CloudFront 函数完成,因为您无法访问那里的请求正文。

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

相关问题 AWS Cloudfront S3非常慢 - AWS cloudfront s3 is very slow 将 brotli 与 AWS Cloudfront + S3 一起使用 - Using brotli with AWS Cloudfront + S3 AWS Cloudfront 以 AWS S3 作为来源给出 CORS 错误 - AWS Cloudfront gives a CORS error with AWS S3 as origin AWS CloudFront如何更新现有的S3文件? - AWS CloudFront how to update existing S3 file? 当从浏览器输入 url 时,AWS 托管(s3,cloudfront)static 反应网站不会路由到子页面 - AWS hosted (s3, cloudfront) static react website does not route to sub page when the url entered from browser 私有 S3 + CloudFront 反应应用程序:“XML 文件似乎没有任何与之关联的样式信息” - Private S3 + CloudFront react app: “XML file does not appear to have any style information associated with it” 使用 AWS S3 + CloudFront 部署时反应 SPA 空白页 - React SPA blank page when deployed using AWS S3 + CloudFront 使用 aws s3 和 cloudfront 托管 React 应用程序。任何人都可以建议配置以访问动态 url 吗? - Using aws s3 and cloudfront to host React application.Can anyone suggest configuration to access dynamic urls? 在 AWS S3 和 CloudFront 中托管 React 路由器应用程序时出现 403 Access Denied 错误 - Getting 403 Access Denied Errors When Hosting a React Router App in AWS S3 and CloudFront Django和AWS S3:静态文件请求到错误的URL - Django and AWS S3: Static File Requests to Wrong URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM