Upload file to cloud/azure blob storage

Class to upload file stream to a cloud
Using Microsoft.WindowsAzure.Storage;
Using Microsoft.WindowsAzure.Storage.Blob;
/// <summary>
/// Uplpoads file stream to a cloud
/// </summary>
class VKUploadToCloud
{
    str storageConnStr;
    str containerName;

    public static VKUploadToCloud construct(str _storageConnStr, str _containerName = '')
    {
        VKUploadToCloud uploadToCloud = new VKUploadToCloud();

        uploadToCloud.parmStorageConnStr(_storageConnStr);
        uploadToCloud.parmContainerName(_containerName);

        return uploadToCloud;
    }

    public str parmStorageConnStr(str _storageConnStr = storageConnStr)
    {
        storageConnStr = _storageConnStr;

        return storageConnStr;
    }

    public str parmContainerName(str _containerName = containerName)
    {
        containerName = _containerName;

        return containerName;
    }

    /// <summary>
    /// Uploads stream to a cloud
    /// </summary>
    /// <param name = "_blobName">file anme</param>
    /// <param name = "_reportMs">stream</param>
    /// <returns>URL</returns>
    public URL upload(str _blobName, System.IO.MemoryStream _reportMs)
    {
        System.Exception    ex;
        URL                    downloadLink = '';

        try
        {
            CloudBlobClient         cloudBlobClient;
            CloudBlobContainer      cloudBlobContainer;
            CloudStorageAccount     cloudStorageAccount;
            CloudBlockBlob          cloudBlockBlob;

            cloudStorageAccount     = CloudStorageAccount::Parse(storageConnStr);
            cloudBlobClient         = cloudStorageAccount.CreateCloudBlobClient();
            cloudBlobContainer      = cloudBlobClient.GetContainerReference(containerName);
            cloudBlockBlob          = cloudBlobContainer.GetBlockBlobReference(_blobName);

            cloudBlockBlob.UploadFromStream(_reportMs, null, null, null);
            downloadLink = cloudBlockBlob.Uri.AbsoluteUri;
        }
        catch(Exception::CLRError)
        {
            ex = ClrInterop::getLastException();
            if (ex != null)
            {
                ex = ex.get_InnerException();
                if (ex != null)
                {
                    error(ex.ToString());
                }
            }
        }
        catch(ex)
        {
            error(ex.Message);
        }

        return downloadLink;
    }

}
Usage:
            System.Byte[]                    reportBytes = new System.Byte[0]();
            reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmReportContract().parmReportPath(),
                parameterValueArray,
                printerSettings.fileFormat(),
                printerSettings.deviceinfo());

            if (reportBytes)
            {
                System.IO.MemoryStream     stream = new System.IO.MemoryStream(reportBytes);
                URL                        downloadLink;
                VKUploadToCloud            uploadToCloud = VKUploadToCloud::construct(
                        'DefaultEndpointsProtocol=https;AccountName={AccountName};AccountKey={AccountKey};EndpointSuffix=core.windows.net',
                        'invoices'
                    );
                
                downloadLink = uploadToCloud.upload(fileName, stream);
            }
To browse result:
- Open connect dialog
- Storage account or service
- Account name and key
- populate Display name, Account name, Account key


 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units and "buy me a coffee" link.
Join us.

Blog Tags