Reading CSV/text file from Azure storage blob

 Reading CSV/text file from Azure storage blob


Perquisite:

1. Create Azure storage account

2. Blob container

3. Folders inside the blob container

4. CSV file inside the folder

5. Access key


How to create Azure storage account?

1. Login on https://portal.azure.com

2. To create storage account click on Storage account


3. Follow the wizard instruction to create storage account


4. Once storage account name created, create container, folder and place file inside folder.

click on storage browser



5. Place file inside folder


Now we have created Storage account, container, folder and file.

6. How to get storage account access key ?

Go to storage account main page --> Security + networking --> Access Keys






 


Establishing connection between D365 F&O and Azure storage account, reading azure storage file from D365 F&O using X++

1. Create runnable class - XXXTestAzureBlob

2. Method to establish connection - connectToAzureStorageAccountBlob()

3. Reading the storage location container, folder and file - getBlobStorageFileName()

4. Reading the file details - readFileValueFromBlob()


using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Blob;

public class XXXTestAzureBlob

{

    #File

    #define.delimiterField(',')

    public static void main(Args s)

    {

        CloudBlobContainer cloudBlobContainer;

 

        XXXTestAzureBlob testAzureBlob  = new XXXTestAzureBlob();

        cloudBlobContainer= testAzureBlob.connectToAzureStorageAccountBlob();

 

        testAzureBlob.getBlobStorageFileName(cloudBlobContainer);

        testAzureBlob.readFileValueFromBlob(cloudBlobContainer);

    }

 

    public CloudBlobContainer connectToAzureStorageAccountBlob()

    {

        CloudBlobClient         cloudBlobClient;

        CloudBlobContainer      cloudBlobContainer;

        CloudStorageAccount     cloudStorageAccount;

 

        cloudStorageAccount =

            CloudStorageAccount::Parse("DefaultEndpointsProtocol=https;AccountName=XXXAccount_Name5ec66837f;AccountKey=xxbwxh7x wxvwh8GM3ovlByVIhcDM8oSWOkZkOWgysHvwgxvwg566+788==;EndpointSuffix=core.windows.net");


//  Parse() parameter is the access key. Refer point# 6 to get access key 

        cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

        CloudBlobContainer = cloudBlobClient.GetContainerReference("testimport"); // Container name

 

        info(strFmt("Storage container Name: %1",CloudBlobContainer.Name));

 

        return CloudBlobContainer;

 

    }

 

    public void getBlobStorageFileName(CloudBlobContainer  _cloudBlobContainer)

    {

        CloudBlobDirectory  cloudBlobDirectory;

        container           fileCon;

 

        CloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference("testFolder");//folder name

 

        System.Collections.IEnumerable blobIEnumerable = CloudBlobDirectory.ListBlobs(false,0,null,null);

        System.Collections.IEnumerator blobIEnumerator = blobIEnumerable.GetEnumerator();

 

        List fileNames = new List(Types::String);

        while(blobIEnumerator.MoveNext())

        {

            IListBlobItem item = blobIEnumerator.Current;

            if(item is CloudBlockBlob)

            {

                CloudBlockBlob blob = item;

                blob.FetchAttributes(null,null, null);

                fileCon = str2con(blob.Name, "/");

                fileNames.addStart(conPeek(fileCon,conLen(fileCon)));

                info(strFmt("File name: %1",conPeek(fileCon,conLen(fileCon))));


// Depend on the business requirements "conPeek(fileCon,conLen(fileCon))" use to store values in the table  

 

            }

 

        }

    }

 

    public void readFileValueFromBlob(CloudBlobContainer  _cloudBlobContainer)

    {

        System.IO.MemoryStream memoryStream;

        TextStreamIo           textStreamIo;

        container              rec;

       

        CloudBlobDirectory      cloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference("testFolder");//folder name

        CloudBlockBlob          blob = cloudBlobDirectory.GetBlockBlobReference("csvFileUpload.csv"); //file name

       

        memoryStream = new System.IO.MemoryStream();

        blob.DownloadToStream(memoryStream, null, null, null);

 

        textStreamIo = TextStreamIo::constructForRead(memoryStream);

        textStreamIo.inFieldDelimiter(#delimiterField);

        textStreamIo.inRecordDelimiter(#delimiterCRLF);

 

        while (!textStreamIo.status())

        {

            rec = textStreamIo.read();

 

            if (conLen(rec))

            {

                info(strFmt("First value: %1",conPeek(rec, 1)));  // Blob file data

                info(strFmt("Second value: %1",conPeek(rec, 2)));

                info(strFmt("Third value: %1",conPeek(rec, 3)));

            }

        }

    }


Sample file data:











Comments

Popular posts from this blog

Execute D365 F&O SSRS report with parameter and upload report output on Azure blob storage using X++

Microsoft D365 F&O: Remove custom models from deployable package

Generate Text/CSV/DAT file using X++ and upload on Azure blob storage