ArticleZip > Upload Entire Directory Tree To S3 Using Aws Sdk In Node Js

Upload Entire Directory Tree To S3 Using Aws Sdk In Node Js

When working with AWS and Node.js, uploading an entire directory tree to an S3 bucket can be a super useful task. Fortunately, AWS SDK for Node.js provides convenient features to help you achieve this effortlessly. In this guide, we will walk you through the steps involved in uploading a directory tree to S3 using the AWS SDK in Node.js.

First things first, make sure you have the AWS SDK installed in your Node.js project. If not, you can easily add it to your project by running the following command in your project directory:

Plaintext

npm install aws-sdk

Once you have the AWS SDK installed, you can start by initializing the SDK in your Node.js file. Here's a basic example of how you can do this:

Javascript

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

// Set up AWS credentials
AWS.config.update({
  accessKeyId: 'YOUR_ACCESS_KEY_ID',
  secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
  region: 'YOUR_REGION'
});

// Create an S3 instance
const s3 = new AWS.S3();

Replace `'YOUR_ACCESS_KEY_ID'`, `'YOUR_SECRET_ACCESS_KEY'`, and `'YOUR_REGION'` with your actual AWS credentials and desired region.

Now, to upload the entire directory tree to S3, you will need to recursively go through the directory structure and upload each file to the S3 bucket. Here's a simple recursive function that achieves this:

Javascript

const fs = require('fs');
const path = require('path');

function uploadDirectory(directoryPath, bucketName) {
  fs.readdir(directoryPath, (err, files) => {
    if (err) {
      console.error('Error reading directory:', err);
      return;
    }
    
    files.forEach((file) => {
      const filePath = path.join(directoryPath, file);
      
      fs.stat(filePath, (err, stats) => {
        if (err) {
          console.error('Error stating file:', err);
          return;
        }
        
        if (stats.isDirectory()) {
          uploadDirectory(filePath, bucketName);
        } else {
          const fileContent = fs.readFileSync(filePath);
          
          s3.upload({
            Bucket: bucketName,
            Key: filePath,
            Body: fileContent
          }, (err, data) => {
            if (err) {
              console.error('Error uploading file:', err);
            } else {
              console.log('File uploaded successfully:', data.Location);
            }
          });
        }
      });
    });
  });
}

// Call the function to upload the directory tree
uploadDirectory('/path/to/directory', 'YOUR_BUCKET_NAME');

Replace `'/path/to/directory'` with the path to the directory you want to upload and `'YOUR_BUCKET_NAME'` with the name of your S3 bucket.

Once you run this script, it will recursively upload all files in the specified directory to your S3 bucket, preserving the directory structure. It's a handy way to quickly transfer a group of files to S3 using the power of Node.js and the AWS SDK.

So, the next time you need to upload an entire directory tree to S3 in your Node.js project, remember these steps and make the process seamless and efficient!

×