wip: refactor s3 access and generate file listing on build

This commit is contained in:
Nils Knappmeier
2023-08-05 00:04:45 +02:00
parent 520e1d5f08
commit ab920cc1ce
5 changed files with 3975 additions and 72 deletions
+3928 -71
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -30,8 +30,9 @@
"uglify-js": "^3.1.4"
},
"devDependencies": {
"@aws-sdk/client-s3": "^3.385.0",
"@playwright/test": "^1.17.1",
"aws-sdk": "^2.1.49",
"aws-sdk": "^2.1430.0",
"babel-loader": "^5.0.0",
"babel-runtime": "^5.1.10",
"benchmark": "~1.0",
+3
View File
@@ -0,0 +1,3 @@
const { listBucketFiles } = require('./list-bucket-files');
module.exports = { listBucketFiles };
@@ -0,0 +1,35 @@
const { S3Client, ListObjectsV2Command } = require('@aws-sdk/client-s3');
const bucket = process.env.S3_BUCKET_NAME;
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
// https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html
throw new Error(
'AWS credentials must be present in environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY'
);
}
const client = new S3Client({
region: 'us-east-1'
});
async function listBucketFiles() {
const command = new ListObjectsV2Command({
Bucket: bucket
});
let isTruncated = true;
const files = [];
while (isTruncated) {
const { Contents, IsTruncated, NextContinuationToken } = await client.send(
command
);
files.push(...Contents.map(s3obj => s3obj.Key));
isTruncated = IsTruncated;
command.input.ContinuationToken = NextContinuationToken;
}
return files
}
module.exports = { listBucketFiles}
+7
View File
@@ -0,0 +1,7 @@
const { listBucketFiles } = require('./index');
const main = async () => {
console.log(await listBucketFiles());
};
main().catch(console.error);