Today I will show you how you can perform multiple operation on any file in Business central by accessing of Azure blob storage.
I going to work on some image file, where I will store those files into blob storage and after reading that file move that file into another folder and delete from the source folder.
Before stating you should consider your permission using the Azure blob storage, you can consult with your Admin to provide the same. and follow the link how to create Azure blob storage. Click Me
In first step we will follow how we will create blob storage account in Azure.
- Open your azure account through this link Azure Portal and create blob storage account, I am not going into details how you will create your Azure blob storage account, that you read on Click me link.
- Now you need to copy your container Access key along with other details which will be used in Business central see the below image
- In the next image you can see I have created two containers one for store the image file in Output container and other one when I read those file from output container and archive in processed container.
- Move to business central and do some coding.
- In business central following are the objects which are responsible to manage the blob storage and perform the operations,
ABSBlobClient: Codeunit "ABS Blob Client"; ABSContainerClient: Codeunit "ABS Container Client"; StorageServiceAuthorization: Codeunit "Storage Service Authorization"; ABSOperationResponse: Codeunit "ABS Operation Response"; ABSContainerContent: Record "ABS Container Content";
- There are two codeunit available to perform the actions from business central to blob storage
- “ABS Container Client” > this is use for to delete and creating the container in blob storage account.
- “ABS Blob Client” > and this one for putting or reading the files in your Blob Storage’s container account.
- There are two codeunit available to perform the actions from business central to blob storage
- In next step we will try to import some file in Azure blob storage’s container
// This one import the file into Azure blob storage container local procedure UploadfileintoContainers() var StorageServiceAuthorization: Codeunit "Storage Service Authorization"; ABSBlobClient: Codeunit "ABS Blob Client"; ABSOperationResponse: Codeunit "ABS Operation Response"; Authorization: Interface "Storage Service Authorization"; instr: Instream; AccountAccessKey: text; AccountName: Text; AccountContainer: Text; filename: Text; begin AccountAccessKey := 'your key here'; AccountName := 'Storage account name'; AccountContainer := 'storage container name'; Authorization := StorageServiceAuthorization.CreateSharedKey(AccountAccessKey); ABSBlobClient.Initialize(AccountName, AccountContainer, Authorization); filename := Uploadfile(instr); ABSOperationResponse := ABSBlobClient.PutBlobBlockBlobStream(filename, instr); if ABSOperationResponse.IsSuccessful() then Message('File has been imported successfully.'); end; procedure Uploadfile(var IStream: Instream): text[100] var FromFile: Text[100]; FileMgt: Codeunit "File Management"; begin UploadIntoStream('Please choose any file.', '', '', FromFile, IStream); if FromFile <> '' then exit(FileMgt.GetFileName(FromFile)); end;
7. After putting the file in container. Its time to read that file from container.
local procedure ReadContainerfile() var StorageServiceAuthorization: Codeunit "Storage Service Authorization"; ABSBlobClient: Codeunit "ABS Blob Client"; ABSOperationResponse: Codeunit "ABS Operation Response"; Authorization: Interface "Storage Service Authorization"; instr: Instream; AccountAccessKey: text; AccountName: Text; AccountContainer: Text; filename: Text; ABSContainerContent: Record "ABS Container Content"; begin AccountAccessKey := 'your key here'; AccountName := 'Storage account name'; AccountContainer := 'storage container name'; Authorization := StorageServiceAuthorization.CreateSharedKey(AccountAccessKey); ABSBlobClient.Initialize(AccountName, AccountContainer, Authorization); ABSOperationResponse := ABSBlobClient.ListBlobs(ABSContainerContent); if ABSOperationResponse.IsSuccessful() then begin ABSContainerContent.Reset(); if ABSContainerContent.FindSet() then repeat Clear(instr); ABSOperationResponse := ABSBlobClient.GetBlobAsStream(ABSContainerContent.Name, instr); if ABSOperationResponse.IsSuccessful() then Message('File read successfully.'); until ABSContainerContent.Next() = 0; end; end;
8. Now we can do the Move and delete operation into container.
local procedure MoveandDeleteContainerFile() var StorageServiceAuthorization: Codeunit "Storage Service Authorization"; ABSBlobClient: Codeunit "ABS Blob Client"; ABSOperationResponse: Codeunit "ABS Operation Response"; Authorization: Interface "Storage Service Authorization"; instr: Instream; AccountAccessKey: text; AccountName: Text; AccountContainer: Text; filename: Text; ABSContainerContent: Record "ABS Container Content"; ProcessContainerName: Text; ABSBlobClient_2: Codeunit "ABS Blob Client"; begin AccountAccessKey := 'your key here'; AccountName := 'Storage account name'; AccountContainer := 'storage container name'; Authorization := StorageServiceAuthorization.CreateSharedKey(AccountAccessKey); ABSBlobClient.Initialize(AccountName, AccountName, Authorization); ABSOperationResponse := ABSBlobClient.ListBlobs(ABSContainerContent); if ABSOperationResponse.IsSuccessful() then begin ABSContainerContent.Reset(); if ABSContainerContent.FindSet() then repeat Clear(instr); // Reading the file from output container ABSOperationResponse := ABSBlobClient.GetBlobAsStream(ABSContainerContent.Name, instr); Clear(ABSBlobClient_2); if ABSOperationResponse.IsSuccessful() then begin // Now Initilize Process container where I will put that file from source container ABSBlobClient_2.Initialize(AccountName, ProcessContainerName, Authorization); // Putting file here from source container. ABSOperationResponse := ABSBlobClient_2.PutBlobBlockBlobStream(ABSContainerContent.Name, instr); if ABSOperationResponse.IsSuccessful() then begin // Deleting the same file from source container. ABSOperationResponse := ABSBlobClient.DeleteBlob(ABSContainerContent.Name); if ABSOperationResponse.IsSuccessful() then Message('File has been deleted from the original container.'); end; end; until ABSContainerContent.Next() = 0; end; end;
Those are above operations to Put, Read, move and delete from business central to Azure blob storage.
The whole code unit you can find from here.
codeunit 61903 "Perform Blob files" { // This one import the file into Azure blob storage container local procedure UploadfileintoContainers() var StorageServiceAuthorization: Codeunit "Storage Service Authorization"; ABSBlobClient: Codeunit "ABS Blob Client"; ABSOperationResponse: Codeunit "ABS Operation Response"; Authorization: Interface "Storage Service Authorization"; instr: Instream; AccountAccessKey: text; AccountName: Text; AccountContainer: Text; filename: Text; begin AccountAccessKey := 'your key here'; AccountName := 'Storage account name'; AccountContainer := 'storage container name'; Authorization := StorageServiceAuthorization.CreateSharedKey(AccountAccessKey); ABSBlobClient.Initialize(AccountName, AccountContainer, Authorization); filename := Uploadfile(instr); ABSOperationResponse := ABSBlobClient.PutBlobBlockBlobStream(filename, instr); if ABSOperationResponse.IsSuccessful() then Message('File has been imported successfully.'); end; procedure Uploadfile(var IStream: Instream): text[100] var FromFile: Text[100]; FileMgt: Codeunit "File Management"; begin UploadIntoStream('Please choose any file.', '', '', FromFile, IStream); if FromFile <> '' then exit(FileMgt.GetFileName(FromFile)); end; local procedure ReadContainerfile() var StorageServiceAuthorization: Codeunit "Storage Service Authorization"; ABSBlobClient: Codeunit "ABS Blob Client"; ABSOperationResponse: Codeunit "ABS Operation Response"; Authorization: Interface "Storage Service Authorization"; instr: Instream; AccountAccessKey: text; AccountName: Text; AccountContainer: Text; filename: Text; ABSContainerContent: Record "ABS Container Content"; begin AccountAccessKey := 'your key here'; AccountName := 'Storage account name'; AccountContainer := 'storage container name'; Authorization := StorageServiceAuthorization.CreateSharedKey(AccountAccessKey); ABSBlobClient.Initialize(AccountName, AccountContainer, Authorization); ABSOperationResponse := ABSBlobClient.ListBlobs(ABSContainerContent); if ABSOperationResponse.IsSuccessful() then begin ABSContainerContent.Reset(); if ABSContainerContent.FindSet() then repeat Clear(instr); ABSOperationResponse := ABSBlobClient.GetBlobAsStream(ABSContainerContent.Name, instr); if ABSOperationResponse.IsSuccessful() then Message('File read successfully.'); until ABSContainerContent.Next() = 0; end; end; local procedure MoveandDeleteContainerFile() var StorageServiceAuthorization: Codeunit "Storage Service Authorization"; ABSBlobClient: Codeunit "ABS Blob Client"; ABSOperationResponse: Codeunit "ABS Operation Response"; Authorization: Interface "Storage Service Authorization"; instr: Instream; AccountAccessKey: text; AccountName: Text; AccountContainer: Text; filename: Text; ABSContainerContent: Record "ABS Container Content"; ProcessContainerName: Text; ABSBlobClient_2: Codeunit "ABS Blob Client"; begin AccountAccessKey := 'your key here'; AccountName := 'Storage account name'; AccountContainer := 'storage container name'; Authorization := StorageServiceAuthorization.CreateSharedKey(AccountAccessKey); ABSBlobClient.Initialize(AccountName, AccountName, Authorization); ABSOperationResponse := ABSBlobClient.ListBlobs(ABSContainerContent); if ABSOperationResponse.IsSuccessful() then begin ABSContainerContent.Reset(); if ABSContainerContent.FindSet() then repeat Clear(instr); // Reading the file from output container ABSOperationResponse := ABSBlobClient.GetBlobAsStream(ABSContainerContent.Name, instr); Clear(ABSBlobClient_2); if ABSOperationResponse.IsSuccessful() then begin // Now Initilize Process container where I will put that file from source container ABSBlobClient_2.Initialize(AccountName, ProcessContainerName, Authorization); // Putting file here from source container. ABSOperationResponse := ABSBlobClient_2.PutBlobBlockBlobStream(ABSContainerContent.Name, instr); if ABSOperationResponse.IsSuccessful() then begin // Deleting the same file from source container. ABSOperationResponse := ABSBlobClient.DeleteBlob(ABSContainerContent.Name); if ABSOperationResponse.IsSuccessful() then Message('File has been deleted from the original container.'); end; end; until ABSContainerContent.Next() = 0; end; end; }
Let me know the feedback.
Thanks for visiting my blog.