使用 C# Azure SDK 将文件上传到 PageBlob 的(非 hacky)方式

(non-hacky) way to upload file to PageBlob using C# Azure SDK

我使用 通过手动分块文件上传到 PageBlob。有用。我将该逻辑翻译成 C#。我想知道 Azure SKD 是否内置了更简单的解决方案?不那么棘手的解决方案。

byte[] bytes = (encoding ?? Encoding.UTF8).GetBytes(content);

int fileSize = bytes.Length;
int blockSize = fileSize;
int boundary = blockSize % 512;
if (boundary != 0)
{
    byte[] padding = Enumerable.Repeat((byte)0x0, 512 - boundary).ToArray();
    bytes = bytes.Concat(padding).ToArray();
    blockSize = blockSize + 512 - boundary;
}

// Allocate pages
cloudPageBlob.Create(
    blockSize,
    accessCondition,
    options,
    operationContext);

var CHUNK_MAX_SIZE = 4 * 1024 * 1024;
var count = (int)Math.Ceiling(1.0 * bytes.Length / CHUNK_MAX_SIZE);

int remaining = bytes.Length;
for (var i = 0; i < count; i++)
{
    int chunkSize = Math.Min(CHUNK_MAX_SIZE, remaining);
    cloudPageBlob.UploadFromByteArray(
        bytes,
        i,
        chunkSize,
        accessCondition,
        options,
        operationContext);

    remaining -= chunkSize;
}

我尝试在我的系统中上传块并稍微修改你的代码,将虚拟字节用于测试目的

using System;
using System.Linq;
using System.Text;


using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Blob;

namespace UploadPageChunck
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("blob uri");
            Console.WriteLine("Hello World!");
            AccessCondition data =null;
            byte[] bytes= { 0x32, 0x00, 0x1E, 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
            //byte[] bytes= { 0x32, 0x00, 0x1E, 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00, 0x00, 0x00, };
            var storage = new StorageCredentials("testsnapshotex", "Storage key ”)
            int fileSize = bytes.Length;
            int blockSize = fileSize;
            int boundary = blockSize % 512;
            if (boundary != 0)
            {
                byte[] padding = Enumerable.Repeat((byte)0x0, 512 - boundary).ToArray();
                bytes = bytes.Concat(padding).ToArray();
                blockSize = blockSize + 512 - boundary;
            }

            // Allocate pages
            CloudPageBlob cloudPageBlob = new CloudPageBlob(uri,storage);
            //cloudPageBlob.Create(
            //    blockSize,
            //    null,
            //    null,
            //    null);

            var CHUNK_MAX_SIZE = 4 * 1024 * 1024;
            var count = (int)Math.Ceiling(1.0 * bytes.Length / CHUNK_MAX_SIZE);

            int remaining = bytes.Length;
            for (var i = 0; i < count; i++)
            {
                int chunkSize = Math.Min(CHUNK_MAX_SIZE, remaining);
                cloudPageBlob.UploadFromByteArray(
                    bytes,
                    i,
                    chunkSize,
                    null,
                    data,
                    null);

                remaining -= chunkSize;
            }
        }
    }
}

输出