Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- const fs = require("fs");
- const zlib = require("zlib");
- const path = require("path");
- (async () => {
- console.log(
- [
- "ログファイル",
- "リージョン",
- "時間",
- "バケット名",
- "ファイル名",
- "イベント",
- "userAgent",
- "署名バージョン"
- ].join()
- );
- const files = await find_files("./s3logs/");
- for (let i = 0; i < files.length; i++) {
- const result = await load_event_file(files[i]);
- for (let j = 0; j < result.length; j++) {
- const ev = result[j];
- if (
- ev.eventSource === "s3.amazonaws.com" &&
- ev.additionalEventData.SignatureVersion != null &&
- ev.additionalEventData.SignatureVersion === "SigV2"
- ) {
- const date = new Date(ev.eventTime);
- let column = [];
- column.push(
- files[i],
- `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`,
- ev.awsRegion,
- ev.requestParameters.bucketName,
- ev.requestParameters.key,
- ev.eventName,
- ev.userAgent,
- ev.additionalEventData.SignatureVersion
- );
- console.log(column.join());
- //console.log(ev);
- }
- }
- }
- })();
- async function find_files(dirpath) {
- let file_list = [];
- const fromDir = startPath => {
- const files = fs.readdirSync(startPath);
- for (let i = 0; i < files.length; i++) {
- const filename = path.join(startPath, files[i]);
- const stat = fs.lstatSync(filename);
- if (stat.isDirectory()) {
- fromDir(filename); //recurse
- } else if (filename.indexOf("json.gz") >= 0) {
- file_list.push(filename);
- }
- }
- };
- fromDir(dirpath);
- return file_list;
- }
- async function load_event_file(file_name) {
- const gzipContent = fs.readFileSync(file_name);
- const result = await new Promise((resolv, reject) => {
- zlib.gunzip(gzipContent, (err, binary) => {
- if (err) {
- reject(err);
- }
- const string = JSON.parse(binary.toString("utf-8"));
- resolv(string.Records);
- });
- });
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement