-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlogdisplay.js
56 lines (49 loc) · 1.58 KB
/
logdisplay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const AWS = require('aws-sdk');
const fs = require('fs');
// Set your AWS credentials and S3 bucket information
const accessKeyId = '';
const secretAccessKey = '';
const bucketName = '';
const region = 'us-east-1';
const directoryPrefix = ''; // Replace with the actual directory path
// Create an S3 instance
const s3 = new AWS.S3({
accessKeyId,
secretAccessKey,
region,
});
// Set the parameters for the S3 listObjects request
const listParams = {
Bucket: bucketName,
Prefix: directoryPrefix,
};
// Use the listObjects method to list all objects in the specified "directory"
s3.listObjects(listParams, (listErr, listData) => {
if (listErr) {
console.error('Error listing objects:', listErr);
} else {
// Process the list of objects
const logFiles = listData.Contents.map(obj => obj.Key);
const logContents = []; // Array to store log content
// Read each log file
logFiles.forEach(logFile => {
const readParams = {
Bucket: bucketName,
Key: logFile,
};
s3.getObject(readParams, (readErr, readData) => {
if (readErr) {
console.error('Error reading log file:', readErr);
} else {
// Process the log file content
const logContent = readData.Body.toString('utf-8');
logContents.push(logContent);
// If this is the last log file, display the array
if (logContents.length === logFiles.length) {
console.log('Log contents:', logContents);
}
}
});
});
}
});