简体   繁体   English

Azure 使用 WriteRange 和 MD5 上传文件存储是 KO

[英]Azure File Storage upload with WriteRange and MD5 is KO

I'm trying to upload a file in azure storage with C# REST API library.我正在尝试使用 C# REST ZDB974238714831DE63ZA7ACED 库在 azure 存储中上传文件I want to be able to upload a file with percentage of upload, so I looked in the documentation and try to achieve this with WriteRange method.我希望能够上传具有上传百分比的文件,所以我查看了文档并尝试使用 WriteRange 方法来实现这一点。

It's working but I'm unable to store the MD5 of the file (and to retrieve it later).它正在工作,但我无法存储文件的 MD5(并在以后检索它)。

This is my repro case:这是我的复制案例:

static void Main(string[] args)
{
    var storageAccount = CloudStorageAccount.Parse(connectionString);
    var fileClient = storageAccount.CreateCloudFileClient();
    var share = fileClient.GetShareReference(shareReference);
    var rootDir = share.GetRootDirectoryReference();

    var firstFileCloudName = "test/file1.txt";
    var firstFilePath = "c:\\test\\file1.txt";
    var secondFileCloudName = "test/file2.txt";
    var secondFilePath = "c:\\test\\file2.txt";

    // upload first file
    var firstFile = rootDir.GetFileReference(firstFileCloudName);
    firstFile.UploadFromFile(firstFilePath, options: new FileRequestOptions { StoreFileContentMD5 = true });

    // check md5 of first file
    var checkFirstFile = rootDir.GetFileReference(firstFileCloudName);
    if (checkFirstFile.Exists() && checkFirstFile.Properties.ContentMD5 == Convert.ToBase64String(MD5.Create().ComputeHash(File.ReadAllBytes(firstFilePath))))
    {
        Console.WriteLine("First file OK"); // OK
    }

    // upload second file with chunks
    var secondFile = rootDir.GetFileReference(secondFileCloudName);
    Upload(secondFile, secondFilePath);

    // check md5 of second file
    var checksecondFile = rootDir.GetFileReference(secondFileCloudName);
    if (checksecondFile.Exists() && checksecondFile.Properties.ContentMD5 == Convert.ToBase64String(MD5.Create().ComputeHash(File.ReadAllBytes(secondFilePath))))
    {
        Console.WriteLine("Second file OK"); // KO !!!
    }

    // but the file is correctly uploaded because downloaded md5 is OK
    var downloadedFile = rootDir.GetFileReference(secondFileCloudName);
    var memoryStream = new MemoryStream();
    downloadedFile.DownloadToStream(memoryStream);
    if (Convert.ToBase64String(MD5.Create().ComputeHash(memoryStream.ToArray())) == Convert.ToBase64String(MD5.Create().ComputeHash(File.ReadAllBytes(secondFilePath))))
    {
        Console.WriteLine("Second file downloaded OK"); // KO !!!
    }
}

private static void Upload(CloudFile currentFile, string file)
{
    var options = new FileRequestOptions { StoreFileContentMD5 = true };

    long bytesToUpload = new FileInfo(file).Length;
    long fileSize = bytesToUpload;
    currentFile.Create(fileSize);
    var blockSize = 256 * 1024;
    currentFile.StreamWriteSizeInBytes = blockSize;
            
    int index = 1;
    long startPosition = 0;
    long bytesUploaded = 0;
    var allBytes = File.ReadAllBytes(file);
    var ms = new MemoryStream(allBytes);

    do
    {
        var bytesToRead = Math.Min(blockSize, bytesToUpload);
        var blobContents = new byte[bytesToRead];
        ms.Position = startPosition;
        ms.Read(blobContents, 0, (int)bytesToRead);

        var md5 = Convert.ToBase64String(MD5.Create().ComputeHash(new MemoryStream(blobContents)));
        currentFile.WriteRange(new MemoryStream(blobContents), startPosition, md5, options: options);

        bytesUploaded += bytesToRead;
        bytesToUpload -= bytesToRead;
        startPosition += bytesToRead;
        index++;
        double percentComplete = (double)bytesUploaded / fileSize;
        Console.WriteLine("Percent complete = " + percentComplete.ToString("P"));
    }
    while (bytesToUpload > 0);

    currentFile.SetProperties(options: options);
}

Some explainations:一些解释:

First case, upload with UploadFromFile: it's working and I can store and read the MD5.第一种情况,使用 UploadFromFile 上传:它正在工作,我可以存储和读取 MD5。 (I can see in azure portal that MD5 is correctly stored in properties) (我可以在 azure 门户中看到 MD5 正确存储在属性中)

Second case, with custom upload, the ContentMD5 is null.第二种情况,使用自定义上传,ContentMD5 为 null。 (I can see in azure portal that MD5 is not stored in properties) (我可以在 azure 门户中看到 MD5 未存储在属性中)

But when I download the second file and calculate md5, the file is correct so the upload is OK.但是当我下载第二个文件并计算md5时,文件是正确的,所以上传是可以的。

How can I store the MD5 in azure file with second upload?如何在第二次上传时将 MD5 存储在 azure 文件中? (or change upload way with percentage) (或用百分比更改上传方式)

In fact, at the end of upload, I can set the MD5 manually;其实在上传结束的时候,我可以手动设置MD5; I thought it was get only:我以为它只是得到:

currentFile.Properties.ContentMD5 = md5;
currentFile.SetProperties();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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