Translate

Sunday, January 12, 2020

Upload file in azure storage account in Dynamics 365 FnO

In this blog, I will discuss how we can upload a file to a storage account in D365FO. We normally use an Azure storage account to hold things that need to be further processed and stored in the cloud.

Please see the code below with an explanation.

Explanation of source code :
  • Getting Azure credentials from vendor parameter tables.
  • Setting the connection to access the storage account.
  • Initialise the cloud file client, which is used to configure and execute requests against the File service
  • Initialise cloud file share.
  • Check file share exists or not.
  • Initialise cloud file directory, which is a directory of files that hold directories, and directories hold files.
  • Gets a reference to a virtual blob directory beneath this container.
  • Initialise the cloud file.
  • Uploads a stream to a file. If the file already exists on the service, it will be overwritten.
Source Code (Method):

/// <summary>
    /// Upload file to Azure file storage
    /// </summary>
    /// <param name = "_fileContentInStream">File stream</param>
    /// <parmam name = "_folderName">Folder where file saves</param>
    /// <param name = "_fileName">File name</param>
    /// <returns>True or False</returns>
    public static boolean uploadFileToAzureFileStorage(System.IO.Stream _fileContentInStream , str _folderName, str _fileName)
    {
        try
        {
            //Getting Azure credentials from vendor parameter tables (in my case)
            VendParameters  vendParameter = VendParameters::find();

            //Setting connection
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials  new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("storage account name""storage account access key"); 
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount  new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
           
            //Provides a client-side logical representation of the Microsoft Azure File service.
            //This client is used to configure and execute requests against the File service.
            Microsoft.WindowsAzure.Storage.File.CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
           
            //Represents a share in the Microsoft Azure File service.
            Microsoft.WindowsAzure.Storage.File.CloudFileShare share = fileClient.GetShareReference("Your file share name");

            //If not exist, throw an error
            if (!share.Exists(nullnull))
            {
                throw error(strFmt("File share not exists."));
            }

            //Represents a directory of files which hold directories, and directories hold files.
            Microsoft.WindowsAzure.Storage.File.CloudFileDirectory cloudDir = share.GetRootDirectoryReference();

            container conFolders = str2con(_folderName, '/');

            for (int i = 1; i <= conlen(conFolders); i++)
            {
                str folderName = conpeek(conFolders, i);

                //Gets a reference to a virtual blob directory beneath this container.
                cloudDir = cloudDir.GetDirectoryReference(folderName);
                cloudDir.CreateIfNotExists(nullnull);
            }

            //Represents a file in the Microsoft Azure File service.
            Microsoft.WindowsAzure.Storage.File.CloudFile file = cloudDir.GetFileReference(_fileName);

            //Have to run Seek on Stream.
            if (_fileContentInStream.CanSeek)
            {
                _fileContentInStream.Seek(0, System.IO.SeekOrigin::Begin);
            }

            //Uploads a stream to a file. If the file already exists on the service, it will be overwritten
            file.UploadFromStream(_fileContentInStream, nullnullnull);

            return true;
        }
        catch
        {
            return false;
        }
    }