【问题标题】:Upload files to blob storage via Azure Front Door通过 Azure Front Door 将文件上传到 Blob 存储
【发布时间】:2022-01-12 11:16:20
【问题描述】:

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

【问题讨论】:

    标签: c# azure azure-blob-storage azure-front-door


    【解决方案1】:

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

    我将 Azure 存储帐户与 Front Door 集成。我可以使用 Front Door URL 访问 Azure 存储帐户中的 Blob 文件。

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

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

    【讨论】:

      【解决方案2】:

      很遗憾,对于上传,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;
              }
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-24
        • 2017-08-19
        • 2022-01-12
        • 1970-01-01
        • 2020-03-02
        • 2019-07-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多