如何清理文件&;文件开始流式传输到浏览器后Azure本地存储中的目录?
背景:我正在使用Azure本地存储。这应该被视为“易失性”存储。首先,我创建的文件和目录在Web角色实例上保留多长时间(在我的例子中有2个)?如果在每个用户使用完这些文件/目录后,我不清理这些文件/目录,是否需要担心存储空间不足?我所做的是从一个单独的服务中提取多个文件,将它们存储在Azure本地存储中,将它们压缩成zip文件并存储该zip文件,然后将该zip文件流式传输到浏览器 问题是:除了一个小的打嗝,这一切都运行得很好。文件似乎以异步方式传输到浏览器。因此,发生的情况是,当我尝试在之后从azure本地存储中删除压缩文件时,会引发异常,因为它仍在流式传输到浏览器的过程中。在文件完全流式传输到浏览器后,强制执行删除过程的最佳方法是什么 这是我的密码:如何清理文件&;文件开始流式传输到浏览器后Azure本地存储中的目录?,azure,asynchronous,local-storage,filestream,httpresponse,Azure,Asynchronous,Local Storage,Filestream,Httpresponse,背景:我正在使用Azure本地存储。这应该被视为“易失性”存储。首先,我创建的文件和目录在Web角色实例上保留多长时间(在我的例子中有2个)?如果在每个用户使用完这些文件/目录后,我不清理这些文件/目录,是否需要担心存储空间不足?我所做的是从一个单独的服务中提取多个文件,将它们存储在Azure本地存储中,将它们压缩成zip文件并存储该zip文件,然后将该zip文件流式传输到浏览器 问题是:除了一个小的打嗝,这一切都运行得很好。文件似乎以异步方式传输到浏览器。因此,发生的情况是,当我尝试在之后从a
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启动的单独线程。
如果本地空间降到某个阈值以下,您甚至可以使用自动重新映像实例您可以将文件(特别是用户下载的最终压缩文件)放置在Windows Azure blob存储中吗?该文件可以公开,或者创建共享访问签名,以便只有您提供URL的用户才能下载该文件。将文件放在blob存储中供下载可以减轻web服务器上的一些压力。感谢您的输入。我一定会看看这款AzureWatch。这实际上是首选的长期解决方案。这只是暂时的解决办法。