Friday, 24 July 2020

Connect to Azure File storage using script from SAP CPI

There is no standard way to connect to Azure file storage and does not have a adapter aswell in SAP CPI. 
Advantco Azure Adapter used to connect Azure storage but that is not added in CPI.

So would need to connect externally using script from SAP CPI.


Script:

import com.sap.gateway.ip.core.customdev.util.Message
import java.io.*
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.file.*;
import com.microsoft.azure.storage.common.*;

def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String

String accountName = "myaccountstorage"
String accountKey = "Key"
String storageConnectionString = "DefaultEndpointsProtocol=https;" + "AccountName=" + accountName+ ";" + "AccountKey=" + accountKey;


CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString)
CloudFileClient fileClient = storageAccount.createCloudFileClient();


CloudFileShare share = fileClient.getShareReference("fsmshare");


String fileName = "filename1.jpg"
String fileContent = body
byte[] fileBytes = fileContent.getBytes()


CloudFileDirectory rootDir = share.getRootDirectoryReference();

CloudFileDirectory sampleDir = rootDir.getDirectoryReference("attachments_inbound");



CloudFile cloudFile = sampleDir.getFileReference(fileName); cloudFile.uploadFromByteArray(fileBytes, 0, fileBytes.length)


cloudFile.getProperties().setContentType("image/jpeg")
cloudFile.uploadProperties()


message.setBody("OK")


return message;
}






1 comment: