Read/Upload file (csv/excel) - Azure Storage D365

           Read/Upload file (csv/excel) from 

                Azure blob container - D365



Read CSV file from Azure blob container.



1) Create a method that downloads the file in to memory stream. 

We can create connection with Azure using different approaches(Go to link below)

public System.IO.Stream downloadFileFromAzureBlobContainer(System.IO.MemoryStream fileStream, str _fileName)
{
    try
    {
        fileStream = new System.IO.MemoryStream();
            
        blobClient = storageAccount.CreateCloudBlobClient();
        blobContainer = blobClient.GetContainerReference("YourcontainerName");

        blobContainer.CreateIfNotExistsAsync();

        CloudBlockBlob blockblob = blobContainer.GetBlockBlobReference(_fileName);

        if (blockblob && blockblob.Exists(null, null))
        {
           blockblob.DownloadToStream(fileStream, null, null, null);
           if (fileStream.Length > 0)
           {
               return fileStream;
           }
        }
        return null;
    }
    catch (Exception::CLRError)
    {
        CLRObject   exception       = CLRInterop::getLastException();
        CLRObject   innerException  = exception.get_InnerException();
        return null;
    }
}


2) Read the downloaded stream of file
 
public void readCSVFileFromAzure()
{
    System.IO.MemoryStream        streamFile, streamFileAzure;
    CommaStreamIo                       streamIO;

    streamFileAzure = this.downloadFileFromAzureBlobContainer(streamFile, "YourFileName");
        isHeaderRow = true;
       
    if (streamFileAzure != null && streamFileAzure.Length > 0)
    {
         streamIO = commaStreamIo::constructForRead(streamFileAzure);
         streamIO.inFieldDelimiter(',');

         if(streamIO)
         {
              while(streamIO.status() == IO_Status::Ok)
              {
                  conFile = streamIO.read();
                  if (conLen(conFile) > 0 && !isHeaderRow)
                 {
                      this.insertRecord(conFile);
                  }                   
                  isHeaderRow = false;
              }
            
          }
     }
}

Upload CSV file to Azure blob container.


1) Create a method that uploads the file memory stream to Azure. 

We can create connection with Azure using different approaches(Go to link below)

public boolean uploadFileToAzureBlobContainer(System.IO.Stream _fileContentInStream, str _fileName)
{
    try
    {
        blobContainer.CreateIfNotExistsAsync();
        CloudBlockBlob blockblob = blobContainer.GetBlockBlobReference(_fileName);
            
        if (blockblob && !blockblob.Exists(null, null))
        {
           _fileContentInStream.Position = 0; 
           blockblob.UploadFromStreamAsync(_fileContentInStream,                                                                _fileContentInStream.Length);
           return true;
        }
        return false;
    }
    catch
    {
        return false;
    }
}


2) Create memory stream of CSV file to upload.
 
public void readCSVFileFromAzure()
{      
    CommaStreamIo                       streamIO;
     
    System.IO.MemoryStream        streamFileAzure;
    boolean                                     isFileUploaded, isLineAdded;

    streamIO = commaStreamIo::constructForWrite();
    container header = ["Name", "Id", "Gender"];

    streamIO.writeExp(header);

    container line =  ["Saeed", "1", "Male"];
    streamIO.writeExp(line);
    isLineAdded = true;       

    if (isLineAdded)
    {
        streamFileAzure = streamIO.getStream();
        isFileUploaded = this.uploadFileToAzureBlobContainer(streamFileAzure, "FileName.csv");
            
        if (isFileUploaded)
        {
           info("file uploaded");
        }

    }
}


Read Excel file from Azure blob container.


1) Create a method that downloads the file memory stream from Azure. 

We can create connection with Azure using different approaches(Go to link below)

public System.IO.Stream downloadFileFromAzureBlobContainer(System.IO.MemoryStream fileStream, str _fileName)
{
    try
    {
        fileStream = new System.IO.MemoryStream();
            
        blobClient = storageAccount.CreateCloudBlobClient();
        blobContainer = blobClient.GetContainerReference("YourcontainerName");

        blobContainer.CreateIfNotExistsAsync();

        CloudBlockBlob blockblob= blobContainer.GetBlockBlobReference(_fileName);

        if (blockblob && blockblob.Exists(null, null))
        {
            blockblob.DownloadToStream(fileStream, null, null, null);
            if (fileStream.Length > 0)
           {
               return fileStream;
           }
       }
       return null;
    }
    catch (Exception::CLRError)
    {
        CLRObject   exception       = CLRInterop::getLastException();
        CLRObject   innerException  = exception.get_InnerException();
        return null;
    }
}


2) Read memory stream of Excel file.

public void importExcelFile(System.IO.Stream _streamFile)
{
   OfficeOpenXml.ExcelWorksheet        worksheet;
   OfficeOpenXml.ExcelRange            cells;
   System.IO.Stream                    stream, streamFile;
   int                                 totalRows, totalCells, rows, cellCount;   

   streamFile = this.downloadFileFromAzureBlobContainer(stream, "fileName");

   OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(streamfile);
       
   if(package)
   {
      worksheet       = package.get_Workbook().get_Worksheets().get_Item(1);
      cells           = worksheet.Cells;
      totalRows       = worksheet.Dimension.End.Row;
      totalCells      = worksheet.Dimension.End.Column;

      for (rows= 2; rows<= totalRows; rows++)
      {
          for (cellCount=1; cellCount<=totalCells; cellCount++)
          {
               //your logic goes here
              //cells.get_Item(rows, cellCounter).value;
          }
      }
   }
}


Comments

Popular posts from this blog

Cross Company queries - X++

Create multi select company lookup in report dialogue using X++

Positive pay file in D365