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); }
public void AppendLineToBlob(string gameEventType, string data) { var account = CloudStorageAccount.Parse(_storageAccountConnectionString); var client = account.CreateCloudBlobClient(); var container = client.GetContainerReference(_containerName); container.CreateIfNotExists(); var folder = $"{gameEventType}/{_folderStructureFunc()}"; var blobName = GetBlobName(gameEventType); var fullBlobName = $"{folder}/{blobName}"; var blob = container.GetAppendBlobReference(fullBlobName); try { if (!blob.Exists()) { Console.WriteLine($"Creating AppendBlob: {fullBlobName}"); try { blob.CreateOrReplace(AccessCondition.GenerateIfNotExistsCondition()); } catch (StorageException ex) { } } Console.WriteLine($"Write to [{blobName}]: {data}"); blob.AppendText(data + "\n", accessCondition: AccessCondition.GenerateIfMaxSizeLessThanOrEqualCondition(_maxBlobSize)); } //TODO: Figure out exactly what exception is thrown if blob is too big and only catch that catch (StorageException ex) { // Blob was too big, get new blob name GetNewBlobName(gameEventType); // ... and try again AppendLineToBlob(gameEventType, data); } //TODO: Catch and handle other exceptions }