public async Task CreateIfNotExist() { if (!await this.blob.ExistsAsync()) { await blob.UploadFromByteArrayAsync(new byte[0], 0, 0); } }
public async Task WriteAppendBlobAsync(string containerName, string filename, byte[] 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.UploadFromByteArrayAsync(source, 0, source.Length, 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)); } }
private async Task UploadAsync(CloudAppendBlob blob, byte[] buffer, CancellationToken token) { string fullpath = blob.Container.Name + "/" + blob.Name; 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(fullpath, buffer.Length, progress.BytesTransferred)); } }); try { await blob.UploadFromByteArrayAsync(buffer, 0, buffer.Length, default(AccessCondition), default(BlobRequestOptions), default(OperationContext), progressHandler, token); watch.Stop(); } catch (Exception ex) { watch.Stop(); if (ex.InnerException is TaskCanceledException) { buffer = null; } else { throw ex; } } OnUploadCompleted?.Invoke(this, new BlobCompleteEventArgs(fullpath, token.IsCancellationRequested)); }
public static void UploadFromByteArray(this CloudAppendBlob cloudBlob, byte[] buffer, int index, int count, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null) { cloudBlob.UploadFromByteArrayAsync(buffer, index, count, accessCondition, options, operationContext).GetAwaiter().GetResult(); }
public static void UploadText(this CloudAppendBlob blob, string text, Encoding encoding = null) { byte[] msgBytes = null != encoding?encoding.GetBytes(text) : Encoding.UTF8.GetBytes(text); blob.UploadFromByteArrayAsync(msgBytes, 0, msgBytes.Length).Wait(); }
public static void UploadFromByteArray(this CloudAppendBlob blob, byte[] buffer, int index, int count) { blob.UploadFromByteArrayAsync(buffer, index, count).Wait(); }