Saturday, May 26, 2018

Upload files into Azure Blob in D365FO/AX7

I was tasked with creating a mod where we need to attach files related to products which were coming from Azure Blob, so I created a C# library class to perform this activity. I attached the DLL as a reference to my project and I am calling each method accordingly as per the requirement. 

ConnectionString is nothing but AccountKey which we can get it from Azure Portal where we create the new Blob folder.

Using below code you can Upload,Download, View, Delete files from Azure blob.

using System;
using System.Collections.Generic;
using System.Linq;
//using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
//using Microsoft.WindowsAzure.Storage.Auth;
//using Microsoft.WindowsAzure.Storage.Core;
//using Microsoft.Azure.Management.DataFactories.Models;
//using Microsoft.Azure.Management.DataFactories.Runtime;

namespace BlobsStorageAssembly
{

    using Dynamics.AX.Application;
    public class AccessImageBlob
    {
        public List<string> ListBlobWithStorageClientLibrary(String _subFolder, String _product, String _rootFolder, String _connectionString)
        {
    
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            var backupContainer = blobClient.GetContainerReference(_rootFolder);
            var listOfFileNames = new List<string>();
            var list = backupContainer.ListBlobs(_subFolder + "/" + _product, useFlatBlobListing: true);
            foreach (var blob in list)
            {
                listOfFileNames.Add(blob.Uri.ToString());
            }

            return listOfFileNames;
        }

        public void deleteImageFile(String _subFolder, String _product, String _rootFolder, String _connectionString)
        {

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            var backupContainer = blobClient.GetContainerReference(_rootFolder);
            
           
            var list = backupContainer.ListBlobs(_subFolder + "/" + _product, useFlatBlobListing: true);

            if (list == null || list.Count() == 0)
            {
                return;
            }
            foreach (IListBlobItem blob in list)
            {
                String fileName = Path.GetFileName(blob.Uri.ToString());
                ICloudBlob cloudBlob = backupContainer.GetBlobReferenceFromServer(_subFolder + "\\" +fileName,null,null,null);
                cloudBlob.FetchAttributes();
                
                cloudBlob.DeleteIfExists();
            }
           
        }

        public void UploadToAzureStorage(System.IO.Stream streams,String _fileNamePath, String _rootFolder, String _subFolder, String _connectionString)
        {
            
            Task t = this.UploadToAzureStorageInner(streams, _fileNamePath,  _rootFolder,  _subFolder,  _connectionString);
        }

        public async Task UploadToAzureStorageInner(System.IO.Stream streams,String _fileNamePath, String _rootFolder, String _subFolder, String _connectionString)
        {
            string filename;
            CloudStorageAccount storageAccount =
                          CloudStorageAccount.Parse(_connectionString);

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            var backupContainer = blobClient.GetContainerReference(_rootFolder);


            Uri uri = new Uri(_fileNamePath);

            filename = System.IO.Path.GetFileName(uri.LocalPath);
            CloudBlockBlob blockBlob = backupContainer.GetBlockBlobReference(_subFolder + "\\" + filename);

            await blockBlob.UploadFromStreamAsync(streams);

        }
    

    }
    
}