Article
· Aug 19 4m read

Accessing Azure Blob Storage

Accessing an Azure cloud storage to upload/download blobs is quite easy using the designated %Net.Cloud.Storage.Client class API methods, or using the EnsLib.CloudStorage.* inbound/outbound adaptors.

Note that you'll need to have the %JavaServer External Language Server up and running to use the cloud storage API or adaptors, since they both use the PEX framework using the Java Server.

Here is a quick summary:

Azure Blob Storage is accessed using a connection string that looks similar to this:

DefaultEndpointsProtocol=https;AccountName=abcdefscleanisr;AccountKey=n3mWskdjgfhklsghdfjaskhgkjdfizqMWJ5X2L4JpqeEJk/FuEnw0rPI6E/rULPn0V5BWgFw+AStRJZlYQ==;EndpointSuffix=core.windows.net

Break it into lines with ";" as the delimiter and save it as a text file:

DefaultEndpointsProtocol=https;
AccountName=abcdefscleanisr;
AccountKey=n3mWskdjgfhklsghdfjaskhgkjdfizqMWJ5X2L4JpqeEJk/FuEnw0rPI6E/rULPn0V5BWgFw+AStRJZlYQ==;
EndpointSuffix=core.windows.net

For this example I will name this file "MyAzureStorage.txt".

Now let's activate the API:

set file="C:\Storage\MyAzureStorage.txt"
set endpoint="abcdefscleanisr.privatelink.blob.core.windows.net"
set tStatus=""

In the CreateClient API method, the second parameter is the cloud provider:

  • 0 - Amazon S3
  • 1 - Azure Blob
  • 2 - Google Cloud Storage
SET myClient = ##CLASS(%Net.Cloud.Storage.Client).CreateClient(,1,file,0,.tStatus,endpoint)

Once the client has been established (tStatus=1), you can run any of the client's methods:

 

In Azure Blob Storage,  a Bucket is a Container.

So for example, to get the list of Azure containers, run the ListBuckets method:

set list=myClient.ListBuckets()

for i=1:1:list.Count() {write list.GetAt(i).name,!}

clean
dirty

Once you have a container name (let's take the "clean" container as an example), you can get the list of blobs in it:
 

set list=myClient.ListBlobs("clean")

for i=1:1:list.Count() {write list.GetAt(i).name,!}

4caa6f29-e9e6-4cde-9112-65ec28d4eded.jpeg
2374233-e9e6-4cde-9112-65ec28d4eded.jpeg
3klfd3lld-e9e6-4cde-9112-65ec28d4eded.jpeg
4caa6f29-e9e6-87ry-9112-65ec28d4eded.jpeg

The CloudStorage outbound adaptor has fewer options - its methods include only the following:

  • UploadBlobFromString(bucketName,blobName,content)
  • UploadBlobFromStream(bucketName,blobName,content)
  • UploadBlobFromFile(bucketName,blobName,filePath)
  • DeleteBlob(bucketName,blobName)

Here are the settings for configuring a business operation using the EnsLib.CloudStorage.OutboundAdapter with Azure Blob Storage. The Storage Region parameter is not relevant for Azure Blob Storage.

All other parameters are relevant the same as when we were using the CreateClient method:

The ContainerName parameter is not part of the adaptor's parameters - it is not needed to create the client (the CreateClient method does not use it).

However, all the other adaptor methods listed above do need to specify to which bucket/container should the blob be loaded, it makes sense to add it as a parameter as part of the settings:

Class Sasa.BO.Storage Extends Ens.BusinessOperation
{ 
Parameter ADAPTER = "EnsLib.CloudStorage.OutboundAdapter"; 
Property Adapter As EnsLib.CloudStorage.OutboundAdapter; 
Parameter INVOCATION = "Queue"; 
/// Bucket name(Amazon) = Container name(Azure)
Property ContainerName As %String(MAXLEN = 1000); 
Parameter SETTINGS = "ContainerName:Cloud Storage"; 
Method CreateFile(pRequest As Cloud.RES.REST, Output pResponse As Cloud.RES.REST) As %Status
{
    #dim tException As %Exception.SystemException
    Set tStatus = $$$OK
    set pResponse=##class(Cloud.RES.REST).%New() 
    Try {
        Set tStatus = ..Adapter.DeleteBlob(..ContainerName, pRequest.FileName)
        Set tStatus = ..Adapter.UploadBlobFromStream(..ContainerName, pRequest.FileName, pRequest.FileData)   
        if $$$ISERR(tStatus) {
            set pResponse.Success = 0
            set pResponse.ErrorMessage = $system.Status.GetErrorText(tStatus)
        }
    } 
    Catch tException {
        Set tStatus = tException.AsStatus()
        set pResponse.Success=0
        set pResponse.ErrorMessage=$system.Status.GetErrorText(tStatus)
    }
    Quit $$$OK
}

Hope that will help you start using the cloud adaptor with Azure.

Keren.

Discussion (0)0
Log in or sign up to continue