-1

I need to get all the files under a directory in aws fsx, I already know how to get a files from a particular directory using FS npm module ,but don't know how to get from AWS Fsx.

I read this document but no idea or what method need to use for this.

Document : https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-fsx/ Npm: https://www.npmjs.com/package/@aws-sdk/client-fsx

I tried with multiple doc , but not get the correct method to use.

dev_20
  • 3
  • 3

1 Answers1

0

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);
SterbenTheOG
  • 63
  • 11