我在blob存儲中創建了一個2GB文件,並且正在構建一個將該文件下載到桌面的控制檯應用程序。要求是分割成100MB的塊並在文件名中附加一個數字。我不需要重新組合這些文件。我需要的只是文件塊。在blob存儲中下載大塊文件並將其分割爲100 MB塊
我公司目前擁有從Azure download blob part
這個代碼,但我無法弄清楚如何停止下載時,文件大小爲100MB已經和創建一個新的。
任何幫助將不勝感激。
更新:這裏是我的代碼
CloudStorageAccount account = CloudStorageAccount.Parse(connectionString);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var file = uri;
var blob = container.GetBlockBlobReference(file);
//First fetch the size of the blob. We use this to create an empty file with size = blob's size
blob.FetchAttributes();
var blobSize = blob.Properties.Length;
long blockSize = (1 * 1024 * 1024);//1 MB chunk;
blockSize = Math.Min(blobSize, blockSize);
//Create an empty file of blob size
using (FileStream fs = new FileStream(file, FileMode.Create))//Create empty file.
{
fs.SetLength(blobSize);//Set its size
}
var blobRequestOptions = new BlobRequestOptions
{
RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(5), 3),
MaximumExecutionTime = TimeSpan.FromMinutes(60),
ServerTimeout = TimeSpan.FromMinutes(60)
};
long startPosition = 0;
long currentPointer = 0;
long bytesRemaining = blobSize;
do
{
var bytesToFetch = Math.Min(blockSize, bytesRemaining);
using (MemoryStream ms = new MemoryStream())
{
//Download range (by default 1 MB)
blob.DownloadRangeToStream(ms, currentPointer, bytesToFetch, null, blobRequestOptions);
ms.Position = 0;
var contents = ms.ToArray();
using (var fs = new FileStream(file, FileMode.Open))//Open that file
{
fs.Position = currentPointer;//Move the cursor to the end of file.
fs.Write(contents, 0, contents.Length);//Write the contents to the end of file.
}
startPosition += blockSize;
currentPointer += contents.Length;//Update pointer
bytesRemaining -= contents.Length;//Update bytes to fetch
Console.WriteLine(fileName + dateTimeStamp + ".csv " + (startPosition/1024/1024) + "/" + (blob.Properties.Length/1024/1024) + " MB downloaded...");
}
}
while (bytesRemaining > 0);
你能分享你的實際代碼嗎? –
按要求添加代碼。 – Joseph
你解決了這個問題嗎,你需要進一步的幫助嗎? –