Azure Web App Temp file cleaning responsibility

后端 未结 3 582
你的背包
你的背包 2021-01-11 15:31

In one of my Azure Web App Web API application, I am creating temp files using this code in a Get method

    string path = Path.GetTempFileName();
    // d         


        
3条回答
  •  忘掉有多难
    2021-01-11 16:29

    Following sample demonstrate how to save temp file in azure, both Path and Bolb.

    Doc is 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

    Under part is the core logic of bolb 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 blobs = container.ListBlobs()
            .OfType()
            .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;
        }
    }
    

提交回复
热议问题