Wednesday, January 25, 2023

Azure copy & move files from Blob storage in D635FO X++

 using Microsoft.Azure;

using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Blob;


using Microsoft.WindowsAzure.Storage.Auth;

using Microsoft.Dynamics.AX.Framework.FileManagement;

using fileShareStorage = Microsoft.WindowsAzure.Storage.File;


class SANAzureBlobFileMove

{

    /// <summary>

    /// Runs the class with the specified arguments.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>

    public static void main(Args _args)

    {

        SAN_FileIntegrationParameters   fileParameters;

        str                             azureBlobFolderPath;

        CloudBlobClient                 blobClient;

        Filename                        folderPath ='DailyPortalOrder';

        Name                            containerReference = 'dp';

        str                             fileName;

        #File


        fileName = 'TestFile.Txt';

        fileParameters = SAN_FileIntegrationParameters::find();


        KeyVaultCertificateTable    certificateTable= KeyVaultCertificateTable::findByName(fileParameters.AuthorizationSecretName);

        str keyvaultValue   = KeyVaultCertificateHelper::getManualSecretValue(certificateTable.RecId);


        if (fileParameters.AuthorizationSecretName == "")

        {

            throw error("Key vault authorization secert must be specified");

        }


        try

        {

            System.Uri  uri = new System.Uri(keyvaultValue);

            blobClient = new CloudBlobClient(uri);

            if (folderPath)

            {

                azureBlobFolderPath = containerReference + #FilePathDelimiter + folderPath;

            }

            else

            {

                azureBlobFolderPath = containerReference;

            }

            

            CloudBlobContainer blobcontainer = blobClient.GetContainerReference(azureBlobFolderPath);

            CloudBlobContainer sourceBlobcontainer = blobClient.GetContainerReference(containerReference);

            CloudBlobContainer destBlobcontainer = blobClient.GetContainerReference(containerReference);

            /*

            CloudBlobDirectory  cloudBlobDirectory;

            Container  con;

            cloudBlobDirectory = blobcontainer.GetDirectoryReference("DailyPortalOrder");

            System.Collections.IEnumerable lstEnumarable = cloudBlobDirectory.ListBlobs(false, 0, null, null);

            System.Collections.IEnumerator lstEnumarator = lstEnumarable.GetEnumerator();

            List filenames = new List(Types::String);

            while(lstEnumarator.MoveNext())

            {

                IListBlobItem item = lstEnumarator.Current;

                if(item is CloudBlockBlob)

                {

                    CloudBlockBlob blob = item;

                    blob.FetchAttributes(null, null, null);

                    con = str2con(blob.name, "/");

                    filenames.addStart(conPeek(con,conlen(con)));

                    info(blob.name);

                    //System.IO.StreamReader  reader = new System.IO.StreamReader(blob.OpenRead(null, null, null));

                    //Info(reader.ReadToEnd());

                }

            }

            */


            System.Byte[] reportBytes = new System.Byte[0]();

            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

            reportBytes = enc.GetBytes("SAN TEST TEXT");

            System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes);

            CloudBlockBlob      blockBlob = blobcontainer.GetBlockBlobReference(fileName);

            blockBlob.UploadFromByteArray(stream.ToArray(),0,stream.ToArray().Length, null,null,null);


            info(strfmt("updloaded to file path %1 %2 %3", fileName, fileParameters.AzureStorageAccountName, azureBlobFolderPath));


            CloudBlobDirectory sourceCloudBlobDirectory = sourceBlobcontainer.GetDirectoryReference("DailyPortalOrder/");

            CloudBlobDirectory destinationCloudBlobDirectory = destBlobcontainer.GetDirectoryReference("DailyPortalOrder/archive");

            CloudBlockBlob SourceBlob = sourceCloudBlobDirectory.GetBlockBlobReference(fileName);

            CloudBlockBlob destinationBlob = destinationCloudBlobDirectory.GetBlockBlobReference(fileName);

            destinationBlob.UploadFromStream(blockBlob.OpenRead(null,null,null),null,null,null);

            blockBlob.Delete(0,null,null,null);

            

            CloudBlockBlob      blockBlob = blobcontainer.GetBlockBlobReference(_fileName);

            blockBlob.UploadFromByteArray(_memoryStream.ToArray(),0,_memoryStream.ToArray().Length, null,null,null);

        }

        catch(Exception::Error)

        {

            error("Error on file processing");

        }

    }


}


No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...