services | platforms | author |
---|---|---|
blobs |
c# |
msonecode |
This sample demonstrates how to store temp files by Azure Blob storage in Azure applications
This sample demonstrates how to store temp file in Azure applications.
We have two solutions:
- First one is the traditional way of calling ‘Path.GetTempPath()’ to store temp file in all windows platform.
- The other one is specific for Azure by using blob object in Azure to simulate temp file.
- Double click CSAzureTempFiles.sln file to open this sample solution by using Microsoft Visual Studio 2012 or the later version(s).
- Restore nugget packages in the solution
- Copy account name, access key and address of your azure storage account.
- Configure necessary parameters about account name, key and address in the solution in file Controllers\AzureBlobController.cs
You can upload the sample to your Azure storage, or run at your local IIS, or just debug it in your Visual Studio.
Solution1:
private void SaveTempFile(HttpPostedFileBase file)
{
//get the uploaded file name
string fileName = file.FileName;
//get temp directory path
string tempPath = Path.GetTempPath();
//init the file path
string filePath = tempPath + fileName;
//if the path is exists,delete old file
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
//and then save new file
file.SaveAs(filePath);
}
Solution 2:
private async Task SaveTempFile(string fileName, long contentLenght, Stream inputStream)
{
try
{
//firstly, we need check the container if exists or not. And if not, we need to create one.
await container.CreateIfNotExistsAsync();
//init a blobReference
CloudBlockBlob tempFileBlob = container.GetBlockBlobReference(fileName);
//if the blobReference is exists, delete the old blob
tempFileBlob.DeleteIfExists();
//check the count of blob if over limit or not, if yes, clear them.
await CleanStorageIfReachLimit(contentLenght);
//and upload the new file in this
tempFileBlob.UploadFromStream(inputStream);
}
catch (Exception ex)
{
if (ex.InnerException != null)
{
throw ex.InnerException;
}
else
{
throw ex;
}
}
}
//check the count of blob if over limit or not, if yes, clear them.
private async Task CleanStorageIfReachLimit(long newFileLength)
{
List<CloudBlob> blobs = container.ListBlobs()
.OfType<CloudBlob>()
.OrderBy(m => m.Properties.LastModified)
.ToList();
//get total size of all blobs.
long totalSize = blobs.Sum(m => m.Properties.Length);
//calculate out the real limit size of before upload
long realLimetSize = TotalLimitSizeOfTempFiles - newFileLength;
//delete all,when the free size is enough, break this loop,and stop delete blob anymore
foreach (CloudBlob item in blobs)
{
if (totalSize <= realLimetSize)
{
break;
}
await item.DeleteIfExistsAsync();
totalSize -= item.Properties.Length;
}
}
- Microsoft Azure storage documents: https://azure.microsoft.com/en-us/documentation/services/storage