提高 axios 获取下载速度 [英] Improve axios get download speed
问题描述
我正在使用 axios
从 Azure 存储 Blob 下载文件 (~100MB).
axios({方法:'获取',网址:uri,onDownloadProgress: (progressEvent) =>{console.log("已加载:" + ((progressEvent.loaded/progressEvent.total) * 100) + "%");},响应类型:'数组缓冲区'}).然后({})
我的问题是实际下载文件需要很长时间(约 10 分钟).我以前使用的是 fetch(),它甚至比这还慢(约 15-20 分钟).大家有什么关于如何加快下载速度的建议吗?我的互联网速度不是问题,因为直接下载文件或使用 Azure 存储资源管理器(1.12.0,AzCopy 10.3.3)只需不到 2 分钟.
我也尝试使用 azure-storage 的 blobServiceClient,但速度与 axios 和 fetch 相似(大约 15kbps).
如果有帮助,这是在 React 应用程序中.
我测试了下载速度.希望我的结果对你有用.
<块引用>- 我上传
StorageExplorer.exe
作为下载测试的源文件.文件大小为 92.5M.
- 通过
Azure Storage Explore
下载文件,耗时1分07秒
.
- 通过chrome浏览器在门户中下载文件,需要
58秒
.
- 通过我的测试代码下载文件.
① 从 portal
或 Storage Explore
复制网址.
网址如:https://p*****ge.blob.core.windows.net/testcontainer/StorageExplorer.exe
经过我的代码测试,需要1分52秒
,非常不稳定,有时测试下载时间会更长.
②从AzCopy Command
复制Url.
url 格式如下: https://pan********ge.blob.core.windows.net/testcontainer/StorageExplorer.exe?se=2020-09-18T07%3A55%3A28Z&sp=rl&sv=2018-03-28&sr=c&sig=5kJyTBwHHRS******mlj3%2FWj9CmvQriXCMi4%3D
用相同的代码测试后,需要1分02秒
.
我的测试结论:
不要使用看起来像 https://p*****ge.blob.core.windows.net/testcontainer/StorageExplorer.exe
的 url.
您可以使用类似于从 AzCopy 命令获取的 url.
下面是我的测试代码.
<块引用>- npm 我进步了
- npm i axios
'使用严格'const Fs = require('fs')const Path = require('path')const axios = require('axios')const ProgressBar = require('progress')异步函数下载 () {const url = 'https://pan*****e.blob.core.windows.net/testcontainer/StorageExplorer.exe'console.log('正在连接……')const { 数据,标题 } = 等待 Axios({网址,方法:'获取',响应类型:'流'})const totalLength = headers['content-length']console.log('开始下载')const progressBar = new ProgressBar('-> 下载 [:bar] :percent :etas', {宽度:40,完整:'=',不完整:' ',渲染节流阀:1,总计:parseInt(totalLength)})const writer = Fs.createWriteStream(Path.resolve(__dirname, 'software', 'StorageExplorer.exe'))data.on('data', (chunk) =>progressBar.tick(chunk.length))数据管道(写入器)}下载()
I am using axios
to download a file (~100MB) from an Azure Storage Blob.
axios({
method: 'get',
url: uri,
onDownloadProgress: (progressEvent) => {
console.log("Loaded: " + ((progressEvent.loaded / progressEvent.total) * 100) + "%");
},
responseType: 'arraybuffer'
}).then({})
My problem is that it is taking quite a while to actually download the file (~10 minutes). I was previously using fetch() which was slower than this even (~15-20 minutes). Are there any recommendations you guys have on how to speed up the download? My internet speed isn't the problem, as downloading the file directly or using the Azure Storage Explorer(1.12.0, AzCopy 10.3.3) takes less than 2 minutes.
I also tried using azure-storage's blobServiceClient, but got similar speeds to axios and fetch (roughly 15kbps).
This is in a React app if that helps.
I have test the speed of download. I hope my result can useful to you.
- I upload
StorageExplorer.exe
as source file for download test. The size of file is 92.5M.
- Download file by
Azure Storage Explore
, it will took1 minute and 07 seconds
.
- Download file in portal by chrome broswer, it will took
58 seconds
.
- Download file by my test code.
① Copy Url from portal
or Storage Explore
.
The url like :https://p*****ge.blob.core.windows.net/testcontainer/StorageExplorer.exe
After test by my code, it will took 1 minute and 52 seconds
and it is very unstable, sometimes the test download time will be longer.
② Copy Url from AzCopy Command
.
The url format look like: https://pan********ge.blob.core.windows.net/testcontainer/StorageExplorer.exe?se=2020-09-18T07%3A55%3A28Z&sp=rl&sv=2018-03-28&sr=c&sig=5kJyTBwHHRS******mlj3%2FWj9CmvQriXCMi4%3D
After test by same code, it will took 1 minute and 02 seconds
.
My test conclusion:
Don't use url which look like https://p*****ge.blob.core.windows.net/testcontainer/StorageExplorer.exe
.
You can use url which look like get from AzCopy Command.
Below is my test code.
- npm i progress
- npm i axios
'use strict'
const Fs = require('fs')
const Path = require('path')
const Axios = require('axios')
const ProgressBar = require('progress')
async function download () {
const url = 'https://pan*****e.blob.core.windows.net/testcontainer/StorageExplorer.exe'
console.log('Connecting …')
const { data, headers } = await Axios({
url,
method: 'GET',
responseType: 'stream'
})
const totalLength = headers['content-length']
console.log('Starting download')
const progressBar = new ProgressBar('-> downloading [:bar] :percent :etas', {
width: 40,
complete: '=',
incomplete: ' ',
renderThrottle: 1,
total: parseInt(totalLength)
})
const writer = Fs.createWriteStream(
Path.resolve(__dirname, 'software', 'StorageExplorer.exe')
)
data.on('data', (chunk) => progressBar.tick(chunk.length))
data.pipe(writer)
}
download()
这篇关于提高 axios 获取下载速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!