/// <summary> /// Expect sas token has the Append permission for the specified container. /// </summary> internal void ValidateContainerAppendableWithSasToken(CloudBlobContainer container, string sastoken) { Test.Info("Verify container Append permission"); CloudStorageAccount sasAccount = TestBase.GetStorageAccountWithSasToken(container.ServiceClient.Credentials.AccountName, sastoken); CloudBlobClient sasBlobClient = sasAccount.CreateCloudBlobClient(); CloudBlobContainer sasContainer = sasBlobClient.GetContainerReference(container.Name); string blobName = Utility.GenNameString("sasAppendblob"); CloudAppendBlob appendblob = sasContainer.GetAppendBlobReference(blobName); container.GetAppendBlobReference(blobName).CreateOrReplace(); long buffSize = 1024 * 1024; byte[] buffer = new byte[buffSize]; random.NextBytes(buffer); MemoryStream ms = new MemoryStream(buffer); appendblob.AppendBlock(ms); CloudBlob retrievedBlob = StorageExtensions.GetBlobReferenceFromServer(container, blobName); Test.Assert(retrievedBlob != null, "Append blob should exist on server"); TestBase.ExpectEqual(StorageBlobType.AppendBlob.ToString(), retrievedBlob.BlobType.ToString(), "blob type"); TestBase.ExpectEqual(buffSize, retrievedBlob.Properties.Length, "blob size"); }
/// <summary> /// Sends chunks to append blob /// </summary> /// <param name="events"></param> public void Write(IEnumerable <LogEvent> events) { CloudAppendBlob blob = null; var sb = new StringBuilder(); foreach (LogEvent e in events) { if (blob == null) { blob = GetBlob(e.EventTime); } string line = TextFormatter.Format(e); sb.AppendLine(line); } if (blob != null) { using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()))) { blob.AppendBlock(ms); } } //AppendText has a strange proble, whereas AppendBlock doesn't have it! The append position condition specified was not met. //blob?.AppendText(sb.ToString()); }
private bool AppendToBlob(CloudAppendBlob blob, MemoryStream stream) { try { if (!blob.Exists()) { return(false); } blob.AppendBlock(stream, accessCondition: AccessCondition.GenerateIfMaxSizeLessThanOrEqualCondition(_maxBlobSize)); } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 412) // HttpStatusCode.PreconditionFailed { return(false); } else { throw; } } return(true); }
private void ProcessEvent(LoggingEvent loggingEvent) { CloudAppendBlob appendBlob = _cloudBlobContainer.GetAppendBlobReference(Filename(_directoryName)); var xml = _lineFeed + loggingEvent.GetXmlString(); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { appendBlob.AppendBlock(ms); } }
private void ProcessEvent(LoggingEvent loggingEvent) { CloudAppendBlob appendBlob = _cloudBlobContainer.GetAppendBlobReference(Filename(_directoryName, _filenamePrefixDateTimeFormat, _filenameSuffix)); PatternLayout layout = (PatternLayout)base.Layout; string log = layout.Format(loggingEvent); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(log))) { appendBlob.AppendBlock(ms); } }
public void Log(object data, string source, string message = null) { try { string dataString = (data != null) ? data as string : "null"; if (dataString == null) { dataString = JsonConvert.SerializeObject(data); } var sasUri = new Uri(this.configuration.ObjectLogSASUrl); var accountName = sasUri.Host.TrimEndString(".blob.core.windows.net"); StorageCredentials creds = new StorageCredentials(sasUri.Query); CloudStorageAccount strAcc = new CloudStorageAccount(creds, accountName, endpointSuffix: null, useHttps: true); CloudBlobClient blobClient = strAcc.CreateCloudBlobClient(); //Setup our container we are going to use and create it. CloudBlobContainer container = blobClient.GetContainerReference(configuration.ObjectLogBlobContainer); container.CreateIfNotExistsAsync(); // Build my typical log file name. DateTime date = DateTime.Now; // This creates a reference to the append blob we are going to use. CloudAppendBlob appBlob = container.GetAppendBlobReference( $"{date.ToString("yyyy-MM")}/{date.ToString("dd")}/{date.ToString("HH")}-{source}.log"); // Now we are going to check if todays file exists and if it doesn't we create it. if (!appBlob.Exists()) { appBlob.CreateOrReplace(); } // Add the entry to our log. var logMessage = $"{date.ToString("o")}|{message}|{dataString}{Environment.NewLine}"; using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(logMessage))) { // Append a block. AppendText is not safe across multiple threads & servers so use AppendBlock instead. appBlob.AppendBlock(ms); } } catch (Exception ex) { this.logger.Error("Error logging data to the permanent store", ex); } }
public void WriteTextToBlob(string text) { var formattedStorageContainerName = FormatContainerName(StorageContainerName); var formattedStorageBlobName = FormatBlobName(StorageBlobName); if (!string.IsNullOrEmpty(text)) { InstanciateStorageContainer(formattedStorageContainerName); InstanciateStorageBlob(formattedStorageContainerName, formattedStorageBlobName); using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(text))) { WriteDebugInfo(String.Format("NLog.AzureStorage - {0}/{1} - Successfully wrote bytes: {2}", formattedStorageContainerName, formattedStorageBlobName, memoryStream.Length)); _appendBlob.AppendBlock(memoryStream); } } }
public static List <long> AppendToWasbContainer(CloudBlobClient blobClient, string containerName, string blobName, int numIterations, byte[] contents) { CloudBlobContainer appendContainer = blobClient.GetContainerReference(containerName); CloudAppendBlob appendBlob = appendContainer.GetAppendBlobReference(blobName); appendBlob.CreateOrReplace(); appendContainer.CreateIfNotExists(); var perfMetrics = new List <long>(); for (int i = 0; i < numIterations; i++) { using (var stream = new MemoryStream(contents)) { var watch = Stopwatch.StartNew(); appendBlob.AppendBlock(stream); watch.Stop(); perfMetrics.Add(watch.ElapsedMilliseconds); } } return(perfMetrics); }
public static void AppendToBlob(string fileName, string textToAppend) { CloudStorageAccount account = CloudStorageAccount.Parse(AmbientConnectionStringProvider.Instance.GetConnectionString("blobConnection")); CloudBlobClient client = account.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference("images"); CloudAppendBlob appendBlob = container.GetAppendBlobReference(fileName); if (!appendBlob.Exists()) { appendBlob.CreateOrReplace(); } MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write(textToAppend + Environment.NewLine); writer.Flush(); stream.Position = 0; appendBlob.AppendBlock(stream); }