【问题标题】:Azure Web App Temp file cleaning responsibilityAzure Web App 临时文件清理责任
【发布时间】:2016-03-18 19:09:53
【问题描述】:

在我的 Azure Web App Web API 应用程序之一中,我在 Get 方法中使用此代码创建临时文件

    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

我的问题是,在这样的托管环境中(不是在 VM 中),我需要自己清除那些临时文件吗? Azure 本身不应该清除那些临时文件吗?

【问题讨论】:

  • 是的,就像 Windows 应该清理 TEMP 文件夹.. 你知道.. 因为它被称为 TEMP.. 但我不认为微软拥有字典,所以它是永久的垃圾存储。

标签: azure asp.net-web-api garbage-collection azure-web-app-service temporary-files


【解决方案1】:

只有在您的网站重新启动时才会清除这些文件。

如果您的网站在免费或共享模式下运行,它只会获得 300MB 的临时文件,因此如果您不清理,您可能会用完。

如果您的网站处于基本或标准模式,则空间会大得多(大约 200GB!)。因此,您可能会逃脱不清理而不会遇到限制。最终,您的网站将重新启动(例如在平台升级期间),因此一切都会得到清理。

有关此主题的更多详细信息,请参阅 this page。

【讨论】:

  • 所以,如果我每天早上安排一次“重新启动”,那么我就不必担心临时文件。我说得对吗@David-ebbo?
  • 是的,那应该是安全的。
【解决方案2】:

也许如果您扩展 FileStream 您可以覆盖 dispose 并在调用 dispose 时将其删除?这就是我现在解决它的方式。如果我错了,请告诉我。

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

【讨论】:

    【解决方案3】:

    以下示例演示了如何在 azure 中保存临时文件,包括 Path 和 Bolb。

    文档在这里:https://code.msdn.microsoft.com/How-to-store-temp-files-in-d33bbb10
    代码点这里:https://github.com/Azure-Samples/storage-blob-dotnet-store-temp-files/archive/master.zip

    下面部分是bolb代码的核心逻辑:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多