问题描述:使用 AWS CDN 和 Lambda@Edge 服务时,出现了一种情况,即进行请求处理的 Lambda 函数中返回的内容类型(或 MIME 类型)不正确。
考虑在返回缓存文件时指定正确的 MIME 类型。
代码示例:
'use strict';
exports.handler = (event, context, callback) => {
// 请求响应
const response = event.Records[0].cf.response;
const headers = response.headers;
const contentType = headers['content-type'];
// 更新 MIME 类型
headers['content-type'] = [{key: 'Content-Type', value: 'text/html'}];
callback(null, response);
};
在示例代码中,我们获取到了响应(response)和响应头部信息(headers),然后通过检查 content-type 属性获取到了 MIME 类型(contentType)。最后,我们更新了合适的 MIME 类型,并将它设置到响应头部中。最后,我们将更新后的响应对象(response)返回。
这样,我们就可以很容易地解决 AWS CDN + Lambda@Edge MimeType 问题了。