Upload files to an Azure Storage X++ Dynamics 365 Finance and Operations

This is a brief example of how to upload files to Azure Storage using X++ code

Please note that you will need the URL to connect to the container, as well as the access key.


Points to keep in mind:

  1. Use access keys to authenticate your applications and make requests to your Azure storage account (Manage account access keys – Azure Storage | Microsoft Docs).
  2. Store your access keys securely, for example with Azure Key Vault, and do not share them.
  3. We recommend regenerating your access keys regularly.
using BlobStorage = Microsoft.WindowsAzure.Storage;
using Microsoft.Dynamics.AX.Framework.FileManagement;
using System.IO;
 
class SavFileAzureBlobStorageJob
{
    public static void main(Args _args)
    {
        CommaStreamIo       io = CommaStreamIo::constructForWrite();
        CustTable           custTable;
        const str           connectionString = "YOUR_CONNECTION_STRING";
        const str           containerName = "YOUR_CONTAINER_NAME";
        const str           fileName = "customers.csv";
 
        BlobStorage.CloudStorageAccount storageAccount = BlobStorage.CloudStorageAccount::Parse(connectionString);
        BlobStorage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        BlobStorage.Blob.CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
        blobContainer.CreateIfNotExistsAsync();
 
        io.writeExp(['Account Number', 'Customer' , 'Currency', 'Data Area']);
 
        while select custTable
            where custTable.DataAreaId == 'USMF'
        {
            io.writeExp([custTable.AccountNum, custTable.name(), custTable.Currency, custTable.DataAreaId]);
        }
 
        BlobStorage.Blob.CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
 
        if(blockBlob && !blockBlob.Exists(null,null))
        {
            System.IO.Stream stream = iO.getStream();
            stream.Position = 0;
            System.IO.StreamReader reader = new System.IO.StreamReader(stream);
            str csvFileContent = reader.ReadToEnd();
           
            if(csvFileContent)
            {
                blockBlob.UploadText(csvFileContent,null,null,null,null);
                blockBlob.FetchAttributes(null,null,null);
                BlobStorage.Blob.BlobProperties blobProperties = blockBlob.Properties;
 
                if(blobProperties.Length != 0)
                {
                    info('File upload successful');
                }
            }
        }
        else
        {
            info('File already exits');
        }
  
    }
}

En el siguiente enlace, el cliente puede encontrar la documentación de cómo crear una cuenta de almacenamiento de Azure en su única cuenta de Azure (suscripción Pay as You Go) o AIS

https://docs.microsoft.com/en-us/azure/storage/common/storage-quickstart-create-account?toc=%2Fazure%2Fstorage%2Fblobs%2Ftoc.json&tabs=azure-portal