Monday, 30 September 2019

CSV to Azure File Storage


using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;
using Microsoft.WindowsAzure.Storage.StorageException;
#File
class WriteCSVFileInAzureFileStorage
{
    Dynamics.AX.Application.Exception exception;



    public static void main(Args _args)
    {
        CommaStreamIo io = CommaStreamIo::constructForWrite();
        str     fileName = 'Cust_DE.csv',
       fileContent;
        CustTable   custTable;

        // Write header
        io.writeExp(['Customer', 'Account number', 'Currency', 'DataAreaId']);

        // Write line
        while select custTable
     where custTable.DataAreaId == 'USMF'
        {
            io.writeExp([custTable.name(),
       custTable.AccountNum,
       custTable.Currency,
       custTable.DataAreaId]);
        }

        // Set stream
        System.IO.Stream stream = io.getStream();
        stream.Position = 0;

        // Set stream reader
        System.IO.StreamReader sReader = new System.IO.StreamReader(stream);

        // Set file contentn string
        fileContent = sReader.ReadToEnd();

        //// Save file
        //File::SendStringAsFileToUser(fileContent, fileName);
        Dynamics.AX.Application.Exception exception;
        infolog.add(exception::Info, "Azure Storage File Sample");


        WriteCSVFileInAzureFileStorage writeCSVFileInAzureFileStorage = new WriteCSVFileInAzureFileStorage();
        WriteCSVFileInAzureFileStorage::BasicAzureFileOperations(fileContent,fileName);

    }

    private static void BasicAzureFileOperations(str _fileContent,str _fileName)
    {
        //const str DemoShare = "democsvfileshare";
        const str DemoShare = "agreementfileshare";
        //const str DemoDirectory = "DemocsvFileDirectory";
        const str DemoDirectory = "AgreementcsvFileDirectory";
        //const str DemoFile = "demofile";
       // const str ImageToUpload = @'C:\Users\Usere4627ee6be0\Desktop\VS error.PNG'; //"HelloWorld.png";
        str FileContents = _fileContent;
        str fileName = _fileName;

        Dynamics.AX.Application.Exception exception;
        StorageException storageException = new Microsoft.WindowsAzure.Storage.StorageException();
       
        // Retrieve storage account information

        Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials('siyedev813pu23e0d5d398f4', 'k03yWIis7PV3yIIUNxXKseUQZ7GSQMW3Ch0Ri2vAGmWVbov/dcj/7mzjpbAYrwA46LSuvKOWZKyBpA60B+zXpA==');

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

        // Create a file client for interacting with the file service.

        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

        // Create a share for organizing files and directories within the storage account.

        CloudFileShare share = fileClient.GetShareReference(DemoShare);
           
        try
        {
            share.CreateIfNotExistsAsync();
        }
        catch (storageException)
        {
            infolog.add(exception::Error, "Unable to connect to Azure File Storage");
            throw exception::Error;
        }

        // Get a reference to the root directory of the share.

        CloudFileDirectory root = share.GetRootDirectoryReference();

        // Create a directory under the root directory

        CloudFileDirectory dir = root.GetDirectoryReference(DemoDirectory);
        dir.CreateIfNotExistsAsync();

        // Creating a new file in the directory created above and writing contents to the file
        // Also delete if exists

        CloudFile file = dir.GetFileReference(fileName);
        file.DeleteIfExistsAsync();
        file.UploadTextAsync(FileContents);

        // Uploading a local file to the directory created above

        //CloudFile file = dir.GetFileReference(ImageToUpload);
        //file.UploadFromFileAsync(ImageToUpload, System.IO.FileMode::Open);
    }

}

1 comment:

  1. Do you need Finance? Are you looking for Finance? Are you looking for finance to enlarge your business? We help individuals and companies to obtain finance for business expanding and to setup a new business ranging any amount. Get finance at affordable interest rate of 3%, Do you need this finance for business and to clear your bills? Then send us an email now for more information contact us now via (financialserviceoffer876@gmail.com) whats-App +918929509036 Dr James Eric Finance Pvt Ltd Thanks

    ReplyDelete