Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. "use strict";
  2. const fs = require("fs");
  3. const zlib = require("zlib");
  4. const path = require("path");
  5.  
  6. (async () => {
  7. console.log(
  8. [
  9. "ログファイル",
  10. "リージョン",
  11. "時間",
  12. "バケット名",
  13. "ファイル名",
  14. "イベント",
  15. "userAgent",
  16. "署名バージョン"
  17. ].join()
  18. );
  19. const files = await find_files("./s3logs/");
  20. for (let i = 0; i < files.length; i++) {
  21. const result = await load_event_file(files[i]);
  22. for (let j = 0; j < result.length; j++) {
  23. const ev = result[j];
  24. if (
  25. ev.eventSource === "s3.amazonaws.com" &&
  26. ev.additionalEventData.SignatureVersion != null &&
  27. ev.additionalEventData.SignatureVersion === "SigV2"
  28. ) {
  29. const date = new Date(ev.eventTime);
  30. let column = [];
  31. column.push(
  32. files[i],
  33. `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`,
  34. ev.awsRegion,
  35. ev.requestParameters.bucketName,
  36. ev.requestParameters.key,
  37. ev.eventName,
  38. ev.userAgent,
  39. ev.additionalEventData.SignatureVersion
  40. );
  41. console.log(column.join());
  42. //console.log(ev);
  43. }
  44. }
  45. }
  46. })();
  47.  
  48. async function find_files(dirpath) {
  49. let file_list = [];
  50. const fromDir = startPath => {
  51. const files = fs.readdirSync(startPath);
  52. for (let i = 0; i < files.length; i++) {
  53. const filename = path.join(startPath, files[i]);
  54. const stat = fs.lstatSync(filename);
  55. if (stat.isDirectory()) {
  56. fromDir(filename); //recurse
  57. } else if (filename.indexOf("json.gz") >= 0) {
  58. file_list.push(filename);
  59. }
  60. }
  61. };
  62. fromDir(dirpath);
  63. return file_list;
  64. }
  65.  
  66. async function load_event_file(file_name) {
  67. const gzipContent = fs.readFileSync(file_name);
  68. const result = await new Promise((resolv, reject) => {
  69. zlib.gunzip(gzipContent, (err, binary) => {
  70. if (err) {
  71. reject(err);
  72. }
  73. const string = JSON.parse(binary.toString("utf-8"));
  74. resolv(string.Records);
  75. });
  76. });
  77. return result;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement