1

I'm using Box SDK for nodeJS and I have a function for downloading files. I just need to setup the download to be dropped in a project subfolder I've read the documentation and could not find any parameter for this

async function downloadBoxFile(fileID, fileName) {
  try {
    // Load configuration
    const config = loadConfiguration();
    const { clientID, clientSecret, enterpriseID } = config.boxConfiguration;

    // Authenticate Box client
    const boxClient = boxAuthentication(clientID, clientSecret, enterpriseID);        

    const fileReadStream = await boxClient.files.getReadStream(fileID, null, {
      fields: 'modified_at, size, sha1, owned_by'
    });

    const writeStream = fs.createWriteStream(fileName);
    fileReadStream.pipe(writeStream);

    return new Promise((resolve, reject) => {
      fileReadStream.on('end', () => {        
        writeLog('> File downloaded successfully:' + fileName);
        resolve();
      });
      fileReadStream.on('error', (error) => {
        console.log('Error downloading file:', error);
        writeLog('Error downloading file:', error);
        reject(error);
      });
    });
  } catch (error) {
    console.log('Error downloading file:', error);
    writeLog('Error downloading file:', error);
    throw error;
  }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
CMartins
  • 3,247
  • 5
  • 38
  • 53

1 Answers1

0

I manage to solve it simply passing the path param to the function and append to the filename

const fullPath = path.join(folderPath, fileName);
const writeStream = fs.createWriteStream(fullPath);
fileReadStream.pipe(writeStream);
CMartins
  • 3,247
  • 5
  • 38
  • 53