I am currently downloading file from FTP and uploading to Azure. I am using this link for uploading large files to Azure (using BlockBlob).
private async Task UploadFileAsBlockBlob(Stream stream, BlobStorageConfiguration blobStorageConfiguration, string fileName)
{
try
{
var storageAccount = CloudStorageAccount.Parse(blobStorageConfiguration.ConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var cloudContainer = blobClient.GetContainerReference(blobStorageConfiguration.Container);
await cloudContainer.CreateIfNotExistsAsync().ConfigureAwait(false);
var storageDirectory = cloudContainer.GetDirectoryReference(blobStorageConfiguration.Path);
var blob = storageDirectory.GetBlockBlobReference(fileName);
const long pageSizeInBytes = 1048576 * 10; // 10mb at a time
var blocklist = new HashSet<string>();
if (!stream.CanRead)
{
var error = $"Error while reading file from ftp {fileName}";
return;
}
long prevLastByte = 0;
long bytesRemain = stream.Length;
do
{
long bytesToCopy = Math.Min(bytesRemain, pageSizeInBytes);
byte[] bytesToSend = new byte[bytesToCopy];
// some issue might occur for very large files (above 2 gb)
// overflow exception will occur.
stream.Read(bytesToSend, 0, (int)bytesToCopy);
prevLastByte += bytesToCopy;
bytesRemain -= bytesToCopy;
// create blockId
string blockId = Guid.NewGuid().ToString();
string base64BlockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(blockId));
await blob.PutBlockAsync(base64BlockId, new MemoryStream(bytesToSend, true), null).ConfigureAwait(false);
blocklist.Add(base64BlockId);
}
while (bytesRemain > 0);
// post blocklist
await blob.PutBlockListAsync(blocklist).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogCritical(ex, "Error while importing Files");
}
}
I am using Stream from ftp. 180Mb file takes 25 mins. Any faster approach suggested to speed up?