This code provides a function latestS3
that retrieves the 5 most recently modified files from an S3 bucket, optionally filtering them based on a given pattern. It's designed to be used both as a server-side module and potentially in a client-side environment.
npm run import -- "latest s3 bucket"
var fs = require('fs');
var path = require('path');
var minimatch = require("minimatch")
var AWS = require('aws-sdk');
var importer = require('../Core');
var s3 = new AWS.S3();
// For dev purposes only
var AWS_HTTP = 'https://s3-us-west-2.amazonaws.com/selenium-bots/';
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
var key = JSON.parse(fs.readFileSync(path.join(PROFILE_PATH, '.credentials/aws-sdk.json')).toString());
AWS.config.update(key);
function getS3Objects(bucket, marker) {
return new Promise((resolve, reject) => s3.listObjects({
Bucket: bucket || 'selenium-bots',
Prefix: '',
Marker: marker
}, function (err, resp) {
if(err) {
return reject(err);
}
return resolve(resp);
}))
.then(resp => {
if(resp.IsTruncated) {
return getS3Objects(bucket, resp.Contents[resp.Contents.length-1].Key)
.then(contents => resp.Contents.concat(contents));
}
return resp.Contents;
})
}
function latestS3(match, bucket) {
return getS3Objects(bucket)
.then(files => {
return files.sort((a, b) => b.LastModified.getTime() - a.LastModified.getTime())
.filter(s => !match || minimatch(s.Key, match))
.slice(0, 5)
.map(i => AWS_HTTP + i.Key)
});
}
module.exports = latestS3;
if(typeof $ !== 'undefined') {
$.async();
latestS3()
.then(r => $.sendResult(r))
.catch(e => $.sendError(e))
}
const fs = require('fs');
const path = require('path');
const minimatch = require('minimatch');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const importer = require('../Core');
// Configuration constants
const AWS_HTTP = 'https://s3-us-west-2.amazonaws.com/selenium-bots/';
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
const CREDENTIALS_FILE = path.join(PROFILE_PATH, '.credentials/aws-sdk.json');
// Load AWS credentials from file
const loadCredentials = () => {
try {
const credentials = fs.readFileSync(CREDENTIALS_FILE, 'utf8');
return JSON.parse(credentials);
} catch (error) {
throw new Error('Failed to load AWS credentials');
}
};
// Update AWS config with credentials
const updateAWSConfig = (credentials) => {
AWS.config.update(credentials);
};
// Get S3 objects from a bucket
const getS3Objects = (bucket, marker) => {
return s3.listObjects({
Bucket: bucket ||'selenium-bots',
Prefix: '',
Marker: marker
}).promise()
.then((resp) => {
if (resp.IsTruncated) {
return getS3Objects(bucket, resp.Contents[resp.Contents.length - 1].Key);
}
return resp.Contents;
});
};
// Get the latest S3 objects that match a pattern
const getLatestS3 = (match, bucket) => {
return getS3Objects(bucket)
.then((files) => {
return files.sort((a, b) => b.LastModified.getTime() - a.LastModified.getTime())
.filter((s) =>!match || minimatch(s.Key, match))
.slice(0, 5)
.map((i) => AWS_HTTP + i.Key);
});
};
// Export the getLatestS3 function
module.exports = getLatestS3;
// Initialize AWS config and export the function
const init = () => {
const credentials = loadCredentials();
updateAWSConfig(credentials);
return getLatestS3();
};
if (typeof $!== 'undefined') {
init()
.then((r) => $.sendResult(r))
.catch((e) => $.sendError(e));
}
This code defines a function latestS3
that retrieves the 5 most recently modified files from an S3 bucket that match a given pattern.
Here's a breakdown:
Dependencies:
fs
, path
, minimatch
, AWS
, and importer
modules.fs
is for file system operations.path
is for working with file paths.minimatch
is for pattern matching.AWS
is for interacting with AWS services, specifically S3.importer
is likely a custom module.AWS Configuration:
aws-sdk.json
).AWS_HTTP
as the base URL for accessing S3 objects.getS3Objects
Function:
latestS3
Function:
match
pattern (optional) and a bucket
name as arguments.getS3Objects
to get all objects in the bucket.match
pattern using minimatch
.AWS_HTTP
.Module Export:
latestS3
function is exported as the main module.Client-Side Execution (Conditional):
latestS3
and sends the result or error to a client-side function ($.sendResult
and $.sendError
).Let me know if you have any more questions!