D365/AX7: Read & Download a File from Azure Blob Storage Using X++

Part-1: Azure BLOB storage with Dynamics 365 for operations

Requirement – Reading a file & getting the memory stream of file from Azure Blob Storage in Microsoft dynamics 365 for finance and operations using X++.

Prerequisite – Azure Blob Storage along with a valid connection string.

Sample Code –

using Microsoft.WindowsAzure.Storage;
 Using Microsoft.WindowsAzure.Storage.Blob;
 class RunnableClassBlobStorageDownload
 {
     /// 
     /// Runs the class with the specified arguments.     /// 

     /// 
The specified arguments.
     public static void main(Args _args)
     {
         CloudBlobDirectory  cloudBlobDirectory;
         CloudBlobClient  cloudBlobClient;
         CloudBlobContainer  cloudBlobContainer;
         CloudStorageAccount  cloudStorageAccount;
         cloudStorageAccount  = CloudStorageAccount::Parse("Azure Blob Connection String");
         cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
         cloudBlobContainer  = cloudBlobClient.GetContainerReference("files"); // 
         System.Collections.IEnumerable lstEnumarable =   cloudBlobContainer.ListBlobs(null, false, 0, null, null);
         System.Collections.IEnumerator lstEnumarator = lstEnumarable.GetEnumerator();
         List filenames = new List(Types::String);
         while(lstEnumarator.MoveNext())
         {
             IListBlobItem item = lstEnumarator.Current;
             if(item is CloudBlockBlob)
             {
                 CloudBlockBlob blob = item;
                 System.IO.StreamReader  reader = new System.IO.StreamReader(blob.OpenRead(null, null, null));
                 Info(reader.ReadToEnd());           
             }
         }
     }
 }

One thought on “D365/AX7: Read & Download a File from Azure Blob Storage Using X++”

Leave a Reply