public void Write(byte[] buffer, int offset, int count) { var stream = new MemoryStream(buffer, 0, count); var t = blob.AppendFromStreamAsync(stream); t.Wait(); }
private void Flush(FlushMode flushMode) { // If this is the forced flush on Dispose, wait until we acquire the lock. Otherwise, // just check to see if the lock is available, and if not, we are still processing the // last tranche of appends, so bail out and wait for the next flush interval. var lockTimeout = (flushMode == FlushMode.IfIdle ? TimeSpan.Zero : Timeout.InfiniteTimeSpan); bool acquiredLock = false; Monitor.TryEnter(_lock, lockTimeout, ref acquiredLock); if (!acquiredLock) { return; } try { var file = new FileInfo(_filePath); if (!file.Exists) { return; } var uploadPointer = file.Length; if (uploadPointer <= _flushPointer) { return; } using (var stm = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { stm.Seek(_flushPointer, SeekOrigin.Begin); _blob.AppendFromStreamAsync(stm, uploadPointer - _flushPointer).GetAwaiter().GetResult(); _flushPointer = uploadPointer; } } catch (Exception ex) { if (flushMode == FlushMode.IfIdle) { OnFlushError(ex); } else { throw; } } finally { Monitor.Exit(_lock); } }
async Task ProcessFile(CloudBlob outputBlob, CloudAppendBlob resultBlob) { var sw = Stopwatch.StartNew(); using (var contentStream = await outputBlob.OpenReadAsync()) { await resultBlob.AppendFromStreamAsync(contentStream); } sw.Stop(); this.logger.LogInformation($"Finished processing file '{outputBlob.Name}' into '{resultBlob.Name}' in {sw.ElapsedMilliseconds}ms"); }
public async Task WriteAsync(string id, Stream sourceStream, bool append, CancellationToken cancellationToken) { GenericValidation.CheckBlobId(id); GenericValidation.CheckSourceStream(sourceStream); if (append) { CloudAppendBlob cab = _blobContainer.GetAppendBlobReference(StoragePath.Normalize(id, false)); if (!(await cab.ExistsAsync())) { await cab.CreateOrReplaceAsync(); } await cab.AppendFromStreamAsync(sourceStream); } else { CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(StoragePath.Normalize(id, false)); await blob.UploadFromStreamAsync(sourceStream); } }
public async Task WriteAsync(string fullPath, Stream sourceStream, bool append, CancellationToken cancellationToken) { GenericValidation.CheckSourceStream(sourceStream); (CloudBlobContainer container, string path) = await GetPartsAsync(fullPath).ConfigureAwait(false); if (append) { CloudAppendBlob cab = container.GetAppendBlobReference(StoragePath.Normalize(path, false)); if (!await cab.ExistsAsync()) { await cab.CreateOrReplaceAsync().ConfigureAwait(false); } await cab.AppendFromStreamAsync(sourceStream).ConfigureAwait(false); } else { CloudBlockBlob cloudBlob = container.GetBlockBlobReference(StoragePath.Normalize(path, false)); await cloudBlob.UploadFromStreamAsync(sourceStream).ConfigureAwait(false); } }
public static void AppendFromStream(this CloudAppendBlob blob, Stream stream) { blob.AppendFromStreamAsync(stream).Wait(); }