仅当您启用并使用存储桶的网站托管功能时,S3 才会提供自动索引文档,方法是指向存储桶的网站托管端点 ${bucket}.s3-website.${region}.amazonaws.com 而不是存储桶的通用 REST 端点 @987654323 @。
网站端点和 REST 端点都有numerous differences,包括这个。
您看到这些以 / 结尾的对象键的 0 字节文件的原因是您正在使用 S3 控制台或其他实际创建 0 字节对象的实用程序在存储桶中创建文件夹对象。一旦文件夹中“包含”对象,就不需要它们——但它们是在 S3 控制台中显示空文件夹的唯一方法,它将名为 foo/ 的对象显示为名为 foo 的文件夹,即使没有其他具有foo/ 键前缀的对象。它是控制台中文件夹层次结构的可视化模拟的一部分,即使 S3 中的对象从未真正“在”文件夹中。
如果由于某种原因您需要使用 REST 端点(例如您不想公开存储桶),那么您需要 CloudFront 中的两个 Lambda@Edge 触发器,以相当接近地模拟此功能。
Origin Request 触发器可以在检查 CloudFront 缓存之后、在请求发送到源之前检查和修改请求。我们使用它来检查以/ 结尾的路径,如果发现则追加index.html。
Origin Response 触发器可以在将响应写入 CloudFront 缓存之前检查并可能修改响应。源响应触发器还可以检查生成响应的请求之前的原始请求。我们使用它来检查响应是否是错误的。如果是,并且原始请求确实 not 似乎是针对索引文档或文件(具体来说,在路径中的最后一个斜杠之后,“文件”至少有一个字符,后跟一个点,后跟至少一个字符——如果是这样,那可能是一个“文件”)。如果两者都不是,我们将重定向到原始路径加上我们附加的最终/。
原始请求和原始响应触发器仅在缓存未命中时触发。当缓存命中时,两个触发器都不会触发,因为它们位于 CloudFront 的源端 - 缓存的背面。可以从缓存中提供服务的请求从缓存中提供,因此不会调用触发器。
以下是用 Node.js 8.10 编写的 Lambda@Edge 函数。这个 Lambda 函数会根据上下文修改其行为,使其表现为源请求或源响应。在 Lambda 中发布版本后,将该版本的 ARN 与 CloudFront 缓存行为设置关联为源请求和源响应触发器。
'use strict';
// combination origin-request, origin-response trigger to emulate the S3
// website hosting index document functionality, while using the REST
// endpoint for the bucket
// https://stackoverflow.com/a/54263794/1695906
const INDEX_DOCUMENT = 'index.html'; // do not prepend a slash to this value
const HTTP_REDIRECT_CODE = '302'; // or use 301 or another code if desired
const HTTP_REDIRECT_MESSAGE = 'Found';
exports.handler = (event, context, callback) => {
const cf = event.Records[0].cf;
if(cf.config.eventType === 'origin-request')
{
// if path ends with '/' then append INDEX_DOCUMENT before sending to S3
if(cf.request.uri.endsWith('/'))
{
cf.request.uri = cf.request.uri + INDEX_DOCUMENT;
}
// return control to CloudFront, to send request to S3, whether or not
// we modified it; if we did, the modified URI will be requested.
return callback(null, cf.request);
}
else if(cf.config.eventType === 'origin-response')
{
// is the response 403 or 404? If not, we will return it unchanged.
if(cf.response.status.match(/^40[34]$/))
{
// it's an error.
// we're handling a response, but Lambda@Edge can still see the attributes of the request that generated this response; so, we
// check whether this is a page that should be redirected with a trailing slash appended. If it doesn't look like an index
// document request, already, and it doesn't end in a slash, and doesn't look like a filename with an extension... we'll try that.
// This is essentially what the S3 web site endpoint does if you hit a nonexistent key, so that the browser requests
// the index with the correct relative path, except that S3 checks whether it will actually work. We are using heuristics,
// rather than checking the bucket, but checking is an alternative.
if(!cf.request.uri.endsWith('/' + INDEX_DOCUMENT) && // not a failed request for an index document
!cf.request.uri.endsWith('/') && // unlikely, unless this code is modified to pass other things through on the request side
!cf.request.uri.match(/[^\/]+\.[^\/]+$/)) // doesn't look like a filename with an extension
{
// add the original error to the response headers, for reference/troubleshooting
cf.response.headers['x-redirect-reason'] = [{ key: 'X-Redirect-Reason', value: cf.response.status + ' ' + cf.response.statusDescription }];
// set the redirect code
cf.response.status = HTTP_REDIRECT_CODE;
cf.response.statusDescription = HTTP_REDIRECT_MESSAGE;
// set the Location header with the modified URI
// just append the '/', not the "index.html" -- the next request will trigger
// this function again, and it will be added without appearing in the
// browser's address bar.
cf.response.headers['location'] = [{ key: 'Location', value: cf.request.uri + '/' }];
// not strictly necessary, since browsers don't display it, but remove the response body with the S3 error XML in it
cf.response.body = '';
}
}
// return control to CloudFront, with either the original response, or
// the modified response, if we modified it.
return callback(null, cf.response);
}
else // this is not intended as a viewer-side trigger. Throw an exception, visible only in the Lambda CloudWatch logs and a 502 to the browser.
{
return callback(`Lambda function is incorrectly configured; triggered on '${cf.config.eventType}' but expected 'origin-request' or 'origin-response'`);
}
};