X++ code to upload files from Dynamics 365 to Azure Blob Storage
using BlobStorage = Microsoft.WindowsAzure.Storage;
using Microsoft.Dynamics.AX.Framework.FileManagement;
using System.IO;
class RunnableClass3
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
CommaStreamIo io = CommaStreamIo::constructForWrite();
CustTable custTable;
const str connectionString = "DefaultEndpointsProtocol=https;AccountName=sarathbujala;AccountKey=6bCwPjV6Co3bIFgOTv6sQs+/rnXbcJbwe1gPfRofTH8zBXy0AteMv34xruT0pSrUu5V0ZxO8Li4X+AStVXATBQ==;EndpointSuffix=core.windows.net";
const str containerName = "sarathcontainer";
const str fileName = @"C:\Users\Administrator\Desktop\Mydetails.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(['CustomerName']);
while select custTable
where custTable.DataAreaId == 'USRT'
{
io.writeExp([custTable.AccountNum]);
}
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');
}
}
}
Comments
Post a Comment