C
C#14mo ago
SwaggerLife

❔ Upload large files to azure storage!

Does anyone have a method that uploads large files to azure storage efficiently? I'm using
Azure.Storage.Blobs
Azure.Storage.Blobs
Here is the current method
public async Task<Uri?> UploadFileAsync(string containerName, string fileName, Stream stream,
CancellationToken cancellationToken)
{
try
{
var containerClient = _blobService.GetBlobContainerClient(containerName);
await containerClient.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
var blobClient = containerClient.GetBlockBlobClient(fileName);

var options = new BlobUploadOptions
{
TransferOptions = new StorageTransferOptions
{
MaximumConcurrency = 16, // Adjust the concurrency level as needed
MaximumTransferSize = 4 * 1024 * 1024, // 4MB chunk size
},
};

await blobClient.UploadAsync(stream, options, cancellationToken);
return blobClient.Uri;
}
catch (Exception ex)
{
return null;
}
}
public async Task<Uri?> UploadFileAsync(string containerName, string fileName, Stream stream,
CancellationToken cancellationToken)
{
try
{
var containerClient = _blobService.GetBlobContainerClient(containerName);
await containerClient.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
var blobClient = containerClient.GetBlockBlobClient(fileName);

var options = new BlobUploadOptions
{
TransferOptions = new StorageTransferOptions
{
MaximumConcurrency = 16, // Adjust the concurrency level as needed
MaximumTransferSize = 4 * 1024 * 1024, // 4MB chunk size
},
};

await blobClient.UploadAsync(stream, options, cancellationToken);
return blobClient.Uri;
}
catch (Exception ex)
{
return null;
}
}
3 Replies
Henkypenky
Henkypenky14mo ago
it looks fine blob uploading is slow
OneDeveloper
OneDeveloper14mo ago
This should work for large files as well and you can get the progress also from IProgress that UploadAsync() uses. Are you facing any issue??
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.