get3FolderNames

2023-09-17

以下示例是关于Javascript中包含get3FolderNames用法的示例代码,想了解get3FolderNames的具体用法?get3FolderNames怎么用?get3FolderNames使用的例子?那么可以参考以下相关源代码片段来学习它的具体使用方法。

文件名:getS3FolderNames[英]:getS3FolderNames源码类型:Javascript
const AWS = require("aws-sdk");

// Configure AWS with your credentials and desired region
AWS.config.update({ region: "us-east-1" }); // Replace with your desired region

const s3 = new AWS.S3();

// Array of S3 bucket names
const bucketNames = ["v2production.com", "v3production.com"]; // Replace with your S3 bucket names
let masterFolderNames = [];

// Function to list folder names in an S3 bucket
async function listFolderNames(bucketName) {
  try {
    const listObjectsResponse = await s3
      .listObjectsV2({ Bucket: bucketName, Delimiter: "/" })
      .promise();

    const folderNames = listObjectsResponse.CommonPrefixes.map((prefix) => {
      // Remove trailing '/' character from the prefix to get the folder name
      return `s3://${bucketName}/${prefix.Prefix.slice(0, -1)}/index.html`;
    });

    return folderNames;
  } catch (error) {
    console.error(`Error listing folders in bucket ${bucketName}:`, error);
    return [];
  }
}

// Loop through the array of bucket names and list folder names for each bucket
module.exports = async function getS3FolderNames() {
  for (const bucketName of bucketNames) {
    const folderNames = await listFolderNames(bucketName);
    // push folernames to masterFoldernsmames array
    masterFolderNames = [...folderNames, ...masterFolderNames];
  }
  return masterFolderNames;
};

本文地址:https://itbaoku.cn/snippets/872565.html