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



我正在尝试使用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();

相关内容

最新更新