Upload and Download File from BLOB Storage Account in D365 F&O using X++
Scenario
In my case, a third-party tool was uploading documents to a Blob storage account, and I needed to use these documents within D365 F&O. The tasks included:
- Uploading a file to Azure
- Downloading a file from Azure
We will be using the Share File Management technique.
Case #1: Uploading a File to Azure
Note
We are using the
PurchParameter
table to extract URLs and other folder-related details (customized fields). You can use your own folder name to access the Azure Blob folder. Credential details (e.g., vault key, authorization key) have already been extracted and stored in our custom table and are fetched from D365 F&O through Azure parameters.div Example
- Extracting Storage Credentials:
xppstorageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials( SystemParameters::find().XYZStorageAccountName, purchParameters.XYZKeyVaultSecret ); // Alternatively CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string of the storage account");
- Uploading a File:
xpppublic void uploadFileToBlobStorage() { try { // Extract storage credentials Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials; storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials( SystemParameters::find().XYZStorageAccountName, purchParameters.XYZKeyVaultSecret ); // Create cloud storage account Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true); // Create the blob client Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve a reference to a container Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("your-container-name"); // Create the container if it doesn't already exist container.CreateIfNotExists(); // Retrieve reference to a blob Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference("your-blob-name"); // Upload file to blob storage using (var fileStream = new System.IO.FileStream("path-to-your-file", System.IO.FileMode.Open)) { blockBlob.UploadFromStream(fileStream); } info("File uploaded successfully to Azure Blob Storage."); } catch (Exception::Error e) { error(strFmt("Error uploading file to Azure Blob Storage: %1", e.message())); } }
Case #2: Downloading a File from Azure
Note
Similarly, we use the
PurchParameter
table to extract URLs and other folder-related details. Credential details are fetched from D365 F&O through Azure parameters.div Example
- Extracting Storage Credentials:
xppstorageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials( SystemParameters::find().XYZStorageAccountName, purchParameters.XYZKeyVaultSecret );
- Downloading a File:
xpppublic void downloadFileFromBlobStorage() { try { // Extract storage credentials Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials; storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials( SystemParameters::find().XYZStorageAccountName, purchParameters.XYZKeyVaultSecret ); // Create cloud storage account Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true); // Create the blob client Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve a reference to a container Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("your-container-name"); // Retrieve reference to a blob Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference("your-blob-name"); // Download blob to file using (var fileStream = new System.IO.FileStream("path-to-save-your-file", System.IO.FileMode.Create)) { blockBlob.DownloadToStream(fileStream); } info("File downloaded successfully from Azure Blob Storage."); } catch (Exception::Error e) { error(strFmt("Error downloading file from Azure Blob Storage: %1", e.message())); } }
By following these steps and using the provided div examples, you can efficiently upload and download files to and from Azure Blob Storage within D365 F&O. This ensures seamless integration and processing of documents uploaded by third-party tools.
No comments:
Post a Comment