Advertisement
yudaduy

write and upload csv file to disk

Jan 8th, 2023
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const {Storage} = require('@google-cloud/storage');
  2. const ObjectToCsv = require('objects-to-csv');
  3.  
  4. const uploadFile = async ({file, destination, bucket}) => {
  5.     return new Promise((resolve, reject) => {
  6.         const storage = new Storage();
  7.  
  8.         storage.bucket(bucket).upload(file, {
  9.             destination: destination
  10.         })
  11.         .then(() => {
  12.             return resolve({
  13.                 "code": 200,
  14.                 "message": `success upload file ${file} to bucket ${bucket}`
  15.             });
  16.         })
  17.         .catch((error) => {
  18.             return reject({
  19.                 "code": 400,
  20.                 "error": error.message
  21.             })
  22.         });
  23.     })
  24. }
  25.  
  26. const writeCsv = async ({filename, payload}) => {
  27.     return new Promise(async (resolve, reject) => {
  28.         try {
  29.             payload.map((data) => data.UPDATED_DATE = new Date(data.UPDATED_DATE).toISOString());
  30.             const csvFile = new ObjectToCsv(payload);
  31.             await csvFile.toDisk(`./uploads/geotagging/${filename}`);
  32.             return resolve(`./uploads/geotagging/${filename}`);
  33.         } catch (error) {
  34.             return reject({
  35.                 "code": 400,
  36.                 "error": error
  37.             });
  38.         }
  39.     });
  40. }
  41.  
  42. module.exports = {
  43.     uploadFile,
  44.     writeCsv
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement