public async Task <UploadResult> UploadFile(string fileName, string contentType, Stream stream) { var account = CloudStorageAccount.Parse(_settingsFunc().AzureblobStorageConnectionString); var client = account.CreateCloudBlobClient(); var container = client.GetContainerReference(JabbRUploadContainer); // Randomize the filename everytime so we don't overwrite files string randomFile = Path.GetFileNameWithoutExtension(fileName) + "_" + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(fileName); if (container.CreateIfNotExists()) { // We need this to make files servable from blob storage container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); } CloudBlockBlob blockBlob = container.GetBlockBlobReference(randomFile); blockBlob.Properties.ContentType = contentType; await Task.Factory.FromAsync((cb, state) => blockBlob.BeginUploadFromStream(stream, cb, state), ar => blockBlob.EndUploadFromStream(ar), null); var result = new UploadResult { Url = blockBlob.Uri.ToString(), Identifier = randomFile }; return(result); }
/// <summary> /// Upload Stream to BlobStore with a given name /// </summary> /// <param name="stream"> /// Stream to upload /// </param> /// <param name="blobName"> /// Name of the blob /// </param> /// <returns> /// Async Task Wrapper /// </returns> protected async Task UploadBlobAsync(Stream stream, string blobName) { CloudBlockBlob blockBlob = Container.GetBlockBlobReference(blobName); await Task.Factory.FromAsync( blockBlob.BeginUploadFromStream(stream, null, null), blockBlob.EndUploadFromStream).ConfigureAwait(false); }
/// <summary> /// Put a block blob in the container created in the constructor /// </summary> /// <param name="blobName">blob name</param> /// <param name="data">a stream</param> /// <returns>Return true on success, false if unable to create, throw exception on error</returns> public async Task <bool> PutBlockBlobAsync(string blobName, Stream data) { try { if (_container == null) { return(false); } CloudBlockBlob blob = this._container.GetBlockBlobReference(blobName); var futureRes = blob.BeginUploadFromStream(data, null, null); return(await Task.Factory.FromAsync(futureRes, res => { blob.EndUploadFromStream(res); return true; })); } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 404) { return(false); } throw; } }
/// <summary> /// Upload file to blob storage. input Custome file object /// </summary> /// <param name="file"></param> /// <returns></returns> public async Task <bool> UploadFileToBlob(CustomFile file) { // Get Blob Container CloudBlobContainer _container = AzureBlobUtilities.GetBlobClient.GetContainerReference("kantarimages"); _container.CreateIfNotExists(); // Get reference to blob (binary content) CloudBlockBlob blockBlob = _container.GetBlockBlobReference(file.FileName); // set its properties blockBlob.Properties.ContentType = file.FileMime; blockBlob.Metadata["filename"] = file.FileName; blockBlob.Metadata["filemime"] = file.FileMime; // Get stream from file bytes Stream stream = new MemoryStream(file.FileBytes); // Async upload of stream to Storage AsyncCallback uploadCompleted = new AsyncCallback(OnUploadCompleted); blockBlob.BeginUploadFromStream(stream, uploadCompleted, blockBlob); return(true); }
public static async Task UploadFromStreamAsync <T>(this CloudBlockBlob blob, T stream) where T : Stream { var tcs = new TaskCompletionSource <object>(); blob.BeginUploadFromStream(stream, iar => { try { blob.EndDownloadToStream(iar); tcs.TrySetResult(null); } catch (OperationCanceledException) { tcs.TrySetCanceled(); } catch (Exception ex) { tcs.TrySetException(ex); } }, null); await tcs.Task; }
private void Upload() { CloudBlockBlob blockBlob = container.GetBlockBlobReference(dlg.SafeFileName); using (var fileStream = System.IO.File.OpenRead(dlg.FileName)) { blockBlob.UploadFromStream(fileStream); AsyncCallback callback = new AsyncCallback(toggle); blockBlob.BeginUploadFromStream(fileStream, callback, new object()); } GetAttachments(); }
/// <summary> /// Put (create or update) a block blob /// </summary> /// <param name="containerName">container name</param> /// <param name="blobName">blob name</param> /// <param name="content">string content</param> /// <returns>Return true on success, false if unable to create, throw exception on error</returns> public async Task <bool> PutBlockBlobAsync(string containerName, string blobName, string content) { try { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); CloudBlockBlob blob = container.GetBlockBlobReference(blobName); var futureRes = blob.BeginUploadFromStream(new MemoryStream(Convert(content)), null, null); return(await Task.Factory.FromAsync(futureRes, res => { blob.EndUploadFromStream(res); return true; })); } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 404) { return(false); } throw; } }
/// <summary> /// Upload file to azure. /// </summary> /// <param name="fileName"></param> /// <param name="fileContent"></param> public void UploadFile(string fileName, string fileContent) { // Get Blob Container CloudBlobContainer container = cloudBlobClient.GetContainerReference("documents"); container.CreateIfNotExist(); // Get reference to blob (binary content) CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); // set its properties blockBlob.Properties.ContentType = "text/plain"; blockBlob.Metadata["filename"] = fileName; blockBlob.Metadata["filemime"] = "text/plain"; // Get stream from file bytes Stream stream = new MemoryStream(Encoding.ASCII.GetBytes(fileContent)); // Async upload of stream to Storage AsyncCallback UploadCompleted = new AsyncCallback(OnUploadCompleted); blockBlob.BeginUploadFromStream(stream, UploadCompleted, blockBlob); }