Skip to content

Instantly share code, notes, and snippets.

@mactive
Last active September 22, 2021 23:59
Show Gist options
  • Save mactive/8880b0d6908be37fb15cd89038be27d5 to your computer and use it in GitHub Desktop.
Save mactive/8880b0d6908be37fb15cd89038be27d5 to your computer and use it in GitHub Desktop.
download_stream.js
#!/bin/bash
for i in {1..5}
do
echo "Welcome $i times"
node ./download.js
md5 2.zip
rm 2.zip
echo "=========end $i ========"
done
const Axios = require('axios')
const fs = require('fs')
const path = require('path')
let zipPath = "https://kerry-vender.oss-cn-beijing.aliyuncs.com/1.zip"
let zipPath1 = "https://kerry-vender.oss-cn-beijing.aliyuncs.com/downtest/OmniPlan_Pro.zip"
async function downloadFile(fileUrl, outputLocationPath) {
const writer = fs.createWriteStream(outputLocationPath);
return Axios({
method: 'get',
url: fileUrl,
responseType: 'stream',
}).then(response => {
//ensure that the user can call `then()` only when the file has
//been downloaded entirely.
return new Promise((resolve, reject) => {
response.data.pipe(writer);
let error = null;
writer.on('error', err => {
error = err;
writer.close();
reject(err);
});
// writer.on('data', (chunk) => {
// console.log(chunk.length)
// });
writer.on('finish', () => {
if (!error) {
resolve(true);
}
//no need to call the reject here, as it will have been called in the
//'error' stream;
});
});
});
}
async function downloadSingleFile(taskId, fileName) {
let zipPath1 = "https://kerry-vender.oss-cn-beijing.aliyuncs.com/downtest/OmniPlan_Pro.zip"
console.log('[download_tos.js-log] downloadSingleFile %s', zipPath1)
let filePath = path.join(taskId + fileName)
let outputPath = path.resolve(__dirname, filePath)
console.log(6)
await downloadFile(zipPath1, outputPath)
console.log(7)
}
(async () => {
console.log("before start")
await downloadSingleFile('2','.zip')
console.log("after start")
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment