Tuesday 30 April 2019

Get all files (Blobs) in a list from Azure storage container in D365 using X++ code

Now a days Microsoft have moved everything to Azure. But in D365 fetching blobs as a list from a storage container is still tricky. Here in this blog code you'll be able to get all the blobs from Azure container in a single list.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

class Azure
{
    public void processAzureBlobFiles()
    {      
         var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("Storage account name", "Access key");

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

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

CloudBlobContainer blobContainer = blobClient.GetContainerReference("Container Name");

System.Collections.IEnumerable list = new System.Collections.Generic.List<str>();
list = blobContainer.ListBlobs(null, false, 0, null, null);
System.Collections.IEnumerator listEnumerator = list.getEnumerator();

while  (listEnumerator.moveNext())
{
CloudBlockBlob blob = listEnumerator.get_Current();

if (blob.Exists(null, null))
{
        str file = blob.StorageUri.PrimaryUri.AbsoluteUri;
str fileName = blob.Name;
}
}
    }
}

"Storage account name" Can be taken from Azure -> Storage accounts -> Settings -> Access keys -> Storage account name

"Access key" Can be taken from Azure -> Storage accounts -> Settings -> Access keys -> Key1 or Key2


"Container Name" Can be taken from Azure -> Storage accounts -> Blobs -> Container name

Thanks,
DAXBuddy

No comments:

Post a Comment