如何在Node.js中获取AWS共享路径中文件夹中的所有文件?

huangapple go评论49阅读模式
英文:

How to get all the files in a folder in AWS shared path in nodejs?

问题

我需要获取AWS FSx目录下的所有文件,我已经知道如何使用FS npm模块从特定目录获取文件,但不知道如何从AWS FSx获取。

我阅读了这份文档,但不清楚需要使用哪种方法。

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

我尝试了多份文档,但没有找到正确的使用方法。

英文:

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.

答案1

得分: 0

以下是您要翻译的内容:

我有一些示例节点代码供您使用。它使用 aws-sdk@v2,所以如果您已经对v3 SDK进行了研究,它会有所不同。

const AWS = require('aws-sdk');

// 设置您的AWS凭据和区域
AWS.config.update({
  accessKeyId: 'YOUR_ACCESS_KEY_ID',
  secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
  region: 'us-east-1' // 用您期望的AWS区域替换
});

// 创建一个AWS FSx for Windows File Server客户端
const fsxClient = new AWS.FSx();

// 指定FSx目录ID和路径
const directoryId = 'fs-xxxxxxxx';
const directoryPath = '\\SharedFolder\\Subfolder'; // 用您的FSx目录路径替换

// 递归函数以检索目录下的所有文件
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.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('检索文件时出错:', err);
  }
}

// 调用该函数以获取目录下的所有文件
getAllFiles(directoryId, directoryPath);

请注意,我已经翻译了代码部分,不包括代码中的注释。如果需要其他翻译,请告诉我。

英文:

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);

huangapple
  • 本文由 发表于 2023年5月26日 16:35:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定