public async Task UploadFile(string sourcePath, string sourceFilename, string containerName, string targetFilename, string contentType, bool append = false) { byte[] fileContent = File.ReadAllBytes(Path.Join(fixPath(sourcePath), sourceFilename)); CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString); CloudBlobClient serviceClient = account.CreateCloudBlobClient(); var container = serviceClient.GetContainerReference(getContainerName(containerName)); container.CreateIfNotExistsAsync().Wait(); CloudAppendBlob blob = container.GetAppendBlobReference(getFilepathForContainer(containerName, targetFilename)); if (!append) { await blob.CreateOrReplaceAsync(); } else { if (!blob.ExistsAsync().Result) { throw new Exception($"Cannot append to nonexistent blob file {sourceFilename}"); } } blob.Properties.ContentType = contentType; await blob.AppendFromByteArrayAsync(fileContent, 0, fileContent.Length); }
/// <summary> Append bytes to a blob only if at the provided append position. </summary> /// <remarks> This will throw a <see cref="StorageException"/> on conflict. </remarks> public static Task AppendTransactionalAsync( this CloudAppendBlob blob, byte[] data, long position, CancellationToken cancel = default) { return(blob.AppendFromByteArrayAsync( accessCondition: new AccessCondition { IfAppendPositionEqual = position }, buffer: data, index: 0, count: data.Length, options: new BlobRequestOptions(), operationContext: new OperationContext(), cancellationToken: cancel)); }
public async Task UploadFile(string sourcePath, string sourceFilename, string sasUri, string contentType, bool append = false) { byte[] fileContent = File.ReadAllBytes(Path.Join(fixPath(sourcePath), sourceFilename)); CloudAppendBlob blob = new CloudAppendBlob(new Uri(sasUri)); if (!append) { await blob.CreateOrReplaceAsync(); } else { if (!blob.ExistsAsync().Result) { throw new Exception($"Cannot append to nonexistent blob file {sourceFilename}"); } } blob.Properties.ContentType = contentType; await blob.AppendFromByteArrayAsync(fileContent, 0, fileContent.Length); }
//public async Task WriteBlockBlobAsync(string containerName, string filename, // byte[] source, string contentType, CancellationToken token = default(CancellationToken)) //{ // if (source == null) // { // throw new ArgumentNullException("source"); // } // CloudBlobContainer container = await GetContainerReferenceAsync(containerName); // CloudBlockBlob blob = container.GetBlockBlobReference(filename); // blob.Properties.ContentType = contentType; // await UploadAsync(blob, source, token); //} public async Task WriteAppendBlobAsync(string containerName, string filename, byte[] source, string contentType = "application/octet-stream", CancellationToken token = default(CancellationToken)) { if (source == null) { throw new ArgumentNullException("source"); } CloudBlobContainer container = await GetContainerReferenceAsync(containerName); CloudAppendBlob blob = container.GetAppendBlobReference(filename); if (!await blob.ExistsAsync()) { blob.Properties.ContentType = contentType; await UploadAsync(blob, source, token); } else { await blob.AppendFromByteArrayAsync(source, 0, source.Length); } }
public static void AppendFromByteArray(this CloudAppendBlob blob, byte[] buffer, int offset, int count) { blob.AppendFromByteArrayAsync(buffer, offset, count).Wait(); }