I have some sample node code that you can use. It uses the aws-sdk@v2
, so if you have done R&D on the v3 SDK it will be different.
const AWS = require('aws-sdk');
// Set your AWS credentials and region
AWS.config.update({
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
region: 'us-east-1' // Replace with your desired AWS region
});
// Create an AWS FSx for Windows File Server client
const fsxClient = new AWS.FSx();
// Specify the FSx directory ID and path
const directoryId = 'fs-xxxxxxxx';
const directoryPath = '\\SharedFolder\\Subfolder'; // Replace with your FSx directory path
// Recursive function to retrieve all files under a directory
async function getAllFiles(directoryId, directoryPath) {
try {
const listItemsParams = {
DirectoryId: directoryId,
Path: directoryPath
};
const listItemsResponse = await fsxClient.listItems(listItemsParams).promise();
const files = listItemsResponse.Items.filter(item => item.Type === 'FILE');
for (const file of files) {
console.log(`File: ${file.Name}`);
}
const directories = listItemsResponse.Items.filter(item => item.Type === 'DIRECTORY');
for (const directory of directories) {
const subdirectoryPath = `${directoryPath}\\${directory.Name}`;
await getAllFiles(directoryId, subdirectoryPath);
}
} catch (err) {
console.error('Error retrieving files:', err);
}
}
// Call the function to get all files under the directory
getAllFiles(directoryId, directoryPath);