Skip to content

Commit b4d0051

Browse files
datadump refactor
1 parent 13bdf82 commit b4d0051

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

tools/spectral/ipa/metrics/dataDump.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,49 @@ import { PutObjectCommand, S3Client, S3ServiceException } from '@aws-sdk/client-
66
import config from './config.js';
77
import path from 'path';
88

9-
let AWSConfig = {
10-
aws: {
11-
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
12-
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
13-
region: 'us-east-1',
14-
},
15-
s3: {
16-
prefix: process.env.S3_BUCKET_PREFIX,
17-
},
18-
};
9+
function loadConfig() {
10+
if (existsSync('.env') && !process.env.S3_BUCKET_PREFIX) {
11+
dotenv.config();
12+
}
13+
return {
14+
aws: {
15+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
16+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
17+
region: 'us-east-1',
18+
},
19+
s3: {
20+
prefix: process.env.S3_BUCKET_PREFIX,
21+
},
22+
};
23+
}
24+
25+
export function getS3FilePath() {
26+
const AWSConfig = loadConfig();
1927

20-
function getS3FilePath() {
2128
const pathParts = AWSConfig.s3.prefix.replace('s3://', '').split('/');
2229
const bucketName = pathParts[0];
2330
let key = pathParts.slice(1).join('/');
2431
return { bucketName, key };
2532
}
2633

27-
/**
28-
* Upload a file to an S3 bucket.
29-
* @param filePath
30-
*/
31-
async function uploadMetricCollectionDataToS3(filePath = config.defaultMetricCollectionResultsFilePath) {
32-
const client = new S3Client({
34+
export function getS3Client() {
35+
const AWSConfig = loadConfig();
36+
37+
return new S3Client({
3338
credentials: {
3439
accessKeyId: AWSConfig.aws.accessKeyId,
3540
secretAccessKey: AWSConfig.aws.secretAccessKey,
3641
},
3742
region: AWSConfig.aws.region,
3843
});
39-
const bucketName = AWSConfig.s3.bucketName;
44+
}
45+
46+
/**
47+
* Upload a file to an S3 bucket.
48+
* @param filePath
49+
*/
50+
export async function uploadMetricCollectionDataToS3(filePath = config.defaultMetricCollectionResultsFilePath) {
51+
const client = getS3Client();
4052
const formattedDate = new Date().toISOString().split('T')[0];
4153

4254
const fileProps = getS3FilePath();
@@ -52,38 +64,24 @@ async function uploadMetricCollectionDataToS3(filePath = config.defaultMetricCol
5264
} catch (caught) {
5365
if (caught instanceof S3ServiceException && caught.name === 'EntityTooLarge') {
5466
console.error(
55-
`Error from S3 while uploading object to ${bucketName}. \
56-
The object was too large. To upload objects larger than 5GB, use the S3 console (160GB max) \
57-
or the multipart upload API (5TB max).`
67+
`Error from S3 while uploading object. The object was too large. \
68+
To upload objects larger than 5GB, use the S3 console (160GB max) or the multipart upload API (5TB max).`
5869
);
5970
throw caught;
6071
} else if (caught instanceof S3ServiceException) {
61-
console.error(`Error from S3 while uploading object to ${bucketName}. ${caught.name}: ${caught.message}`);
72+
console.error(`Error from S3 while uploading object. ${caught.name}: ${caught.message}`);
6273
throw caught;
6374
} else {
6475
throw caught;
6576
}
6677
}
6778
}
6879

69-
const args = process.argv.slice(2);
70-
const filePath = args[0];
80+
if (require.main === module) {
81+
const args = process.argv.slice(2);
82+
const filePath = args[0];
7183

72-
//If the config is not populated by the Github Action env variables
73-
if (existsSync('.env') && !AWSConfig.s3.bucketName) {
74-
dotenv.config();
75-
AWSConfig = {
76-
aws: {
77-
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
78-
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
79-
region: 'us-east-1',
80-
},
81-
s3: {
82-
prefix: process.env.S3_BUCKET_PREFIX,
83-
},
84-
};
84+
uploadMetricCollectionDataToS3(filePath)
85+
.then(() => console.log('Data dump to S3 completed successfully.'))
86+
.catch((error) => console.error(error.message));
8587
}
86-
87-
uploadMetricCollectionDataToS3(filePath)
88-
.then(() => console.log('Data dump to S3 completed successfully.'))
89-
.catch((error) => console.error(error.message));

0 commit comments

Comments
 (0)