C#C
C#3y ago
v0fbu1vm

❔ 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
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;
        }
    }
Was this page helpful?