0

The issue happens when I upload a file to one blob (Blob1) which in turn runs a background compression service. The background service streams the file from Blob1, compresses it, and stores it as a zip file in a separate blob (Blob2) to cache for the user to download.

The process works without issue for files < 2GB, but throws a Micrososft.Azure.Storage.StorageException when the file size is > 2GB.

using Microsoft.Azure.Storage.Blob 11.2.2

Sample Code

    public async void DoWork(CancellationToken cancellationToken)
    {
    while (!cancellationToken.IsCancellationRequested)
    {
        await _messageSemaphore.WaitAsync(cancellationToken);
        MyModel model = await _queue.PeekMessage();
        if(model != null)
        {
            try
            {
                //Get CloudBlockBlob zip blob reference
                var zipBlockBlob = await _storageAccount.GetFileBlobReference(_configuration[ConfigKeys.ContainerName], model.Filename, model.FileRelativePath);

                using (var zipStream = zipBlockBlob.OpenWrite()) //Opens zipstream
                {
                    using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, false)) // Create new ZipArchive
                    {
                        //Add each file to the zip archive
                        foreach (var fileUri in Files)
                        {
                            var file = new Uri(fileUri);
                            var cloudBlockBlob = blobContainer.GetBlockBlobReference(file);

                            using (var blobStream = cloudBlockBlob.OpenRead())//Opens read stream
                            {
                                //Create new ZipEntry
                                var zipEntry = archive.CreateEntry(model.Filename, CompressionLevel.Fastest);

                                using (var zipEntryStream = zipEntry.Open())
                                {
                                    //Zip file
                                    blobStream.CopyTo(zipEntryStream);
                                }
                            }
                        }
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine($"Download Error: {e.Message}");
            }
            catch (Microsoft.Azure.Storage.StorageException e) // "
            {
                Console.WriteLine($"Storage Exception Error: {e.Message}");
            }
        }
        else
        {
            _messageSemaphore.Release();
            // Wait for 1 minute between polls when idle
            await Task.Delay(_sleepTime);
        }
        
    }
}
Hwa
  • 23
  • 5
  • I think my issue may be that multiple streams from multiple instances of the application are trying to write the zip file to blob storage at the same time. Is there a way to determine if a blob stream write with a specific name or endpoint is currently in progress? I saw [this post](https://stackoverflow.com/questions/44395881/how-to-check-whether-azure-blob-storage-upload-was-successful) regarding a similar issue. – Hwa Aug 27 '21 at 16:47

1 Answers1

0

The problem was with concurrency and having multiple application instances writing to the same blob at the same time. To get around this I ended up implementing an Azure blob lease on the blob being created but had to create a temp file to apply the lease to. This is a decent enough work around for now, but this could / should probably be implemented using an Azure event driven service.

Hwa
  • 23
  • 5