How to Upload and Download Files Pragmatically to Azure Blob Storage using VB.
Net
In this tutorial I’m going to show how to:
• Connect to Azure Storage
• List files/folders (CloudBlockBlob/CloudBlobDirectory) within an Azure File Storage
• Create a Shared Access Signature for a Blob
• Obtain Blob properties
• Read content from a Blob
• Write content to a Blob
• Upload a Blob to Azure File Storage
• Delete a Blob
• Rename/Move a Blob
• Download a Blob
Source code for this Project can be found here:
https://github.com/patricksameerajayalath/Azure.FileStorage
You can fid the Part 1 of this tutorial here:
https://github.com/patricksameerajayalath/AzureTutorials/blob/master/Azure%20File%20Storage%
20Overview.pdf
Patrick Sameera Jayalath
At the end of this tutorial we will be ending up a UI like bellow.
Patrick Sameera Jayalath
Patrick Sameera Jayalath
To get start we will be creating an ASP.Net Web Project.
• Language – VB.Net
In the Solution Explorer, right click on the Project and select Manage NuGet Packages.
Under Browse tab Search for Microsoft.Azure.Stroage.Blob and on the results Select it and Click
Install.
Patrick Sameera Jayalath
Patrick Sameera Jayalath
For this tutorial we will be using the same File Storage Account we created earlier tutorial. You can
find the tutorial here:
https://github.com/patricksameerajayalath/AzureTutorials/blob/master/Azure%20File%20Storage%
20Overview.pdf
Patrick Sameera Jayalath
We will be storing all Storage Account related information in Web.config.
Web.config
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Web.config
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Web.config#L5
Patrick Sameera Jayalath
You can get the connection string from "Access Keys" tab.
You can get the Blob URL from "Properties" tab.
Patrick Sameera Jayalath
Default.aspx
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx
Default.aspx.vb
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb
Patrick Sameera Jayalath
Code Snippets:
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb
Code 01 - Connect to Azure Storage
Bellow configurations will be read from web.config.
• File Storage Account name – [key="storage:accountName"]
• File Storage key – [key="storage:key"]
• File Storage URI – [key="storage:uri"]
• File Storage Container name – [key="storage:containerName"]
• File Storage directory – [key="storage:directoryUploadDocs"]
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Web.config#L5
Connect to Azure File Storage by retrieving the config values from we.b.config
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L16
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L151
Patrick Sameera Jayalath
Code 02 - List files/folders (CloudBlockBlob/CloudBlobDirectory) within an Azure File Storage
On Page_Load we have called ShowDocumentList() function. It will read configuration from
web.config and connect to Azure File Storage and list all the Blobs.
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L8
Read list of Blobs:
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L23
Iterate through Blobs:
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L42
Iterate through Directories:
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L100
Patrick Sameera Jayalath
Code 03 - Write content to a Blob
Write content to Blob:
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L238
Patrick Sameera Jayalath
Code 04 - Create a Shared Access Signature for a Blob
Generate SharedAccessBlobPolicy which has:
• Read access
• Expiry time of 20 minutes
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L69
Code 05 - Obtain Blob properties
Retrieve Blob properties like:
• Blob size
• Blob last modified date
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L92
Patrick Sameera Jayalath
Code 06 - Upload a Blob to Azure File Storage
Upload a Blob with:
• Check if the Blob already exists
• Upload the Blob as an InputStream
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L163
Patrick Sameera Jayalath
Code 07 - Read content from a Blob
Read Blob content using StreamReader and Writes to a TextArea.
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L82
Code 08 - Delete a Blob
Delete a Blob with:
• Check if the Blob already exists
• Delete Blob
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L209
Patrick Sameera Jayalath
Code 09 - Rename/Move a Blob
Rename functionality:
• First create a new file copy using existing old file
• Next delete the existing old file
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L281
Patrick Sameera Jayalath
Code 10 - Download a Blob
In this example I have shown 3 ways how we can Download a Blob.
• Download to a file – DownloadFile()
• Writing to MemoryStream and FileStream
• Using Response.Redirect
https://github.com/patricksameerajayalath/Azure.FileStorage/blob/master/Default.aspx.vb#L321
Patrick Sameera Jayalath