Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
suman_saha
Contributor

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:


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.



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.

Check this blogpost to get and process a file from azure blob container using CPI.

Cheers,
Suman
19 Comments
Labels in this area