azure - 文件开始流式传输到浏览器后,如何清理 Azure 本地存储中的文件和目录?

标签 azure asynchronous local-storage filestream httpresponse

背景:我正在使用 Azure 本地存储。这应该被视为“ volatile ”存储。首先,我创建的文件和目录在 Web 角色实例上保留多长时间(在我的例子中有 2 个)?如果在每个用户使用完这些文件/目录后不进行清理,我是否需要担心存储空间不足?我正在做的是从单独的服务中提取多个文件,将它们存储在 Azure 本地存储中,将它们压缩为 zip 文件并存储该 zip 文件,最后将该 zip 文件流式传输到浏览器。

问题:除了一个小问题之外,一切都很顺利。该文件似乎异步流式传输到浏览器。因此,当我随后尝试从 azure 本地存储中删除压缩文件时,会引发异常,因为它仍在流式传输到浏览器的过程中。在文件完全传输到浏览器后强制执行删除过程的最佳方法是什么?

这是我的代码:

                using (Service.Company.ServiceProvider CONNECT = new eZ.Service.CompanyConnect.ServiceProvider())
            {
                // Iterate through all of the files chosen
                foreach (Uri fileId in fileIds)
                {
                    // Get the int file id value from the uri
                    System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"e[B|b]://[^\/]*/\d*/(\d*)");
                    string id_str = rex.Match(fileId.ToString()).Groups[1].Value;
                    int id = int.Parse(id_str);

                    // Get the file object from eB service from the file id passed in
                    eZ.Data.File f = new eZ.Data.File(CONNECT.eZSession, id);
                    f.Retrieve("Header; Repositories");

                    string _fileName = f.Name;

                    try
                    {
                        using (MemoryStream stream = new MemoryStream())
                        {
                            f.ContentData = new eZ.ContentData.File(f, stream);

                            // After the ContentData is created, hook into the event
                            f.ContentData.TransferProgressed += (sender, e) => { Console.WriteLine(e.Percentage); };

                            // Now do the transfer, the event will fire as blocks of data is read
                            int bytesRead;
                            f.ContentData.OpenRead();
                            // Open the Azure Local Storage file stream
                            using (azure_file_stream = File.OpenWrite(curr_user_path + _fileName))
                            {
                                while ((bytesRead = f.ContentData.Read()) > 0)
                                {
                                    // Write the chunk to azure local storage
                                    byte[] buffer = stream.GetBuffer();
                                    azure_file_stream.Write(buffer, 0, bytesRead);
                                    stream.Position = 0;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                        //Console.WriteLine("The following error occurred:  " + e);
                    }
                    finally
                    {
                        f.ContentData.Close();
                    }
                } // end of foreach block

            } // end of eB using block

            string sevenZipDllPath = Path.Combine(Utilities.GetCurrentAssemblyPath(), "7z.dll");
            Global.logger.Info(string.Format("sevenZipDllPath: {0}", sevenZipDllPath));
            SevenZipCompressor.SetLibraryPath(sevenZipDllPath);

            var compressor = new SevenZipCompressor
            {
                ArchiveFormat = OutArchiveFormat.Zip,
                CompressionLevel = CompressionLevel.Fast
            };

            // Compress the user directory
            compressor.CompressDirectory(webRoleAzureStorage.RootPath + curr_user_directory, curr_user_package_path + "Package.zip");

            // stream Package.zip to the browser
            httpResponse.BufferOutput = false;
            httpResponse.ContentType = Utilities.GetMIMEType("BigStuff3.mp4");
            httpResponse.AppendHeader("content-disposition", "attachment; filename=Package.zip");

            azure_file_stream = File.OpenRead(curr_user_package_path + "Package.zip");
            azure_file_stream.CopyTo(httpResponse.OutputStream);
            httpResponse.End();

            // Azure Local Storage cleanup
            foreach (FileInfo file in user_directory.GetFiles())
            {
                file.Delete();
            }
            foreach (FileInfo file in package_directory.GetFiles())
            {
                file.Delete();
            }
            user_directory.Delete();
            package_directory.Delete();
        }

最佳答案

您能否简单地在计算机上运行一项作业,在文件创建一天后清理文件?这可以像任务计划程序中的批处理文件或从 WebRole.cs 启动的单独线程一样简单。 您甚至可以使用AzureWatch如果本地空间低于某个阈值,则自动重新镜像您的实例

关于azure - 文件开始流式传输到浏览器后,如何清理 Azure 本地存储中的文件和目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17844121/

相关文章:

azure - 将 Bearer token 与 azure-sdk-for-js 一起使用

azure - 使用 Azure AD 应用程序 (OAuth2) 访问 Microsoft API

c# - async/await WhenAll 在两个具有相同返回类型的方法上

jquery - 本地存储还是其他?

javascript - 存储用户之前是否按下过按钮

javascript - 是否可以使用 xcode 中的 JavaScript 将文件(例如 pdf)写入 iPad(本地)?

azure - 如何避免重启 Azure VM 时临时文件夹被清理?

c# - 有没有办法在 Azure.Storage.Blobs 中获取 blob 的 ContentType?

asynchronous - 微服务客户端确认和事件溯源

node.js - Node Redis 异步