public async Task WriteAppendBlobAsync(string containerName, string filename, Stream source, string contentType = "application/octet-stream", CancellationToken token = default(CancellationToken)) { if (string.IsNullOrEmpty(filename)) { throw new ArgumentException("filename"); } if (source == null) { throw new ArgumentNullException("source"); } Exception error = null; Stopwatch watch = new Stopwatch(); watch.Start(); double time = watch.Elapsed.TotalMilliseconds; long bytesTransferred = 0; IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>( progress => { bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred; if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred) { OnUploadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(containerName, filename, bytesTransferred, source.Length)); } }); try { CloudBlobContainer container = await GetContainerReferenceAsync(containerName); CloudAppendBlob blob = container.GetAppendBlobReference(filename); await blob.UploadFromStreamAsync(source, default(AccessCondition), default(BlobRequestOptions), default(OperationContext), progressHandler, token); } catch (Exception ex) { if (ex.InnerException is TaskCanceledException) { source = null; } else { error = ex; throw ex; } } finally { watch.Stop(); OnUploadCompleted?.Invoke(this, new BlobCompleteEventArgs(containerName, filename, token.IsCancellationRequested, error)); } }
public static void Run([BlobTrigger("helloworld/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, TraceWriter log) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse( "storage account connection string"); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("helloworld"); CloudAppendBlob blob = container.GetAppendBlobReference("log2.txt"); using (var fileStream = System.IO.File.OpenRead(@"D:\log.txt")) { blob.UploadFromStreamAsync(fileStream); } log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"); }
public static async Task WriteStateToBlob(CloudBlobContainer blobContainer, string blobName, string content) { CloudAppendBlob appendBlob = blobContainer.GetAppendBlobReference($"{(object)blobName}.json"); bool flag = await appendBlob.ExistsAsync(); if (!flag) { await appendBlob.CreateOrReplaceAsync(); } appendBlob.Properties.ContentType = "application/json"; using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(content))) { stream.Position = 0; await appendBlob.UploadFromStreamAsync(stream); } }
public static void UploadFromStream(this CloudAppendBlob cloudBlob, Stream source, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null) { cloudBlob.UploadFromStreamAsync(source, accessCondition, options, operationContext).GetAwaiter().GetResult(); }
public static void UploadFromStream(this CloudAppendBlob blob, System.IO.Stream stream) { blob.UploadFromStreamAsync(stream).Wait(); }