Upload Files to Blob Storage With Azure C#
- Download source files - one.5 MB
Part 1: https://www.codeproject.com/Manufactures/1273552/Azure-Storage-Account-Role-1-What-is-Blob
Part two: https://www.codeproject.com/Manufactures/1273613/Azure-Storage-Account-Part-2-Upload-Files-in-Blob
In the terminal mail, Azure Storage Account Part one: What is blob?, of this series, we have seen what is Azure Storage Business relationship and how to manage your files in a blob storage, how to upload and access the files through Azure portal. Here, in this module, we will check how to manage your files (upload, download, delete, copy) in your blobs in an Azure Storage Account.
We will cover upward:
- Connection Strings of a storage business relationship
- Prepare up C# projection for accessing storage account
- Upload a file into a hulk storage using C#
- Download a file using C# from a blob storage
- Delete a blob using C#
- Delete a container using C#
Allow's starting time!
1. Connectedness Strings of a Storage Account
In the last module, we have covered up the keys and connection strings of an Azure storage account. We will employ those connectedness string
due south in our application.
Before that, let'south get though how we tin get the connectedness string
s of a storage business relationship. Go to Storage Account from your Azure portal. Cull the storage account from the list of your accounts. And and then go to "Admission keys" section. Here, you lot will get your keys & connection strings.
Copy 1 of the connection strings, we are going to apply it in our application.
ii. Set up C# Project for Accessing Storage Business relationship
Now create a C# console application (yous can create your choice of project types, web or windows).
Next, you accept to install WindowsAzure.Storage
package from NuGet Package Manager (NPM). Open your NPM Console (NPM) from Tools -> NuGet Bundle Manager -> Package Manager Console.
Now install WindowsAzure.Storage
past running the following control:
PM> Install-Package WindowsAzure.Storage -Version 9.3.3
Later this, couple of references will be added in your solutions:
-
Microsoft.WindowsAzure.KeyVault.Core
-
Microsoft.Windows.Azure.Storage
-
Newtonsoft.Json
At present your application is ready to access the files of Azure storage account.
Please note onwards 9.iv.0 library has been dissever into multiple parts like Microsoft.Azure.Storage.Blob
, Microsoft.Azure.Storage.File
, Microsoft.Azure.Storage.Queue
& Microsoft.Azure.Storage.Common
.
3. Upload a File Into a Blob Storage Using C#
First, fix the connexion cord in your awarding. Set it in Web.Config
under appSettings
section.
Follow the beneath code snippet to upload a file to blob storage.
using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Blob; private static string ConnectionSting { become { return " your connection string"; } } public static bool Upload() { effort { var containerName = " your container name"; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionSting); CloudBlobClient client = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference(containerName); var isCreated = container.CreateIfNotExists(); container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); using (FileStream fileStream = File.Open(@" C:\d\log.txt", FileMode.Open)) { using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Position = 0; fileStream.CopyTo(memoryStream); var fileName = " Test-log.txt"; CloudBlockBlob blob = container.GetBlockBlobReference(fileName); string mimeType = " application/unknown"; string ext = (fileName.Contains(" .")) ? System.IO.Path.GetExtension(fileName).ToLower() : " ." + fileName; Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); if (regKey != null && regKey.GetValue(" Content Type") != cipher) mimeType = regKey.GetValue(" Content Type").ToString(); memoryStream.ToArray(); memoryStream.Seek(0, SeekOrigin.Begin); hulk.Properties.ContentType = mimeType; blob.UploadFromStream(memoryStream); } } return truthful; } grab (Exception ex) { throw; } }
Now go to your Azure portal and refresh the hulk bract, and y'all will discover your file over there.
4. Download a File Using C# From a Hulk Storage
Now later on uploading the file, it is time to get the file back equally stream
.
To practice so, follow the beneath lawmaking snippet:
public static MemoryStream Download() { var containerName = " your container name"; var blobName = " hulk proper noun that you take set"; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionSting); CloudBlobClient customer = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = customer.GetContainerReference(containerName); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); var allBlobs = container.ListBlobs(); MemoryStream memStream = new MemoryStream(); blockBlob.DownloadToStream(memStream); render memStream; }
Now y'all tin can create a file from the retentiveness stream or you can laissez passer the stream from a web API.
5. Delete a Blob Using C#
To delete a blob, procedure is more or less the same. Create the example of storage business relationship and container. And then, check if the blob is present or not. If present, and so delete the same. Microsoft has an inbuilt function to delete the hulk. CloudBlockBlob.DeleteIfExists.
public static bool DeleteBlob() { var containerName = " your container name"; var blobName = " blob proper name that you lot take set"; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionSting); CloudBlobClient client = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference(containerName); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); return blockBlob.DeleteIfExists() }
To delete all files of a container, you need to loop through all blobs of a container and delete individually.
six. Delete a Container Using C#
To delete a whole container, follow the beneath lawmaking snippet. Use CloudBlobContainer.Delete(AccessCondition, BlobRequestOptions, OperationContext) method to delete the container.
public static bool DeleteContainer() { var containerName = " your container name"; var blobName = " blob name that y'all have set"; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionSting); CloudBlobClient customer = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference(containerName); container.Delete(); }
Parts
- Azure Storage Business relationship Part 1: What is Blob?
- Azure Storage Account Part 2: Upload files in Blob storage using C#
CodeProject
Source: https://www.codeproject.com/Articles/1273613/Azure-Storage-Account-Part-2-Upload-Files-in-Blob
0 Response to "Upload Files to Blob Storage With Azure C#"
Postar um comentário