node download s3 file

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

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

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

// Specify the S3 bucket and file key
const bucketName = 'YOUR_S3_BUCKET_NAME';
const fileKey = 'YOUR_S3_FILE_KEY';

// Set the local file path where the S3 file will be downloaded
const localFilePath = 'LOCAL_PATH_TO_SAVE_FILE';

// Create a writable stream to save the S3 file locally
const fileStream = fs.createWriteStream(localFilePath);

// Set the parameters for S3 getObject operation
const getObjectParams = {
  Bucket: bucketName,
  Key: fileKey
};

// Execute the getObject operation and save the file locally
const s3Stream = s3.getObject(getObjectParams).createReadStream();
s3Stream.pipe(fileStream);

// Handle the completion of the download process
fileStream.on('finish', () => {
  console.log('File downloaded successfully.');
});

// Handle errors during the download process
fileStream.on('error', (err) => {
  console.error('Error downloading file:', err);
});