Blob storage

 using Microsoft.Azure;

using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.File;

using Microsoft.WindowsAzure.Storage.Blob;

using Microsoft.WindowsAzure.Storage.Auth;


/// <summary>

/// Helper class to get file stream and upload it to Blob.

/// Export payment file for JPMC Bank file for KSA.

/// </summary>

class GopERVendPaymOutFieUploadUnEncrypted

{

    PurchParameters purchParameters;


    CloudBlobContainer rootContainer;

    public static GopERVendPaymOutFieUploadUnEncrypted construct()

    {

        return new GopERVendPaymOutFieUploadUnEncrypted();

    }


    /// <summary>

    /// Uploading the file to blob.

    /// </summary>

    /// <param name = "_fileStream">

    /// Standard Parameter - stream

    /// </param>

    /// <param name = "_attachmentName">

    /// XML file name.

    /// </param>

    public void uploadFile(System.IO.Stream _fileStream,Str1260 _attachmentName)

    {

        

        purchParameters = PurchParameters::find();


        if(!(purchParameters.GopAccountKey && purchParameters.GopAzureStorageContainer && purchParameters.GopAzurestoragefolder 

            && purchParameters.GopStorageAccountName))

        {

            throw Error ("@Gop:GopParameterError");

        }

     

        System.IO.Stream fileStream = _fileStream;

       

        try

        {

            StorageCredentials credentials = new StorageCredentials(purchParameters.GopStorageAccountName, purchParameters.GopAccountKey);

            CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            rootContainer = blobClient.GetContainerReference(purchParameters.GopAzureStorageContainer);

            if(!rootContainer.Exists(null, null))

            {

                throw error("@Gop:GopAzureParameterError");

            }


            String255 formattedFileName = this.getFileName(_attachmentName);


            CloudBlobDirectory directory = rootContainer.GetDirectoryReference(purchParameters.GopAzurestoragefolder);


            if(this.validateAzureFolder(directory))

            {

                CloudBlockBlob blockBlob =     directory.GetBlockBlobReference(strFmt(formattedFileName));


                if (fileStream.CanSeek)

                {

                    fileStream.Seek(0, System.IO.SeekOrigin::Begin);

                }


                blockBlob.UploadFromStream(fileStream, null, null, null);

            }

            else

            {

                throw error("@Gop:GopDirectoryError");

            }

        }

        catch(Exception::CLRError)

        {

            throw error("@Gop:GopConnectionStringorStorageError");

        }

       

    }


    public String255 getFileName(String255 _attachmentName)

    {

        Str formattedFileName;

        #ISOCountryRegionCodes

        

        if(_attachmentName)

        {

            formattedFileName = LogisticsAddressCountryRegion::findByISOCode(SysCountryRegionCode::countryInfo(curext())).CountryRegionId

                                + '_' + curExt() + '_' + strRem(_attachmentName, "@ElectronicReportingMapping:XmlFileExtension")

                                    + DateTimeUtil::toFormattedStr(DateTimeUtil::getSystemDateTime(),123,

                                        DateDay::Digits2,DateSeparator::None,DateMonth::Digits2,DateSeparator::None,DateYear::Digits4,

                                            TimeSeparator::Space,TimeFormat::AMPM,DateFlags::None) + "@ElectronicReporting:XmlFileExtension";


            

            if(SysCountryRegionCode::isLegalEntityInCountryRegion([#isoCL]) )

            {

                formattedFileName = "@Gop:GopPaymFileCHL" + '_' + strReplace(formattedFileName,' ','_');

            }

            else if(SysCountryRegionCode::isLegalEntityInCountryRegion([#isoCO]))

            {

                formattedFileName = "@Gop:GopPaymFileCOL" + '_' + strReplace(formattedFileName,' ','_');

            }

            else if(SysCountryRegionCode::isLegalEntityInCountryRegion([#isoAE]))

            {

                formattedFileName = "@Gop:Gopentum" + '_' + strReplace(formattedFileName,' ','_');

            }

            else

            {

                formattedFileName = "@Gop:CITIBankKSA" + '_' + strReplace(formattedFileName,' ','_');

            }

        }


        return formattedFileName;

    }


    /// <summary>

    /// Validating existance of upload folder.

    /// </summary>

    /// <param name = "_directory"> directory </param>

    /// <returns> Boolean </returns>

    public boolean validateAzureFolder(Microsoft.WindowsAzure.Storage.Blob.CloudBlobDirectory _directory)

    {

        boolean ret = false;


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

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


        while(lstEnumarator.MoveNext())

        {

            ret = true;

            break;

        }


        return ret;

    }


}

Comments

Popular posts from this blog

Price disc agreement

Sample integration

Create ledger journals in D365FO using X++ + LedgerDimensionACType