【发布时间】:2020-01-05 23:37:25
【问题描述】:
根据 question 和 one 以下代码,我可以将 S3 存储桶中的子文件夹指向我的域。
但是,在未找到子域的情况下,我会收到以下错误消息:
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>2CE9B7837081C817</RequestId>
<HostId>
T3p7mzSYztPhXetUu7GHPiCFN6l6mllZgry+qJWYs+GFOKMjScMmRNUpBQdeqtDcPMN3qSYU/Fk=
</HostId>
</Error>
我不希望它显示此错误消息,相反,在这种情况下,我希望从另一个 S3 存储桶子域(即example-bucket.s3-website.us-east-2.amazonaws.com/error)提供服务,例如,用户将收到一条奇特的错误消息。因此,在找不到 S3 存储桶子文件夹的情况下,它应该退回到那里。我如何通过更改下面的节点函数来实现这一点。
'use strict';
// if the end of incoming Host header matches this string,
// strip this part and prepend the remaining characters onto the request path,
// along with a new leading slash (otherwise, the request will be handled
// with an unmodified path, at the root of the bucket)
const remove_suffix = '.example.com';
// provide the correct origin hostname here so that we send the correct
// Host header to the S3 website endpoint
const origin_hostname = 'example-bucket.s3-website.us-east-2.amazonaws.com'; // see comments, below
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const host_header = headers.host[0].value;
if(host_header.endsWith(remove_suffix))
{
// prepend '/' + the subdomain onto the existing request path ("uri")
request.uri = '/' + host_header.substring(0,host_header.length - remove_suffix.length) + request.uri;
}
// fix the host header so that S3 understands the request
headers.host[0].value = origin_hostname;
// return control to CloudFront with the modified request
return callback(null,request);
};
【问题讨论】:
标签: amazon-s3