Hello, there. I'm trying to upload a zip file, but it's resulting in a 0 byte file (You can check screenshot here). As you can see XML files are being uploaded fine. I save zipped file locally before sending it to S3. This local file size is greater than zero.SaleController.getBySaleId()function getBySaleId(req, res) { const fetch = require("../services/aws/FetchXml") const zip = require("../services/aws/Zip") const fs = require("fs") Sale.findById(req.params.sale_id).then(sale => { if (sale.cancellation_xml_url) { Promise.all([ fetch.download(sale.issuance_xml_url), fetch.download(sale.cancellation_xml_url) ]).then(xmls => { let compression = zip.addXmls(xmls) if (compression.finalize) { let zip = s3Sender.upload( fs.readFileSync(`./tmp/${compression.fileName}.zip`), sale.system_id, sale.franchise_id, sale.order_id, "exportacao", "zip" ) return zip .then(location => { return res.json({ status: "Sucesso", link: location }) }) .catch(err => { console.error(err.message) return res.status(500).json({ status: "Falha", message: "Algo ocorreu ao enviar o zip para o S3" }) }) } return res.status(500).json({ status: "Falha", message: "Algo ocorreu ao zipar os XMLs" }) }) } }) } S3Sender.jsconst config = require("config") const aws = require("aws-sdk") aws.config.update({ accessKeyId: config.get("amazon.accessKeyId"), secretAccessKey: config.get("amazon.secretAccessKeyId") }) function upload(file, systemId, franchiseId, orderId, operation, fileType) { const base64Data = new Buffer(file, "Binary") const s3 = new aws.S3() const params = { Bucket: "notas-fiscais-sistemas-acs", Key: `${systemId}-${franchiseId}-${orderId}-${operation}.${fileType}`, Body: base64Data, ACL: "public-read" } let s3Upload = new Promise((resolve, reject) => s3.upload( params, (err, data) => (err ? reject(err) : resolve(data.Location)) ) ) return s3Upload } module.exports = { upload }
Submitted April 30, 2018 at 02:22AM by giocruz
No comments:
Post a Comment