Skip to Content
Technical Articles

How to Connect Azure using SCPI – Upload file to azure blob

Many of you aware that there is no standard adapter like SFTP, SOAP available to connect Azure Cloud with SCPI. So I am writing this post to share how we can get connected with Azure Cloud using SCPI

In this post you will learn how to get connected with Azure Cloud and uploading file to Azure

Azure provides SDK in Java to get connected. As SCPI supports groovy, we can write a simple groovy script to upload any file to azure blob container

For example, I have created a simple flow which uploads csv file to azure folder “trial”

And the script to upload the file is below

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

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

    String accountName = "your_azure_account_name"
    String accountKey = "your_azure_account_key"
    String storageConnectionString = "DefaultEndpointsProtocol=http;" + "AccountName=" + accountName+ ";" + "AccountKey=" + accountKey

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

    CloudBlobContainer container = serviceClient.getContainerReference("azurefoldername/containername")

    String fileName = "filename.csv"
    String fileContent = body
    byte[] fileBytes = fileContent.getBytes()

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

    blob.getProperties().setContentType("text/csv;charset=utf-8")
    blob.uploadProperties()

    message.setBody("OK")

    return message;
}

 

you have to replace the accountname, account key and container’s name with your azure’s credentials.

File name can be either static or dynamically populated.

In my groovy script, I have replaced all the credentials and file name as test.csv and azure container name as trial( which is already available in my azure cloud)

Now we have all set to upload a file to Azure cloud. Let’s test it via Postman tool

It is Status 200, which indicates success of uploading the file to Azure. Now let’s check the file in Azure using Azure Storage Explorer

 

Good Luck !

 

Questions/Comment and Suggestions are welcome ūüôā

24 Comments
You must be Logged on to comment or reply to a post.
  • My head is going to explode this morning.¬† I’ve read so many good technical blogs.¬† Is there a good business/technical reason for connecting to Azure.

    I know this is the how.¬† ¬†I always ask for more….¬† What was your business case?

  • /
  • Hi Ansari,

     

    I’ve been having some issues that you encountered previously looking at the SCN – with integrating CPI and Commerce Cloud;

     

    “com.sap.gateway.core.ip.component.odata.exception.OsciException: Bad Request : 400 : HTTP/1.1”

    And not having contact data /adr3mas in ERP for the standard B2B Customer replication iflow.

    Would you be able to share you insights on how you solved those problems?

     

    Kind Regards.

    • Hi Daniel,

      It is difficult to handle when you don’t have adr3mas. What I did was, I commented out adr3mas link in both receiver and sender side of the integration flow and created dummy adr3mas data in the property along with adrmas03 and injected the same with payload. To do that, you have to go though the entire flow and also should be careful with how data is stored in data store and pulled back. requires customization

      • Thanks I appreciate your reply!

        We ended up removing the branch for ADR3MAS, editing the xslt which checked to see if adrmas3, adr3mas and debmas had been received, and adding a conversion to remove the xml utf metadata at one stage where it was storing in the datastore for the missing adr3mas (as it is a reserved keyword in xml).

        We then encountered issues with the standard odata adapter for the B2B Unit as it added URL parameters which couldnt be changed, which was resolved by deleting it and using a new OData V2 adapter, loading a fresh instance of the EDMX, and using ATOM only – as the conversion to JSON added an additional comma which caused incorrect syntax.

        Bit if a nightmare but got a working solution in the end… Not quite the plug and play solution we were hoping for ūüėÄ

        Thanks for the tips and guidance. All the best

  • Hi, We have a similar requirement and working towards the same. We need to fetch the data from Azure . We are getting an exception while trying to read blob from Azure and the exception says cause: java.lang.NoClassDefFoundError: javax/crypto/Mac. I belive this is related the JCE policy and not sure if we have the appropriate jar files are present in the JVM. Please provide guidelines in this regards

  • Hello Ansari,

    We are getting the following exception after deployment of iflow.

    javax.script.ScriptException: java.lang.Exception: com.microsoft.azure.storage.StorageException: Host not found@ line 44 in script1.groovy, cause: com.microsoft.azure.storage.StorageException: Host not found

    Can you please guide on this.

    Regards,

    Irfan

    • Actually, it was easier then I thought. I’m processing CSV file from Azure File Storage placed in directory to CPI as text string. To make it even better without fixing a filename this method need to be used: CloudFileDirectory.ListFilesAndDirectories

      code snippet:

          //prepare file
          String fileName = "Test_response.txt" 
      
          CloudFile file = sampleDir.getFileReference(fileName)
          
          //get a file
          String fileContent = file.downloadText()
          file.delete()
          message.setBody(fileContent)
    • Hi, I’m trying the same , connecting from CPI to Azure,

      and getting error while calling

      CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString)

      Error :unable to resolve class CloudStorageAccount 

      do you have some idea to resolve this.

      thanks and regards,

      Anup

  • Hi Ansari,

    I found your blog very helpful!¬† I used it in conjunction with the SAP Commerce ‚Äėazurecloud‚Äô extension to write a PNG file to a cloud hot folder (CHF) from a PNG image URL in a digital asset manager (DAM).¬† I used a different API, but your sample Groovy script was a great starting point!

    Thanks,
    Greg Richardson