S3静态网站托管:是否可以在重定向规则中向目标URL添加多个查询字符串参数? [英] S3 Static website hosting: Is it possible to add multiple query string parameters to the target URL in a redirect rule?

查看:106
本文介绍了S3静态网站托管:是否可以在重定向规则中向目标URL添加多个查询字符串参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在S3上托管一个静态网站,该网站正在使用重定向规则将请求重新路由到lambda函数.

I am currently hosting a static website on S3 which is using redirection rules to re-route the request to a lambda function.

问题是我有两个存储桶都充当静态网站(沙盒与生产),我需要能够将它们指向相同的lambda函数,但必须能够区分它们当函数运行时.无论是标题,GET参数,字符串操作,还是我可以用来确定请求来自哪个存储桶的任何内容.

The problem is I have two buckets that are both acting as static websites (sandbox vs production) and I need to be able to point them to the same lambda function but be able to tell them apart when the function is run. Whether it's a header, a GET parameter, string manipulation, anything I can use to determine which bucket the request came from.

任何人都不知道在lambda函数中如何识别请求来自另一个存储桶吗?这是我目前使用的重定向规则:

Does anyknow know how inside the lambda function I can identify that the request came from one bucket over the other? This is the current redirect rules I am using:

<RoutingRules>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals/>
     <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
    </Condition>
    <Redirect>
      <Protocol>https</Protocol>
      <HostName>removed.execute-api.us-east-2.amazonaws.com</HostName>
      <ReplaceKeyPrefixWith>prod/func?key=</ReplaceKeyPrefixWith>
      <HttpRedirectCode>307</HttpRedirectCode>
    </Redirect>
  </RoutingRule>
</RoutingRules>

据我了解,我无法设置此路由规则集中的标头或GET参数,因为对规则的验证失败,而我目前唯一的选择就是复制lambda函数,我真的不想这么做尽其所能地做.

From what I understand I cannot set and headers or GET parameters from this routing ruleset as the validation for the rules fails and my only option as it stands is to duplicate the lambda function, which I really don't want to do as it feels verbose.

我也已经在event& Lambda函数中的context参数,似乎看不到任何可帮助我唯一标识原始存储桶的项.

I have also done JSON.stringify() on the event & context parameters within the Lambda function and cannot seem to see any item to help me uniquely identify the origin bucket.

对此表示感谢.

推荐答案

如果要添加查询字符串("GET")参数,则必须记住这是XML文档,需要转义&&amp;.通过这种方法,S3仍将在重定向中正确呈现查询字符串.

If you want to add query string ("GET") parameters, you have to remember that this is an XML document, which requires escaping &&amp;. S3 will still render the query string correctly in the redirect with this approach.

无效:

<ReplaceKeyPrefixWith>prod/func?foo=bar&buzz=fizz&key=</ReplaceKeyPrefixWith>

有效:

<ReplaceKeyPrefixWith>prod/func?foo=bar&amp;buzz=fizz&amp;key=</ReplaceKeyPrefixWith>

这篇关于S3静态网站托管:是否可以在重定向规则中向目标URL添加多个查询字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆