{"id":490,"date":"2018-10-06T08:44:47","date_gmt":"2018-10-05T23:44:47","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/10\/06\/s3s3staticweb_cf_basic_auth"},"modified":"2018-10-06T08:44:47","modified_gmt":"2018-10-05T23:44:47","slug":"s3s3staticweb_cf_basic_auth","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/10\/06\/s3s3staticweb_cf_basic_auth\/","title":{"rendered":"S3\u9759\u7684\u30a6\u30a7\u30d6\u30db\u30b9\u30c6\u30a3\u30f3\u30b0+CloudFront+Lambda@Edge\u3067Basic\u8a8d\u8a3c\u3092\u304b\u3051\u308b"},"content":{"rendered":"

\u5148\u65e5\u3001Yahoo!\u30b8\u30aa\u30b7\u30c6\u30a3\u30fc\u30ba\u304c\u30012019\u5e743\u670831\u65e5\u3067\u30b5\u30fc\u30d3\u30b9\u7d42\u4e86\u3059\u308b\u3068\u306e\u3053\u3068\u304c\u516c\u5f0f\u30b5\u30a4\u30c8\u3067\u767a\u8868\u3055\u308c\u305f\u3002<\/p>\n

\u30b5\u30fc\u30d3\u30b9\u7d42\u4e86\u306e\u304a\u77e5\u3089\u305b - Yahoo!\u30b8\u30aa\u30b7\u30c6\u30a3\u30fc\u30ba<\/a> <\/p>\n

\u3053\u306e\u30cb\u30e5\u30fc\u30b9\u306b\u5bfe\u3057\u3066\u3001\u30cd\u30c3\u30c8\u3067\u306f\u3001<\/p>\n

\u300c\u30b8\u30aa\u30b7\u30c6\u30a3\u30fc\u30ba\u304c\u7d42\u308f\u308b\u3068\u5927\u91cf\u306e\u9ed2\u6b74\u53f2\u304c\u6d88\u3048\u308b\u300d
\u300c\u305f\u304f\u3055\u3093\u306e\u9ed2\u6b74\u53f2\u304c\u95c7\u306b\u846c\u3089\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u305d\u3046\u300d
\u300c35\u6b73\u5468\u8fba\u306e\u4eba\u3005\u3001\u9ed2\u6b74\u53f2\u304c\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u304b\u3089\u6d88\u6ec5\u3059\u308b\u306e\u3067\u30db\u30c3\u3068\u3057\u3066\u308b\u306e\u3067\u306f\u300d <\/p>\n

\u306a\u3069\u306e\u58f0\u304c\u4e0a\u304c\u3063\u3066\u3044\u305f\u3002 <\/p>\n

\u3010\u9ed2\u6b74\u53f2\u3011Yahoo!\u30b8\u30aa\u30b7\u30c6\u30a3\u30fc\u30ba\u304c\u30b5\u30fc\u30d3\u30b9\u3092\u7d42\u4e86 \u300c\u9ed2\u6b74\u53f2\u3057\u304b\u6b8b\u3063\u3066\u306a\u3044\u300d\u300c\u30db\u30c3\u3068\u3057\u3066\u308b\u306e\u3067\u306f\u300d : \u307e\u3068\u3081\u30c0\u30cd\uff01<\/a><\/p>\n

\u305d\u3093\u306a\u72b6\u6cc1\u3092\u898b\u3066\u3001<\/p>\n

21\u5e74\u3082\u7d9a\u3044\u305f\u30b5\u30fc\u30d3\u30b9\u304c\u7d42\u4e86\u3057\u3061\u3083\u3046\u306e\u306f\u5bc2\u3057\u3044\u611f\u3058\u304c\u3059\u308b\u306a\u3002\u3075\u30fc\u3093\u3002\u9ed2\u6b74\u53f2\u306d\u3047\u2026\u2026\u3002<\/strong><\/p>\n

\u306a\u3093\u3066\u601d\u3063\u3066\u898b\u3066\u3044\u305f\u306e\u3060\u304c\u2026\u2026\u3002<\/p>\n

\u3042\u30fc\u30fc\u30fc\u3063\uff01\uff01\u305d\u3046\u3044\u3048\u3070\u3001\u5927\u5b66\u751f\u306e\u9803\u3001\u30b8\u30aa\u30b7\u30c6\u30a3\u30fc\u30ba\u4f7f\u3063\u3066\u305f\u308f\uff01\u2026\u2026\u307e\u3060\u6b8b\u3063\u3066\u305f\u308a\u3059\u308b\u3093\u3060\u308d\u3046\u304b\u2026\u2026<\/strong><\/p>\n

\u3068\u601d\u3044\u51fa\u3057\u3066\u3057\u307e\u3063\u305f\u3002
\u305d\u308c\u3067\u3001\u3061\u3087\u3063\u3068\u898b\u306b\u884c\u3063\u305f\u3089\u3002\u306a\u3093\u3068\u6b8b\u3063\u3066\u3044\u305f\u306e\u3060\u3002
\u5f53\u6642\u306e\u307e\u307e\u3002\u3042\u308b\u610f\u5473\u3001\u3053\u308c\u306f\u300c\u9ed2\u6b74\u53f2\u300d\u3068\u8a00\u3048\u308b\u306e\u304b\u3082\u3057\u308c\u306a\u3044\u3002 <\/p>\n

\u3053\u308c\u3092\u793e\u5185\u306eLT\u3001\u901a\u79f0MLT<\/strong>(M<\/strong>MM L<\/strong>ightning T<\/strong>alk)\u3067\u8a71\u3092\u3057\u305f\u3089\u3001\u601d\u3044\u306e\u5916\u30c1\u30fc\u30e0\u30e1\u30f3\u30d0\u30fc\u306b\u53d7\u3051\u3066\u3057\u307e\u3044\u3001<\/p>\n

\u300c\u4e16\u754c\u907a\u7523\u3068\u3057\u3066\u4fdd\u8b77\u3059\u3079\u304d\u30b3\u30f3\u30c6\u30f3\u30c4\u300d
\u300cS3\u306b\u79fb\u884c\u3059\u308b\u3079\u304d\u300d <\/p>\n

\u3060\u3068\u304b\u8a00\u308f\u308c\u305f\u308a\u3001\u3053\u3093\u306a Slack \u306e\u30ab\u30b9\u30bf\u30e0\u30ea\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u4f5c\u3089\u308c\u3066\u3057\u307e\u3063\u305f\u3002
(\u30b5\u30a4\u30c8\u540d\u306b\u300c\u898b\u805e\u9332\u300d\u304c\u5165\u3063\u3066\u3044\u305f\u305f\u3081)<\/p>\n

\"\"<\/p>\n

\u61d0\u304b\u3057\u3044\u601d\u3044\u3082\u3042\u308b\u306e\u3067\u3001\u4fdd\u5b58\u3057\u3066\u304a\u3053\u3046\u3068\u306f\u601d\u3046\u304c\u3001\u300c\u9ed2\u6b74\u53f2\u300d\u306a\u306e\u3067\u3001\u30a2\u30af\u30bb\u30b9\u5236\u9650\u3092\u3057\u306a\u304f\u3066\u306f\uff01
\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u524d\u30d5\u30ea\u304c\u9577\u304f\u306a\u3063\u305f\u304c\u3001\u4eca\u56de\u306f\u3001 S3<\/code> \u306e\u9759\u7684\u30a6\u30a7\u30d6\u30db\u30b9\u30c6\u30a3\u30f3\u30b0 + CloudFront<\/code> \u306e\u74b0\u5883\u306b\u3001 Lambda@Edge<\/code> \u3092\u4f7f\u3063\u3066Basic\u8a8d\u8a3c\u3092\u304b\u3051\u308b\u624b\u9806\u3092\u7c21\u5358\u306b\u307e\u3068\u3081\u3066\u3044\u304f\u3002<\/p>\n

S3\u306b\u30d0\u30b1\u30c3\u30c8\u4f5c\u6210\u3001\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<\/h2>\n

\u307e\u305a\u306f\u3001S3\u306b\u30d0\u30b1\u30c3\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002\u305d\u3057\u3066\u3001\u4fdd\u5b58\u3057\u305f\u3044\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u3002
\u4eca\u56de\u306f\u3001\u30b8\u30aa\u30b7\u30c6\u30a3\u30fc\u30ba\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u304d\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u305d\u306e\u307e\u307e\u5168\u90e8\u7a81\u3063\u8fbc\u3093\u3060\u3002 <\/p>\n

\"\"<\/p>\n

\u6b21\u306b\u3001\u9759\u7684\u30a6\u30a7\u30d6\u30db\u30b9\u30c6\u30a3\u30f3\u30b0\u3092\u6709\u52b9\u306b\u3059\u308b\u3002 <\/p>\n

\"\"<\/p>\n

CloudFront \u306e\u30c7\u30a3\u30b9\u30c8\u30ea\u30d3\u30e5\u30fc\u30b7\u30e7\u30f3\u4f5c\u6210<\/h2>\n

CloudFront \u306e\u30c7\u30a3\u30b9\u30c8\u30ea\u30d3\u30e5\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u3002
Origin Domain Name<\/code> \u306b\u306f\u3001\u5148\u7a0b\u6709\u52b9\u306b\u3057\u305f\u9759\u7684\u30a6\u30a7\u30d6\u30db\u30b9\u30c6\u30a3\u30f3\u30b0\u306e\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306eURL\u3092\u5165\u529b\u3059\u308b\u3002 <\/p>\n

\"\"<\/p>\n

Lambda@Edge \u4f5c\u6210<\/h2>\n

\u7d9a\u3044\u3066 Lambda@Edge<\/code> \u3092\u4f5c\u6210\u3059\u308b\u3002
\u3053\u3053\u3067\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u3001 Lambda<\/code> \u3092\u4f5c\u6210\u3059\u308b\u30ea\u30fc\u30b8\u30e7\u30f3\u306f\u3001 \u7c73\u56fd\u6771\u90e8\uff08\u30d0\u30fc\u30b8\u30cb\u30a2\u5317\u90e8\uff09 \u30ea\u30fc\u30b8\u30e7\u30f3<\/strong> \u3068\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3053\u3068\u3002<\/p>\n

\"\"<\/p>\n

Lambda<\/code> \u3092\u5b9f\u884c\u3059\u308b\u30ed\u30fc\u30eb\u306f\u3001\u300c\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304b\u3089\u65b0\u3057\u3044\u30ed\u30fc\u30eb\u3092\u4f5c\u6210\u300d\u3092\u9078\u629e\u3001\u300c\u57fa\u672c\u7684\u306a Lambda@Edge \u306e\u30a2\u30af\u30bb\u30b9\u6a29\u9650(CloudFront \u30c8\u30ea\u30ac\u30fc\u306e\u5834\u5408)\u300d\u3092\u9078\u3076\u3002<\/p>\n

\"\"<\/p>\n

\u30b3\u30fc\u30c9\u306f\u4e0b\u8a18\u306e\u901a\u308a\u3002<\/p>\n

'use strict';\nexports.handler = (event, context, callback) => {\n\n    \/\/ Get request and request headers\n    const request = event.Records[0].cf.request;\n    const headers = request.headers;\n\n    \/\/ Configure authentication\n    const authUser = 'hiroshi'; \/\/ \u30e6\u30fc\u30b6\u30fc\n    const authPass = 'yujin'; \/\/ \u30d1\u30b9\u30ef\u30fc\u30c9\n\n    \/\/ Construct the Basic Auth string\n    const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64');\n\n    \/\/ Require Basic authentication\n    if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString) {\n        const body = 'Unauthorized';\n        const response = {\n            status: '401',\n            statusDescription: 'Unauthorized',\n            body: body,\n            headers: {\n                'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}]\n            },\n        };\n        callback(null, response);\n    }\n\n    \/\/ Continue request processing if authentication passed\n    callback(null, request);\n};<\/code><\/pre>\n

Lambda<\/code> \u95a2\u6570\u306e\u5236\u9650\u3068\u3057\u3066\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u3082\u306e\u304c\u3042\u308b\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3002
(\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3088\u308a\u629c\u7c8b)<\/p>\n