简体   繁体   中英

Upload files to blob storage via Azure Front Door

The app is set up on multiple on-premise services and uploads regularly some files to Azure Blob Storage placed in East US. But now it's necessary to place an instance of the app in the Australian region. As a result, upload time to the cloud increased drastically. I have tested if Azure Front Door can help to improve it and I found that download from blob storage works 5 times faster if I use the Azure Front Door link. Now I struggle to change C# code to upload files via Azure Front Door. I have tried to use the suffix "azurefd.net" instead of "core.windows.net" in the connection string but it does not help. Could somebody give me a hint on how to upload files to Azure blob storage via Azure Front Door in C#?

As the Storage connection string uses only storage endpoint (core.windows.net), we cannot use front door endpoint (azurefd.net) in the connection string.

I integrated Azure Storage Account with Front Door. I am able to access the Blob Files in the Azure Storage Account with Front Door URL.

We cannot upload files to Blob Storage via Azure Front Door using C#

This is because Azure Storage in C# accepts connection string only from storage endpoint

Unfortunately, for upload, Azure Front Door does not provide any benefit. I used PUT requests for the test described here: 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

and compared times for storage account and Azure Front account. There is no difference in speed for upload.

Code that I used for test:

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;
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM