So to get started on how to access Blob Storage from your code, firstly you need to retrieve the following packages from NuGet.
Microsoft.Azure.ConfigurationManager
Microsoft.Azure.KeyVault.Core
Microsoft.Azure.Storage.Blob
Microsoft.Azure.Storage.Common
Newtonsoft.Json
Once you have gotten the necessary packages, you need to define where is your blob storage located in your configuration file. But if you currently do not have an azure account, you may setup and run the Microsoft Azure Storage Emulator on your machine. Refer to this link https://docs.microsoft.com/en-us/azure/storage/common/storage-use-emulator for more information on how to set it up.
If you are using the Microsoft Azure Storage Emulator, define the following setting in the configuration file.
<add key="StorageConnectionString" value="UseDevelopmentStorage=true" />
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=YourAzureBlobStorageName;AccountKey=YourAzureBlobStorageKey" />
[C#]
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
[VB]
Dim cloudStorageAccount As CloudStorageAccount
= CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"))
With the CloudStorageAccount, use it to create the container. This is required before file can be placed into the Azure Blob Storage. Do take note that the container name must be lower case. For more detail about the container, you may check the following link https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction#blob-storage-resources.
With the CloudStorageAccount, use it to create the container. This is required before file can be placed into the Azure Blob Storage. Do take note that the container name must be lower case. For more detail about the container, you may check the following link https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction#blob-storage-resources.
[C#]
CloudBlobContainer cloudBlobContainer = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("containername");
cloudBlobContainer.CreateIfNotExists();
[VB]
Dim cloudBlobContainer As CloudBlobContainer = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("containername")
cloudBlobContainer.CreateIfNotExists()
After the creation of the container, it is now possible to upload file to the Azure Blob Storage. In the following demonstration, the file in the local machine will be used to upload.
[C#]
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("SampleFileInBlob.txt");
cloudBlockBlob.UploadFromFile(Path.Combine(Environment.CurrentDirectory, "SampleFile.txt"));
[VB]
Dim cloudBlockBlob As CloudBlockBlob =
cloudBlobContainer.GetBlockBlobReference("SampleFileInBlob.txt")
cloudBlockBlob.UploadFromFile(Path.Combine(Environment.CurrentDirectory, "SampleFile.txt"))
Alternatively, the following demonstrate on how to retrieve the file from Azure Blob Storage and save it on local machine.
[C#]
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("SampleFileInBlob.txt");
cloudBlockBlob.DownloadToFile(Path.Combine(Environment.CurrentDirectory, "SavedSampleFile.txt"), FileMode.Create);
[VB]
Dim cloudBlockBlob As CloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("SampleFileInBlob.txt")
cloudBlockBlob.DownloadToFile(Path.Combine(Environment.CurrentDirectory, "SavedSampleFile.txt"), FileMode.Create)
Check out the sample code here https://1drv.ms/u/s!Aj2AA7hoIWHm03MBsAUCRtvATCYe.
No comments:
Post a Comment