Sunday 13 September 2020

Upload and Retrieve File from Azure Blob Storage in C# & VB for .NET Framework

Azure Blob Storage is one of the Microsoft Azure Resource to store any types of file. Files that are placed in the blob storage can be accessible through API, Microsoft Azure Storage Explorer, directly from the azure portal or from PowerShell. So if you have the need to archive files or share the access to the file to different environment or application that is located in different locations, then Blob Storage is a good candidate for you. For more detail, you may further check it out from this link https://docs.microsoft.com/en-us/azure/storage/common/storage-introduction#blob-storage.

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" />

Otherwise, you need to retrieve the connection string from the Azure Portal. The format of the connection string is as follows.

<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=YourAzureBlobStorageName;AccountKey=YourAzureBlobStorageKey" />

To start using the packages retrieved from NuGet. Define the CloudStorageAccount instance based on the configuration defined above. This is to specify the location of the Azure Blob Storage.

[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.

[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.




Sunday 6 September 2020

How to Solve mutex in Namespace std does not Name a Type

Setting up the environment for Mingw-w64 may look easy and straightforward, but choosing the wrong setting during the setup may lead to failure to build or compile the source code. For example, if the source code contains the usage of mutex. With the wrong installation settings for Mingw-w64, you may stumble upon similar errors that are shown below.

error: 'mutex' in namespace 'std' does not name a type

     std::mutex m_mutex;

 

note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?

 

error: 'mutex' is not a member of 'std'

         std::lock_guard<std::mutex> lock(m_mutex);

 

error: template argument 1 is invalid

         std::lock_guard<std::mutex> lock(m_mutex);

 

error: 'm_mutex' was not declared in this scope

         std::lock_guard<std::mutex> lock(m_mutex);

 

note: suggested alternative: 'uv_mutex_t'

         std::lock_guard<std::mutex> lock(m_mutex);

                                          ^~~~~~~

                                          uv_mutex_t

 

 warning: unused variable 'lock' [-Wunused-variable]

         std::lock_guard<std::mutex> lock(m_mutex);


This is due to the settings that you specified during the installation doesn't contain the mutex implementation. So to fix this, you need to reinstall with the correct setting, where the mutex implementation is in the posix. Rerun the Mingw-w64 installer and make sure the posix is selected for the threads and continue with the installation.


Do take note to update the %PATH% Environment Variables to the newly installed path of Mingw-w64 with posix and restart your command prompt session to ensure the command is picking up the correct settings.

Monday 31 August 2020

How to Solve CMake was unable to find a build program corresponding to MinGW Makefiles

You may have a situation that requires you to use CMake to build a source code and then got hit with an error while executing a cmake command, which is something similar as shown below.

C:\SomeProject>cmake . -G "MinGW Makefiles"

CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.

CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage

CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage

-- Configuring incomplete, errors occurred!

There are few possibilities that may lead to this

1. Mingw-w64 is not installed

Since the error message mentioned that it is not able to find "MinGW Makefiles". Which mean that there is a possibility that you have not installed it to your machine. So, you may want to download the installer from here http://mingw-w64.org/doku.php and install it.

2. Installed Mingw-w64 but still showing the same error message

Most likely due to the installer did not place the necessary Mingw-w64 information to the %PATH% Environment Variables. So, you are required to fill in the Mingw-w64's bin path manually. To navigate to the Environment Variables:

a) In your Windows Search, search for the following keyword "Environment Variables" and select "Edit the system environment variables".

b) When the window "System Properties" appeared. Click on the button "Environment Variables".

c) On the next window, under the System variables section, look for the variable named "Path". Select it and click the Edit button.

d) In the Edit environment variable window, click New button and fill in the bin path of the Mingw-w64. Depend on the settings that you have picked during the Mingw-w64 installation, the default location of the Mingw-w64 can either be in Program Files or Program Files (x86).

Some of the example location:
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin
C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin
C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin

If you have multiple settings of Mingw-w64 installed, just pick 1 of the path based on the setting that you required. 

e) Once you are done, save your configured path by clicking on the Ok button to close the "Edit environment variable" window. On the "Environment variable" window, click Ok button to close the window.

If you have command prompt opened while editing the %PATH% Environment Variables, you are required to close them all and open up a new command prompt. This is due to the old command prompt caches the previous %PATH% settings.

This is all that you need to do and the error should no longer appear.