通过 Azure Front Door 将文件上传到 blob 存储

Upload files to blob storage via Azure Front Door

该应用程序设置在多个本地服务上,并定期将一些文件上传到位于美国东部的 Azure Blob 存储。但是现在有必要在澳大利亚地区放置一个应用程序的实例。结果,上传到云端的时间急剧增加。 我已经测试过 Azure Front Door 是否可以帮助改进它,我发现如果我使用 Azure Front Door link,从 blob 存储下载的速度会提高 5 倍。现在我努力更改 C# 代码以通过 Azure Front Door 上传文件。我试图在连接字符串中使用后缀“azurefd.net”而不是“core.windows.net”,但它没有帮助。有人可以提示我如何通过 C# 中的 Azure Front Door 将文件上传到 Azure blob 存储吗?

由于存储连接字符串仅使用存储端点 (core.windows.net),我们不能在连接字符串中使用前门端点 (azurefd.net)。

我将 Azure 存储帐户与 Front Door 集成。我可以使用 Front Door URL.

访问 Azure 存储帐户中的 Blob 文件

我们无法使用 C# 通过 Azure Front Door 将文件上传到 Blob 存储

这是因为 C# 中的 Azure 存储仅接受来自存储终结点的连接字符串

遗憾的是,对于上传,Azure Front Door 不提供任何好处。我用了 PUT 此处描述的测试请求:https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob

PUT https://<entityName>.azurefd.net/<containerName>/<blobName>?<sharedAccessSignature>
x-ms-version: 2020-10-02
x-ms-blob-type: BlockBlob

< C:\Downloads\t1.txt

并比较了存储帐户和 Azure Front 帐户的时间。上传速度没有区别。

我用于测试的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace SandboxV2
{
    class Program
    {
        static async Task Main()
        {
            string frontDoorUrl = "https://<FRONT-DOOR>.azurefd.net";
            string storageUrl = "https://{STORAGE-ACCOUNT}.blob.core.windows.net";
            string sasString = "...";

            Console.Write("File Path: ");
            string filePath = Console.ReadLine();
            await RunUploadTestAsync(filePath, frontDoorUrl, sasString, "-fd");
            await RunUploadTestAsync(filePath, storageUrl, sasString, "-storage");
        }

        private static async Task RunUploadTestAsync(string filePath, string rootUrl, string sasString, string suffix)
        {
            string blobName = Path.GetFileNameWithoutExtension(filePath) + suffix + Path.GetExtension(filePath);
            Console.WriteLine(rootUrl);
            string containerName = "testaccess";
            var speeds = new List<double>();
            var times = new List<TimeSpan>();
            for (int i = 0; i < 5; i++)
            {
                var t1 = DateTime.UtcNow;
                var statusCode = await UploadAsSingleBlock(filePath, rootUrl, blobName, containerName, sasString);
                var t2 = DateTime.UtcNow;

                var time = t2 - t1;
                var speed = new FileInfo(filePath).Length / time.TotalSeconds / 1024 / 1024 * 8;
                speeds.Add(speed);
                times.Add(time);
                Console.WriteLine($"Code: {statusCode}. Time: {time}. Speed: {speed}");
            }
            Console.WriteLine($"Average time: {TimeSpan.FromTicks((long)times.Select(t => t.Ticks).Average())}. Average speed: {speeds.Average()}.");
        }

        private static async Task<HttpStatusCode> UploadAsSingleBlock(string filePath, string rootUrl, string blobName, string containerName, string sasString)
        {
            var request = new HttpRequestMessage(HttpMethod.Put, $"{rootUrl}/{containerName}/{blobName}?{sasString}");
            request.Headers.Add("x-ms-version", "2020-10-02");
            request.Headers.Add("x-ms-blob-type", "BlockBlob");

            HttpResponseMessage response;
            using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                request.Content = new StreamContent(fileStream);
                using (var client = new HttpClient())
                    response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
            }
            return response.StatusCode;
        }

    }
}