Azure Web App Temp file cleaning responsibility

烈酒焚心 提交于 2020-04-10 07:37:11

问题


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();
    // do some writing on this file. then read 
    var fileStream = File.OpenRead(path);
    // then returning this stream as a HttpResponseMessage response

My question is, in a managed environment like this (not in VM), do I need to clear those temporary files by myself? Shouldn't Azure itself supposed to clear those temp files?


回答1:


Those files only get cleaned when your site is restarted.

If your site is running in Free or Shared mode, it only gets 300MB for temp files, so you could run out if you don't clean up.

If your site is in Basic or Standard mode, then there is significantly more space (around 200GB!). So you could probably get away with not cleaning up without running into the limit. Eventually, your site will get restarted (e.g. during platform upgrade), so things will get cleaned up.

See this page for some additional detail on this topic.




回答2:


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<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;
    }
}



回答3:


Maybey if you extend FileStream you can override dispose and remove it when disposed is called? That is how i'm resolving it for now. If i'm wrong let me know.

 /// <summary>
/// Create a temporary file and removes its when the stream is closed.
/// </summary>
internal class TemporaryFileStream : FileStream
{
    public TemporaryFileStream() : base(Path.GetTempFileName(), FileMode.Open)
    {
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        // After the stream is closed, remove the file.
        File.Delete(Name);
    }
}


来源:https://stackoverflow.com/questions/34247801/azure-web-app-temp-file-cleaning-responsibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!