azure - 限制azure blob上传速度的方法

标签 azure azure-storage azure-blob-storage

我有一个应用程序,可将数据库备份上传到 azure 上的 blob 容器。

我正在上传大小约为 8GB 的​​文件,平均速率为 11Mbps

一切都工作正常,但是,当 blob 被传输到容器时,它绝对会杀死该网络上的其他所有内容。

所以我的问题是,有什么方法可以限制 Azure 的上传速度吗?

最佳答案

您的问题没有指定您希望使用哪种编程语言(或原始 REST)来实现上传速度限制。但我找到了一种使用官方 azure storage .NET SDK (Microsoft.WindowsAzure.Storage) 来执行此操作的方法。

SDK 本身不提供显式的速度限制。但是,Upload*Async() 函数(例如 UploadFromFileAsync())确实支持进度报告,通过采用 IProgress<StorageProgress> 实现并定期调用其 Report() 方法。 看起来它是同步调用的。

因此我们可以在 Report() 中添加延迟来限制上传速度。更好的是, Report 为您提供有关 BytesTransferred 的信息。因此,如果您的进度处理程序跟踪持续时间。然后您可以估计当前的上传速度,并使用它来精确限制。

下面是这个想法的说明。请注意,这只是该想法的演示。我用了不到 3 分钟就把它拼凑起来。速率估计和节流算法相当粗糙并且没有经过充分测试。

以下是如何使用它

await blob.UploadFromFileAsync(
    @"some_file.dat", 
    null, null, null, 
    new RateThrottleProgress(300 * 1024), // throttle at 300kb/s
    CancellationToken.None);

指数回退节流器实现

class RateThrottleProgress : IProgress<StorageProgress>
{
    private readonly DateTime start = DateTime.Now;
    private readonly long maxbps;
    private long baseDelay, delay;

    public RateThrottleProgress(long maxbps)
    {
        this.maxbps = maxbps;
        baseDelay = 10;
        delay = baseDelay;
    }

    public void Report(StorageProgress value)
    {
        double duration = (DateTime.Now - start).TotalSeconds;
        double bps = value.BytesTransferred / duration;
        if (bps > maxbps) delay *= 2;
        else delay = Math.Max(baseDelay, delay/2);
        Console.WriteLine($"current estimated upload speed: {bps / 1024.0} KB/s. delay: {delay} ms");
        Thread.Sleep(TimeSpan.FromMilliseconds(delay));
    }
}

我还 put below code snippet as a gist 以获得更好的版本控制和协作。

节流有效:

current estimated upload speed: 287.486007463505 KB/s. delay: 10 ms
current estimated upload speed: 290.086402388889 KB/s. delay: 10 ms
current estimated upload speed: 292.685419108659 KB/s. delay: 10 ms
current estimated upload speed: 295.28201245662 KB/s. delay: 10 ms
current estimated upload speed: 297.876060423937 KB/s. delay: 10 ms
current estimated upload speed: 300.469027029562 KB/s. delay: 20 ms
current estimated upload speed: 302.927815243916 KB/s. delay: 40 ms
current estimated upload speed: 305.112558483135 KB/s. delay: 80 ms
current estimated upload speed: 306.778888691779 KB/s. delay: 160 ms
current estimated upload speed: 307.367196107083 KB/s. delay: 320 ms   <-- speed starts to drop from here...
current estimated upload speed: 305.910611140488 KB/s. delay: 640 ms
current estimated upload speed: 300.564767027164 KB/s. delay: 1280 ms
current estimated upload speed: 288.206861583389 KB/s. delay: 640 ms
current estimated upload speed: 283.672713628354 KB/s. delay: 320 ms
current estimated upload speed: 282.668039190231 KB/s. delay: 160 ms
current estimated upload speed: 283.351226090087 KB/s. delay: 80 ms
current estimated upload speed: 284.861107569046 KB/s. delay: 40 ms
current estimated upload speed: 286.781960850501 KB/s. delay: 20 ms
current estimated upload speed: 288.910675693183 KB/s. delay: 10 ms
current estimated upload speed: 291.140146046991 KB/s. delay: 10 ms
current estimated upload speed: 293.358817316007 KB/s. delay: 10 ms

关于azure - 限制azure blob上传速度的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38978777/

相关文章:

azure - 如何从Azure应用配置中读取值

azure - Microsoft.WindowsAzure.Storage.dll 中发生类型 'System.StackOverflowException' 的未处理异常

azure - 使用 azure blob 存储的 mp4 流媒体问题

Azure 流分析 GetMetadataPropertyValue 不起作用

azure - 添加本地计算机作为 Azure Function Worker

azure - 如何执行DocumentDB中现有的存储过程?

c# - 当使用当前时间格式化 DateTime 时,为什么会得到 2555 年?

azure - 运行 SSIS 包时出现 "The RPC server is unavailable"错误

c# - 如何从 MemoryStream 重新创建 Zip 文件

azure - 格式化 Azure SDK 的 golang 时间