Responsibility for cleaning files in Azure Web App

In one of the Azure Web App web application API applications, I create temporary files using this code in the Get method

string path = Path.GetTempFileName(); // do some writing on this file. then read var fileStream = File.OpenRead(path); // then returning this stream as a HttpResponseMessage response 

My question is that in a managed environment like this (not in a virtual machine), do I need to clear these temporary files myself? Should Lazur not free these temporary files?

+6
source share
2 answers

These files are cleared only after restarting your site.

If your site is in Free or Shared mode, it only gets 300 MB for temp files, so you can end if you don’t clear it.

If your site is in basic or standard mode, then there is significantly more space (about 200 GB!). Thus, you could probably leave without cleansing, without encountering a limit. In the end, your site will be restarted (for example, during the platform update), so everything will be cleared.

See this page for more information on this topic.

+7
source

The following example shows how to save the temp file in the azure region of both Path and Bolb.

Doc here: https://code.msdn.microsoft.com/How-to-store-temp-files-in-d33bbb10
Code click here: https://github.com/Azure-Samples/storage-blob-dotnet-store-temp-files/archive/master.zip

Within the framework of the part is the basic logic of the boxing code:

 private long TotalLimitSizeOfTempFiles = 100 * 1024 * 1024; private async Task SaveTempFile(string fileName, long contentLenght, Stream inputStream) { try { await container.CreateIfNotExistsAsync(); CloudBlockBlob tempFileBlob = container.GetBlockBlobReference(fileName); tempFileBlob.DeleteIfExists(); await CleanStorageIfReachLimit(contentLenght); tempFileBlob.UploadFromStream(inputStream); } catch (Exception ex) { if (ex.InnerException != null) { throw ex.InnerException; } else { throw ex; } } } private async Task CleanStorageIfReachLimit(long newFileLength) { List<CloudBlob> blobs = container.ListBlobs() .OfType<CloudBlob>() .OrderBy(m => m.Properties.LastModified) .ToList(); long totalSize = blobs.Sum(m => m.Properties.Length); long realLimetSize = TotalLimitSizeOfTempFiles - newFileLength; foreach (CloudBlob item in blobs) { if (totalSize <= realLimetSize) { break; } await item.DeleteIfExistsAsync(); totalSize -= item.Properties.Length; } } 
0
source

All Articles