wip: refactor s3 access and generate file listing on build
This commit is contained in:
Generated
+3928
-71
File diff suppressed because it is too large
Load Diff
+2
-1
@@ -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",
|
||||
|
||||
@@ -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}
|
||||
@@ -0,0 +1,7 @@
|
||||
const { listBucketFiles } = require('./index');
|
||||
|
||||
const main = async () => {
|
||||
console.log(await listBucketFiles());
|
||||
};
|
||||
|
||||
main().catch(console.error);
|
||||
Reference in New Issue
Block a user