gpt4 book ai didi

c# - 使用 WriteRange 和 MD5 上传 Azure 文件存储已被淘汰

转载 作者:行者123 更新时间:2023-12-03 00:23:35 25 4
gpt4 key购买 nike

我正在尝试使用 C# REST API 库上传 Azure 存储中的文件。我希望能够上传具有上传百分比的文件,因此我查看了文档并尝试使用 WriteRange 方法来实现此目的。

它可以工作,但我无法存储文件的 MD5(以及稍后检索它)。

这是我的重现案例:

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

一些解释:

第一种情况,使用 UploadFromFile 上传:它正在工作,我可以存储和读取 MD5。 (我可以在azure门户中看到MD5正确存储在属性中)

第二种情况,自定义上传时,ContentMD5 为空。 (我可以在azure门户中看到MD5未存储在属性中)

但是当我下载第二个文件并计算md5时,该文件是正确的,因此上传可以。

如何将 MD5 存储在 Azure 文件中并进行第二次上传? (或用百分比更改上传方式)

最佳答案

其实上传结束后,我可以手动设置MD5;我以为这只是得到:

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

关于c# - 使用 WriteRange 和 MD5 上传 Azure 文件存储已被淘汰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63279035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com