node.js - GET list of objects located under a specific S3 folder

Node.js - GET list of objects located under a specific S3 folder

To retrieve a list of objects located under a specific folder in an S3 bucket using Node.js, you can use the AWS SDK for JavaScript, commonly known as aws-sdk. Make sure you've installed it using npm install aws-sdk.

Here's an example code snippet:

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: 'your-region' }); // Create an S3 object const s3 = new AWS.S3(); // Specify your S3 bucket and folder const bucketName = 'your-bucket-name'; const folderPrefix = 'your/folder/prefix/'; // Configure parameters for listing objects const params = { Bucket: bucketName, Prefix: folderPrefix }; // Use the listObjectsV2 method to retrieve the list of objects s3.listObjectsV2(params, (err, data) => { if (err) { console.error('Error:', err); } else { console.log('Objects in the folder:'); data.Contents.forEach(obj => console.log(obj.Key)); } }); 

Replace 'your-access-key-id', 'your-secret-access-key', 'your-region', 'your-bucket-name', and 'your/folder/prefix/' with your actual AWS credentials, region, bucket name, and folder prefix.

This example uses the listObjectsV2 method from the AWS SDK to retrieve a list of objects in the specified S3 bucket and folder.

Examples

  1. "Node.js AWS SDK list objects in S3 folder"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/', }; s3.listObjectsV2(params, (err, data) => { if (err) console.error(err); else console.log(data.Contents); }); 
    • Description: This code uses the AWS SDK for Node.js to list objects under a specific S3 folder by providing the bucket name and folder path in the params object.
  2. "Node.js AWS SDK recursive list S3 objects in folder"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const listAllObjects = async (params) => { let allObjects = []; const listObjects = async (marker) => { params.Marker = marker; const data = await s3.listObjectsV2(params).promise(); allObjects = allObjects.concat(data.Contents); if (data.NextContinuationToken) { await listObjects(data.NextContinuationToken); } }; await listObjects(); return allObjects; }; const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/', }; listAllObjects(params).then(objects => console.log(objects)); 
    • Description: This code provides a recursive function using the AWS SDK to list all objects under a specific S3 folder, handling continuation tokens for large result sets.
  3. "Node.js AWS SDK filter S3 objects by file extension"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/', }; s3.listObjectsV2(params, (err, data) => { if (err) console.error(err); else { const filteredObjects = data.Contents.filter(obj => obj.Key.endsWith('.txt')); console.log(filteredObjects); } }); 
    • Description: This code lists objects under a specific S3 folder and filters them based on a file extension (e.g., .txt) using the endsWith method.
  4. "Node.js AWS SDK list S3 objects with metadata"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/', }; s3.listObjectsV2(params, (err, data) => { if (err) console.error(err); else { const objectsWithMetadata = data.Contents.map(obj => ({ Key: obj.Key, LastModified: obj.LastModified, Size: obj.Size, Metadata: obj.Metadata, })); console.log(objectsWithMetadata); } }); 
    • Description: This code lists objects under a specific S3 folder and retrieves metadata such as LastModified, Size, and custom metadata using the Metadata property.
  5. "Node.js AWS SDK list S3 objects with pagination"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const listAllObjects = async (params) => { let allObjects = []; const listObjects = async (marker) => { params.Marker = marker; const data = await s3.listObjectsV2(params).promise(); allObjects = allObjects.concat(data.Contents); if (data.IsTruncated) { await listObjects(data.NextContinuationToken); } }; await listObjects(); return allObjects; }; const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/', }; listAllObjects(params).then(objects => console.log(objects)); 
    • Description: This code provides a paginated approach to listing all objects under a specific S3 folder using the AWS SDK, handling truncated results.
  6. "Node.js AWS SDK list S3 objects sorted by LastModified"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/', }; s3.listObjectsV2(params, (err, data) => { if (err) console.error(err); else { const sortedObjects = data.Contents.sort((a, b) => b.LastModified - a.LastModified); console.log(sortedObjects); } }); 
    • Description: This code lists objects under a specific S3 folder and sorts them based on the LastModified property in descending order.
  7. "Node.js AWS SDK list S3 objects with a specific prefix"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/specific-prefix', }; s3.listObjectsV2(params, (err, data) => { if (err) console.error(err); else console.log(data.Contents); }); 
    • Description: This code lists objects under a specific S3 folder with a specific prefix, filtering results based on the provided prefix.
  8. "Node.js AWS SDK list S3 objects with specific metadata value"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/', }; s3.listObjectsV2(params, (err, data) => { if (err) console.error(err); else { const objectsWithSpecificMetadata = data.Contents.filter(obj => obj.Metadata && obj.Metadata.YourCustomKey === 'YourCustomValue'); console.log(objectsWithSpecificMetadata); } }); 
    • Description: This code lists objects under a specific S3 folder and filters them based on a specific metadata key-value pair.
  9. "Node.js AWS SDK list S3 objects with a specific delimiter"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/', Delimiter: '/', }; s3.listObjectsV2(params, (err, data) => { if (err) console.error(err); else console.log(data.CommonPrefixes); }); 
    • Description: This code lists objects under a specific S3 folder and retrieves common prefixes (subfolders) using the Delimiter property.
  10. "Node.js AWS SDK list S3 objects with a specific content type"

    • Code:
      const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const params = { Bucket: 'your-bucket-name', Prefix: 'your-folder-path/', }; s3.listObjectsV2(params, (err, data) => { if (err) console.error(err); else { const objectsWithSpecificContentType = data.Contents.filter(obj => obj.ContentType === 'your-specific-content-type'); console.log(objectsWithSpecificContentType); } }); 
    • Description: This code lists objects under a specific S3 folder and filters them based on a specific content type, assuming that the content type is available as a property in the S3 metadata.

More Tags

invoke-webrequest xticks dictionary exchange-server mms events rgba date-difference filechooser complex-numbers

More Programming Questions

More Everyday Utility Calculators

More Financial Calculators

More Tax and Salary Calculators

More Investment Calculators