0% found this document useful (0 votes)
277 views

How To Upload and Download Files Programmatically To Azure Blob Storage Using

This document provides code examples for uploading and downloading files to Azure blob storage from a VB.NET application. It shows how to connect to Azure storage, list files and folders, create shared access signatures, get blob properties, read and write blobs, upload and delete blobs, rename blobs, and download blobs. The source code for the project is provided in a GitHub repository.

Uploaded by

Juan Cedeño
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
277 views

How To Upload and Download Files Programmatically To Azure Blob Storage Using

This document provides code examples for uploading and downloading files to Azure blob storage from a VB.NET application. It shows how to connect to Azure storage, list files and folders, create shared access signatures, get blob properties, read and write blobs, upload and delete blobs, rename blobs, and download blobs. The source code for the project is provided in a GitHub repository.

Uploaded by

Juan Cedeño
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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

You might also like