上传到 blob 容器后文件大小错误
Wrong size of the file after upload to the blob container
我已将 100 GB 的文本文件上传到 azure blob。这个文件在我的硬盘上有 107,374,182,400 字节的大小。上传后它出现在 Azure 门户大小:3,13 GiB,内容类型:application/octet-stream,类型:块 blob。
当我直接从 Azure protal(按钮下载)下载此文件时,我得到一个大小为 3.12 GB(3,355,443,200 字节)的文件。当然,这不是想要的结果。
我在转移时做错了什么?
这是我的源代码:
public void UploadStreamAsync(string file)
{
var fileInfo = new FileInfo(file);
CloudBlockBlob myBlob = _container.GetBlockBlobReference(fileInfo.Name);
var blockSize = 32 * 1024 * 1024;
myBlob.StreamWriteSizeInBytes = blockSize;
var fileName = fileInfo.FullName;
long bytesToUpload = fileInfo.Length;
long fileSize = bytesToUpload;
if (bytesToUpload < blockSize)
{
CancellationToken ca = new CancellationToken();
var ado = myBlob.UploadFromFileAsync(fileName);
Console.WriteLine(ado.Status); //Does Not Help Much
ado.ContinueWith(t =>
{
_logger.LogInformation("Status = " + t.Status);
_logger.LogInformation("It is over"); //this is working OK
});
}
else
{
List<string> blockIds = new List<string>();
int index = 1;
long startPosition = 0;
long bytesUploaded = 0;
do
{
var bytesToRead = Math.Min(blockSize, bytesToUpload);
var blobContents = new byte[bytesToRead];
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
fs.Position = startPosition;
fs.Read(blobContents, 0, (int)bytesToRead);
}
ManualResetEvent mre = new ManualResetEvent(false);
var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(index.ToString("d6")));
Console.WriteLine("Now uploading block # " + index.ToString("d6"));
blockIds.Add(blockId);
var ado = myBlob.PutBlockAsync(blockId, new MemoryStream(blobContents), null);
ado.ContinueWith(t =>
{
bytesUploaded += bytesToRead;
bytesToUpload -= bytesToRead;
startPosition += bytesToRead;
index++;
double percentComplete = (double)bytesUploaded / (double)fileSize;
Console.WriteLine("Percent complete = " + percentComplete.ToString("P"));
_logger.LogInformation("Percent complete = " + percentComplete.ToString("P"));
mre.Set();
});
mre.WaitOne();
}
while (bytesToUpload > 0);
Console.WriteLine("Now committing block list");
_logger.LogInformation("Now committing block list");
foreach (var blockId in blockIds)
{
_logger.LogInformation("BlockId: " + blockId);
}
var pbl = myBlob.PutBlockListAsync(blockIds);
pbl.ContinueWith(t =>
{
Console.WriteLine("Blob uploaded completely.");
_logger.LogInformation("Blob uploaded completely.");
});
}
}
感谢@skeferstat 的评论。将其转换为答案。
添加缺失的内容类型已解决问题。
我已将 100 GB 的文本文件上传到 azure blob。这个文件在我的硬盘上有 107,374,182,400 字节的大小。上传后它出现在 Azure 门户大小:3,13 GiB,内容类型:application/octet-stream,类型:块 blob。 当我直接从 Azure protal(按钮下载)下载此文件时,我得到一个大小为 3.12 GB(3,355,443,200 字节)的文件。当然,这不是想要的结果。 我在转移时做错了什么?
这是我的源代码:
public void UploadStreamAsync(string file)
{
var fileInfo = new FileInfo(file);
CloudBlockBlob myBlob = _container.GetBlockBlobReference(fileInfo.Name);
var blockSize = 32 * 1024 * 1024;
myBlob.StreamWriteSizeInBytes = blockSize;
var fileName = fileInfo.FullName;
long bytesToUpload = fileInfo.Length;
long fileSize = bytesToUpload;
if (bytesToUpload < blockSize)
{
CancellationToken ca = new CancellationToken();
var ado = myBlob.UploadFromFileAsync(fileName);
Console.WriteLine(ado.Status); //Does Not Help Much
ado.ContinueWith(t =>
{
_logger.LogInformation("Status = " + t.Status);
_logger.LogInformation("It is over"); //this is working OK
});
}
else
{
List<string> blockIds = new List<string>();
int index = 1;
long startPosition = 0;
long bytesUploaded = 0;
do
{
var bytesToRead = Math.Min(blockSize, bytesToUpload);
var blobContents = new byte[bytesToRead];
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
fs.Position = startPosition;
fs.Read(blobContents, 0, (int)bytesToRead);
}
ManualResetEvent mre = new ManualResetEvent(false);
var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(index.ToString("d6")));
Console.WriteLine("Now uploading block # " + index.ToString("d6"));
blockIds.Add(blockId);
var ado = myBlob.PutBlockAsync(blockId, new MemoryStream(blobContents), null);
ado.ContinueWith(t =>
{
bytesUploaded += bytesToRead;
bytesToUpload -= bytesToRead;
startPosition += bytesToRead;
index++;
double percentComplete = (double)bytesUploaded / (double)fileSize;
Console.WriteLine("Percent complete = " + percentComplete.ToString("P"));
_logger.LogInformation("Percent complete = " + percentComplete.ToString("P"));
mre.Set();
});
mre.WaitOne();
}
while (bytesToUpload > 0);
Console.WriteLine("Now committing block list");
_logger.LogInformation("Now committing block list");
foreach (var blockId in blockIds)
{
_logger.LogInformation("BlockId: " + blockId);
}
var pbl = myBlob.PutBlockListAsync(blockIds);
pbl.ContinueWith(t =>
{
Console.WriteLine("Blob uploaded completely.");
_logger.LogInformation("Blob uploaded completely.");
});
}
}
感谢@skeferstat 的评论。将其转换为答案。
添加缺失的内容类型已解决问题。