如何在查看器响应Cloudfront事件中更改状态代码?

Ali*_*kin 6 amazon-cloudfront aws-lambda-edge

我想使用S3对象元数据和DB(或某些远程服务)中的一些角色数据来保护Cloudfront响应,这是当前用户的特定.我想我应该viewer-response在这里使用事件,以便一起访问S3数据和用户数据.我尝试设置statusstatusDescriptionresponse对象中,但它不适用于viewer-response事件,适用于所有其他事件.设置标题仍然有效.

exports.handler = async (event) => {

  const response = event.Records[0].cf.response;
  const request = event.Records[0].cf.request;
  const isUserAllowed = await allowedByTokenAndDb(request);
  const isS3ObjectAllowed = response.headers['x-amz-meta-isSecure'][0].value === 'true';

  if (!isUserAllowed || !isS3ObjectAllowed) {
    response.status = '403'; // does not work
    response.statusDescription = 'Nothing';
  }

  response.headers['X-Powered-By'] = [{  // works, header will be added
    key: 'X-Powered-By',
    value: 'lol',
  }] 

  return response;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法让viewer-response返回另一个状态?AWS文档并未说明它是否可行.也许有另一种解决方案?

jDu*_*ton 0

根据文档,看起来您只能更改viewer-requestorigin-requestorigin-response事件处理程序中的响应。此页面没有明确声明您不能更改viewer-response事件处理程序中的响应,但它确实暗示了这一点,因为它只讨论了受支持的其他三个: https: //docs.aws.amazon.com/AmazonCloudFront/latest /DeveloperGuide/lambda-updating-http-responses.html

和你一样,我也无法让它发挥作用,但我最终还是用它origin-response来满足我的需求。