Azure Storage Uploadfromfileasync Get Url to the Uploaded File

Upload & Download File to Azure Blob Storage Asynchronously and Cancel Async With VB .Net


Asynchronous technique becomes of import when dealing with cloud resources or accessing object in long altitude network route.
Latency could blocking the application UI for sometimes if there are whatsoever large resources processed synchronously.

Microsoft Windows Azure Storage is ane of all-time options to store Hulk, File, Queue, Certificate Tabular array and also SQL databases.
Other well known and could considered as best cloud storage options are Amazon S3, Google only this post will be focused on Azure since the source lawmaking is congenital for Azure storage.

The source code in this postal service is bachelor for download at below url:
WinCloudAsyncAwait SourceCode

The demonstration application is a Visual Bones .Internet WinForm. This app upload and download epitome file from/to Azure Blob storage asynchronously.
It as well has Abolish to Async thread and List of uploaded files. Below is the Grade'due south design


Upload Async & Download Async buttons upload & download image respectively. Cancel button cancels every process in Form i.e upload, download, refresh list.
Picture Box shows image. Delay in seconds textbox specifies how many seconds in the every process should be delayed for sit-in purposes so user tin can have a take chances to click cancel push button and run across what happens.
This example utilize Azure storage emulator to store images as Blob.

VB .Internet Project Pace past Stride

  1. Create a VB .Internet WinForm project
  2. Add together reference to WindowsAzure.Storage. You lot might need to install the library using Nuget Package Managing director
  3. Create a Form similar above Form's Design picture
  4. Open app.config and edit it:
    <?xml version="1.0" encoding="utf-8" ?> <configuration>     <startup>         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />     </startup>     <appSettings>       <add key="StorageConnectionString" value="UseDevelopmentStorage=truthful" />     </appSettings> </configuration>              

    Since we are using Azure storage emulator then the connectedness string is just UseDevelopmentStorage=truthful

  5. Create a course to encapsulate upload, download and list files
    Code to create blob container asynchronously:
    Individual Async Function CreateContainer(ByVal cancelToken Equally CancellationToken) Equally Chore Try 	Dim storAcc As CloudStorageAccount = CloudStorageAccount.Parse(StorageConnStr) 	Dim blobClient Every bit CloudBlobClient = storAcc.CreateCloudBlobClient() 	container = blobClient.GetContainerReference(containerName) 	Wait container.CreateIfNotExistsAsync(cancelToken)  	Dim blobContPermission As New BlobContainerPermissions() 	blobContPermission.PublicAccess = BlobContainerPublicAccessType.Container  	container.SetPermissions(blobContPermission) Catch ex As OperationCanceledException 	Throw New OperationCanceledException("Create container canceled") Cease Try  Stop Part              

    Code to upload a file asynchronously:

    Public Async Function UploadAsync(ByVal path Equally String, ByVal cancelToken Equally CancellationToken) As Chore Try 	Await CreateContainer(cancelToken) 	Dim blockBlob Equally CloudBlockBlob = container.GetBlockBlobReference(System.IO.Path.GetFileName(path)) 	Await blockBlob.UploadFromFileAsync(path, cancelToken)  Grab ex As OperationCanceledException  	Throw New OperationCanceledException("UploadAsync canceled") End Try Cease Function              

    Refer to previous postal service for Upload blob to Azure Storage at this url:
    https://gugiaji.wordpress.com/2016/04/27/upload-file-to-azure-cloud-storage-via-asp-net-spider web-app/

    Below is a Code to download and listing files from Azure

    Public Async Function DownloadAsync(ByVal imgName As String, ByVal cancelToken Equally CancellationToken) Equally Task Try 	Await CreateContainer(cancelToken) 	Dim blockBlob Equally CloudBlockBlob = container.GetBlockBlobReference(imgName) 	Dim targetStream As Organization.IO.FileStream = Organisation.IO.File.OpenWrite(My.Application.Info.DirectoryPath & "\" & imgName)  	Await blockBlob.DownloadToStreamAsync(targetStream, cancelToken)  	targetStream.Close() Take hold of ex As OperationCanceledException  	Throw New OperationCanceledException("DownloadAsync canceled") End Try End Function  Public Async Function ListFilesAsync(ByVal cancelToken As CancellationToken) As Task(Of List(Of Cord)) Dim upshot Every bit New List(Of String) Endeavour 	Await CreateContainer(cancelToken)  	For Each particular As IListBlobItem In container.ListBlobs(vbNull, Imitation) 		If TypeOf detail Is CloudBlockBlob Then 			Dim blockBlob Equally CloudBlockBlob = particular 			result.Add together(blockBlob.Name) 		Finish If  	Next Grab ex Every bit OperationCanceledException 	Throw New OperationCanceledException("ListFilesAsync canceled") End Try Return consequence End Function              

    The download & listing are asynchronously then that we can abolish at anytime and the UI will not blocking.

  6. Code inside a Course
    Private cts As CancellationTokenSource Individual cancelToken As CancellationToken Private Sub Form1_Load(sender Equally Object, e As EventArgs) Handles MyBase.Load 	ToolStripStatusLabel1.Text = "" 	PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage 	cts = New CancellationTokenSource() 	cancelToken = cts.Token 	cancelToken.ThrowIfCancellationRequested()  	ListImagesAsync().ConfigureAwait(Simulated) End Sub              
    Individual Async Part ListImagesAsync() Every bit Task Endeavour 	ToolStripStatusLabel1.Text = "Retrieve Prototype collection" 	Await Task.Delay(txtDelay.Text)  	Dim AZBlob As New AzureBlob(ConfigurationManager.AppSettings("StorageConnectionString"), "mycontainer") 	Dim event Equally List(Of String) = Await AZBlob.ListFilesAsync(cancelToken) 	For Each imgName As String In result 		ListBox1.Items.Add(imgName)  	Next 	ToolStripStatusLabel1.Text = "" Catch exCancel As OperationCanceledException 	Dim message As String = "ListImages canceled at DemoUploadAsync" 	MessageBox.Show(message) 	ToolStripStatusLabel1.Text = message Catch ex As Exception 	MessageBox.Show(ex.Message) 	ToolStripStatusLabel1.Text = ex.Message Finish Try End Part              

    There is Await Job.Filibuster(txtDelay.Text) to delay for some seconds then that we have a chance to click cancel button for demonstration purposes.

    Private Async Sub btnSave_Click(sender Every bit Object, e As EventArgs) Handles btnSave.Click Effort 	ToolStripStatusLabel1.Text = "Uploading" 	Await Job.Delay(txtDelay.Text)  	Dim AZBlob As New AzureBlob(ConfigurationManager.AppSettings("StorageConnectionString"), "mycontainer") 	Await AZBlob.UploadAsync(PictureBox1.ImageLocation, cancelToken) 	MessageBox.Evidence("upload success") 	ToolStripStatusLabel1.Text = "" 	PictureBox1.Image = Nothing Take hold of exCancel As OperationCanceledException 	Dim bulletin As String = "upload canceled at Form" 	MessageBox.Prove(message) 	ToolStripStatusLabel1.Text = message Grab ex As Exception 	MessageBox.Show(ex.Message) 	ToolStripStatusLabel1.Text = ex.Message End Endeavor End Sub              
    Individual Async Sub btnRet_Click(sender Equally Object, east As EventArgs) Handles btnRet.Click Attempt 	ToolStripStatusLabel1.Text = "Download Paradigm" 	Await Task.Filibuster(txtDelay.Text) 	Dim imgName As String = ListBox1.SelectedItem.ToString() 	Dim AZBlob As New AzureBlob(ConfigurationManager.AppSettings("StorageConnectionString"), "mycontainer") 	Expect AZBlob.DownloadAsync(imgName, cancelToken) 	Dim fs Every bit Organisation.IO.FileStream = Organization.IO.File.OpenRead(My.Application.Info.DirectoryPath & "\" & imgName)  	PictureBox1.Image = Prototype.FromStream(fs)  	ToolStripStatusLabel1.Text = "" Catch exCancel As OperationCanceledException 	Dim message As String = "download canceled at Course" 	MessageBox.Show(message) 	ToolStripStatusLabel1.Text = message Catch ex As Exception 	MessageBox.Show(ex.Message) 	ToolStripStatusLabel1.Text = ex.Message  End Try Stop Sub              
    Private Sub btnCancel_Click(sender As Object, east As EventArgs) Handles btnCancel.Click 	cts.Cancel() End Sub              
  7. Yous may download full source code at below url:
    WinCloudAsyncAwait SourceCode
    to test on your own. Update the app.config file to your Azure storage account

Regards,
Agung Gugiaji

sprypral1953.blogspot.com

Source: https://gugiaji.wordpress.com/2018/03/20/upload-download-file-to-azure-blob-storage-asynchronously-and-cancel-async-with-vb-net/

0 Response to "Azure Storage Uploadfromfileasync Get Url to the Uploaded File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel