Last active
June 1, 2018 07:34
-
-
Save ayumi-k/de274280b4fcc75c4c9410db73f6bc4d to your computer and use it in GitHub Desktop.
LambdaからS3へHTMLファイルアップロード(Webサイトホスティング)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//メンテナンス時間(Lambdaの環境変数で時間を設定する) | |
var startTime = process.env['START_TIME']; | |
var endTime = process.env['END_TIME']; | |
//AWS | |
var aws = require('aws-sdk'); | |
var s3 = new aws.S3(); | |
aws.config.region = 'ap-northeast-1'; | |
var bucketName = '<バケット名>'; | |
var contentType = 'text/html'; | |
//時間設定 | |
var date = new Date(); | |
date.setTime(date.getTime() + 32400000); // 1000 * 60 * 60 * 9(hour) | |
var year = date.getFullYear(); | |
var month = date.getMonth()+1; | |
var day = date.getDate(); | |
var dayOfWeek = date.getDay() ; | |
var dayOfWeekStr = [ "日", "月", "火", "水", "木", "金", "土" ][dayOfWeek] ; | |
exports.handler = function(event, context) { | |
var fileName = 'index.html'; | |
var body = [ | |
'<!DOCTYPE html>', | |
'<html lang="ja">', | |
'<head>', | |
'<meta name="robots" content="noindex,nofollow">', | |
'<meta charset="utf-8">', | |
'<meta name="viewport" content="width=device-width">', | |
'<title>メンテナンス</title>', | |
'</head>', | |
'<body>', | |
'', | |
'<div style="margin: 100px 15px; text-align: center;">', | |
' <div style="margin-bottom: 30px;">メンテナンス中です</div>', | |
' <div style="margin-top: 30px;">【メンテナンス時間】' + year + '年' + month +'月' + day + '日(' + dayOfWeekStr + ') ' + startTime + '~' + endTime + '</div>', | |
'</div>', | |
'', | |
'</body>', | |
'</html>', | |
].join(""); | |
var params = { | |
Key: fileName, | |
Body: body, | |
Bucket: bucketName, | |
ContentType: contentType, | |
}; | |
s3.putObject(params, function(err, data) { | |
if (err) { | |
console.log("Error uploading data: ", err); | |
} else { | |
console.log("Successfully uploaded data to " + bucketName + "/" + fileName); | |
} | |
context.done(null, 'Finished UploadObjectOnS3'); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment