D365FO. Working with Azure File storage.

Azure Storage - Files

Current version of AX uses Azure Blob storage for various things like document handling, retail CDX files, DIXF and Excel add-in. You can find several blogs explaining how to upload and download files to Blob, SharePoint or temporary storage. However, what about file shares?

Azure File storage implements SMB 3.0 protocol and could be easily mapped to your local computer. You need just a few minutes to create new storage account and mount it, watch this how-to video for details.

To read file from newly created share we can use next code:

using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.File;

class RunnableClass1
{
    public static void main(Args _args)
    {
        System.IO.MemoryStream memoryStream;

        var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials('AzureStorageAccountName', 'AzureStorageAccountKey');

        CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);

        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

        CloudFileShare share = fileClient.GetShareReference('AzureStorageShareName');

        if (share.Exists(null, null))
        {
            CloudFileDirectory rootDir = share.GetRootDirectoryReference();

            CloudFileDirectory fileDir = rootDir.GetDirectoryReference('folder');

            if (fileDir.Exists(null, null))
            {
                CloudFile file = fileDir.GetFileReference('file.txt');

                if (file.Exists(null, null))
                {
                    memoryStream = new System.IO.MemoryStream();
                    file.DownloadToStream(memoryStream, null, null, null);
                }
            }
        }
    }
}

References:

Edited:

Azure File Storage client source code could be found on GitHub

7 thoughts on “D365FO. Working with Azure File storage.

  1. Ajay kumar Pollamsetty September 23, 2017 / 11:00 pm

    Hi,
    Thanks for sharing this useful tip.
    Last in the code, cloud file is being downloaded to memory stream. If my stream has a pdf data, is there a way I can directly open this pdf in a new browser tab without saving to local drives?

  2. Lucas Figueredo December 3, 2018 / 3:58 pm

    Hi Ievgen,
    Thanks for sharing your code, it was really helpful.
    I’m wondering if you tried to copy a file to another directory, inside the same file share. I found some documentation about TransferManager class but seems that the namespace Microsoft.WindowsAzure.Storage.DataMovement is not available inside AX.

    Do you have any idea?

  3. Ansar Basha Syed January 20, 2019 / 2:19 pm

    Hi Ievgen,

    Hope you are doing good.

    I have used your code to attach files using DocumentManagement::attachFile() class, file is getting attached but it’s size is 0 bytes, when try to open file it says file is corrupted, i have requirement to attach all file types, i’m retrieving files from blobstorage with your code.

    I used FileUploadTemporaryStorageStrategy to upload file and attached to document it works fine.

    Can you share your thoughts.

    Thanks & Regards,
    Ansar Basha.S

    • Ievgen Miroshnikov January 20, 2019 / 6:26 pm

      Are you talking about the code from blog post or from GitHub?

      • Ievgen Miroshnikov January 20, 2019 / 6:59 pm

        Then look at code on GitHub, probably you have this issue because you need to reset memory string to beginning using seek or position.

Leave a Reply to Ievgen Miroshnikov Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s