Uploading attachment file to Microsoft azure storage account blob.
- Goto https://portal.azure.com
- Goto Storage Account
Click on Add
- Fill details and click Review + Create
- Click on create. It will deploy the storage account.
- Go to newly created storage account go to access key .Key 1 and key 2 or connections strings will be used to access this storage account.
- Go to container and click Container
- Fill the name and click ok.
- Now we will create new runnable class in D365FO to upload file to newly created container.using Microsoft.WindowsAzure.Storage;using Microsoft.WindowsAzure.Storage.Blob;class UploadFileToAzureBlob{public static void main(Args _args){DocuRef docuref;/*const str connectionString = "";*/const str containerName = "selflearningcontainer";str storageAccountName = "selflearningtest";str key = "”;System.IO.Stream attachmentStream = null;try{var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, key);CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);if(storageAccount){CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);blobContainer.CreateIfNotExistsAsync();changecompany('USMF'){SalesId salesId = '001835';SalesTable SalesTable;//SalesTable salesTable = SalesTable::find(salesId);select SalesId from salestable where SalesTable.salesid == '001835';select firstonly docuref where docuref.RefTableid == salesTable.TableId&& docuref.RefRecid == salesTable.RecId;}if(docuref.isValueAttached()){docuValue = docuref.docuValue();CloudBlockBlob blockblob = blobContainer.GetBlockBlobReference("testingblob");if(blockblob && !blockblob.Exists(null, null)){var storageProvider = docuvalue.getStorageProvider();if(storageProvider != null){var docContents = storageProvider.GetFile(docuValue.createLocation());attachmentStream = docContents.Content;if(attachmentStream){blockblob.UploadFromStreamAsync(attachmentStream).Wait();blockBlob.FetchAttributes(null,null,null);BlobProperties BlobProperties = blockblob.Properties;if(BlobProperties.Length == attachmentStream.Length){info("file uploaded successfully");}}}}else{info("file already exists");}}}else{error("unable to connect azure file storage with the provided credentials");}}catch(Exception::Error){error("Operation cannot be completed");}}}
- Now we will run the class