Skip to Content
Technical Articles

Connect to AzureBlob from CPI: Using Azure Storage jar

Introduction:

I came across a requirement to upload file to AzureBlob container from CPI. There are multiple ways to achieve this : using REST API or using standard jar provided by MS.

In this blog post I will discuss about uploading files to AzureBlob container using standard jar available in maven repository.

Steps to Configure:

I followed the below blog post and done some modification:

https://blogs.sap.com/2019/08/30/how-to-connect-azure-using-scpi-upload-file-to-azure-blob/

Step 1: I downloaded Azure Storage Client SDK from https://mvnrepository.com/artifact/com.microsoft.azure/azure-storage/

Step 2: I uploaded this .jar file as resource to my iFlow:

Add%20Jar%20to%20Resources

Step 3: I added properties using groovy or content modifier. I used content modifier and externalized so that I can configure as per my need.

Content%20Modifier

Step 4: I created a Groovy script to directly upload the file to the container

/*
Script to upload files to Azure Blob folder using parameters provided
 */
import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap
import com.microsoft.azure.storage.*
import com.microsoft.azure.storage.blob.*
import java.text.SimpleDateFormat

def Message processData(Message message) {
    def body = message.getBody(java.lang.String) as String
    map = message.getProperties()
    
    String accountName=map.get("AzureAccountName")
    String accountKey = map.get("AzureAccountKey")
    String containerRef =map.get("ContainerRef")
	String fileNameScheme = map.get("Filename")
	String timestamp = map.get("AddTimestamp")
    String storageConnectionString = "DefaultEndpointsProtocol=https;" + "AccountName=" + accountName+ ";" + "AccountKey=" + accountKey

    CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString)
    CloudBlobClient serviceClient = account.createCloudBlobClient()

    CloudBlobContainer container = serviceClient.getContainerReference(containerRef)
if(timestamp.equals("Y")){
    String pattern = "yyyyMMddHHmmss";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    String date = simpleDateFormat.format(new Date());
    fileNameScheme = fileNameScheme + "_" + date;
}
    String fileName = fileNameScheme+ ".csv"
    String fileContent = body
    byte[] fileBytes = fileContent.getBytes()

    CloudBlockBlob blob = container.getBlockBlobReference(fileName)
    blob.uploadFromByteArray(fileBytes, 0, fileBytes.length)

    blob.getProperties().setContentType("text/plain;charset=utf-8")
    blob.uploadProperties()
    message.setBody("File is created with name : "+fileName)

    return message;
}

When the CPI flow runs successfully, file is uploaded if the properties mentioned are correct. We don’t need any particular adapter for this iFlow as this script takes care of uploading the files.

Conclusion:

Using this concept you can upload a file to AzureBlob from CPI without any adapter, using jar provided in Maven repository.

In the next blog post I will describe how we can use REST API to achieve the same.

Cheers,
Suman

5 Comments
You must be Logged on to comment or reply to a post.
  • Can we pull all azure blob contents in a container using single rest URI call in SAP PO or CPI. Let's say I have four blobs with names abc,abcd,abcdef,def. I need to pull blob contents of abc,abcd,abcdef  based on some file name pattern in single REST URI call. Is that possible

    • Hi Siva,

      You can follow the below blog post which describes how to achieve this using SAP PO and do the necessary changes to adapt to CPI.

      https://blogs.sap.com/2021/03/22/connect-to-azureblob-from-pi-po-using-rest-api

      Regards,

      Suman