/// <summary> /// Saves to blob storage /// NOTE: If you plan on getting the same blob over and over and quickly saving you will need to throttle and retry /// </summary> /// <param name="blobContainer"></param> /// <param name="fileName"></param> /// <param name="memoryStream"></param> public void SaveBlob(string blobContainer, string fileName, MemoryStream memoryStream) { InitializeStorage(blobContainer); string containerName = blobContainer.ToString().ToLower(); // must be lower case! Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = blobStorageDictionary[containerName]; blobStorage.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, 30, 0); Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobStorage.GetContainerReference(containerName); Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob = container.GetBlockBlobReference(fileName); // save if (fileName.Contains(".")) { blob.Properties.ContentType = GetMimeTypeFromExtension(fileName.Substring(fileName.LastIndexOf("."))); } else { blob.Properties.ContentType = "application/octet-stream"; } // save memoryStream.Position = 0; // rewind blob.UploadFromStream(memoryStream); } // SaveBlob
} // SaveBlob /// <summary> /// Writes text to a blob reference /// </summary> /// <param name="blob"></param> /// <param name="text"></param> public void PutText(Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob, string text) { if (text == null) { text = string.Empty; } using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(text))) { blob.UploadFromStream(memoryStream); } }
/// <summary> /// Writes text to a blob reference with an access condition /// </summary> /// <param name="blob"></param> /// <param name="text"></param> /// <param name="accessCondition"></param> public void PutText(Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob, string text, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition) { using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(text))) { try { blob.UploadFromStream(memoryStream, accessCondition); } catch (Microsoft.WindowsAzure.Storage.StorageException storageException) { if (storageException.Message.Contains("conditional header(s) is not met")) { throw new Sample.Azure.Common.CustomException.CustomConcurrencyException(System.Net.HttpStatusCode.PreconditionFailed, "The file has been updated by another user."); } else { throw; } } } }