Wednesday 22 February 2023

Connect and upload in azure blob with azure key vault using x++

 Today, I will be sharing details about file upload in azure blob using azure vault that includes pre requisite and code snippet used to it.

Before proceeding with code we need following key information which will be added in the key vault parameters in D365 F&O.

  1. Azure application id
  2. Azure application client secret
  3. Key vault URL
  4. Key vault Secret (In our case we need three : Container name, storage account, storage key)
  5. Container/Blob must be created in the azure

The above mentioned information can be added and fetched from azure portal. After getting those details  you need to create a new record in azure vault parameters form.

Go to System administration > Setup > Key vault parameters

Create a new record in key vault then create three new records for secrets.




Below is the code snippet used to get storage account, storage key and container name via Azure vault and upload files in the blob.

Note: I have created csv file using CommaStreamIo and getting the stream from it in such a way
System.IO.Stream stream = commaStreamIo.getStream();
Also, add following namespace at the top of the class :
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer;
using Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob;
using Microsoft.WindowsAzure.Storage.File

Code snippet : 
 private void uploadFileInAzureBlob(System.IO.Stream _stream, str _fileName)
    {
      
        KeyVaultCertificateTable    certTableForContainer;
        KeyVaultCertificateTable    certTableForStorageAccount;
        KeyVaultCertificateTable    certTableForStorageAccountKey;
        str                         containerName;
        str                         storageAccountName;
        str                         storageAccountKey;
        
        certTableForContainer         = KeyVaultCertificateTable::findByName("ContainerName");
        certTableForStorageAccount    = KeyVaultCertificateTable::findByName("StorageAccountName");
        certTableForStorageAccountKey = KeyVaultCertificateTable::findByName("StorageAccountKey");

        try
        {
            if (certTableForStorageAccount.RecId != 0 && certTableForStorageAccountKey.RecId != 0 &&  certTableForContainer.RecId != 0)
            {
                containerName        = KeyVaultCertificateHelper::getManualSecretValue(certTableForContainer.RecId);
                storageAccountName   = KeyVaultCertificateHelper::getManualSecretValue(certTableForStorageAccount.RecId);
                storageAccountkey    = KeyVaultCertificateHelper::getManualSecretValue(certTableForStorageAccountKey.RecId);

                var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(StorageAccountName, storageAccountkey);
                CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);

                if(storageAccount)
                {
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
                    changecompany(curExt())
                    {
                        CloudBlockBlob blockblob =    blobContainer.GetBlockBlobReference(_fileName);
                        if(blockblob && !blockblob.Exists(null, null))
                        {
                            if(_stream)
                            {
                                _stream.Position = 0;
                                System.IO.StreamReader reader = new System.IO.StreamReader(_stream);
                                str csvFileContent = reader.ReadToEnd();
            
                                blockblob.UploadText(csvFileContent,null,null,null,null);
                                blockBlob.FetchAttributes(null,null,null);
                                BlobProperties BlobProperties = blockblob.Properties;

                                if(BlobProperties.Length == _stream.Length)
                                {
                                    info(strFmt("File %1 uploaded successfully", _fileName));
                                }
                            }
                        }
                        else
                        {
                            error(strFmt("File %1 Already Exists",_fileName));
                        }
                    }
                }
                else
                {
                    error("Unable ToConnect With Storage Account");
                }
            }
            else
            {
                info("Storage Account Or Key Record Missing");
            
            }

           
        }
        catch(Exception::Error)
        {
            error("Operation Cannot Be Completed");
        }

}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Connect and upload in azure blob with azure key vault using x++

 Today, I will be sharing details about file upload in azure blob using azure vault that includes pre requisite and code snippet used to it....