MuleSoft Integration With Box Cloud Storage

Author: Shyam Kulkarni

What is Box Cloud Storage?
  • Box.com is a secure, easy-to-use content cloud storage platform built for the entire content lifecycle from file creation and sharing to co-editing, signature, classification, and retention. 
  • Box stands out for its simplicity compared to other cloud storage platforms like Amazon Web Services, Google Drive, Microsoft’s Onedrive, iCloud, and Dropbox.
Use Case:

The use case goes as follows:

  1. A CSV file would be placed inside a folder in the Box.
  2. As the file is placed, MuleSoft Integration picks up the file and decides if the file placed is eligible for processing.
  3. If eligible, then download the file from the Box and perform integration with other systems, if use case demands (e.g can be upserting data to SalesForce).
  4. After completing the above process, next thing is to remove that file from the folder from where we picked it and upload it to the Archive folder. This implies that the file has been processed.

So, basically, if we look at this use case specifically from what we are really doing with MuleSoft and Box, here we are dealing with the following operations:

  1. Downloading/Fetching a file from Box.
  2. Uploading a file inside a folder present in the Box.
  3. Deleting a file from a folder present in the box.

For this we are going to use MuleSoft provided out-of-box “Box Connector” and JWT configuration would be used.

So, I have divided this blog into two parts:

  1. Configuring Box
  2. Mule Box Integration
Configuring Box:

Following are the steps to be followed for configuring Box:

  1. Create a trial developer account by going to this link: https://account.box.com/login
  2. In the “Dev Console”, Click on “Create New App”, then select “Custom App”.

  1. Select the Authentication Method as “Server Authentication (With JWT)” and fill in the custom app name.
  1. Now, the Custom App is Created and the following page appears. Go to the “Configuration” tab and do these things:
    1. Copy the “OAuth 2.0 Credentials”. i.e Client ID and Client Secret (later used in a configuration).
 
  1. Select “App + Enterprise Access” in the App Access Level section.
  1. Provide the required scopes as shown in image i.e. (Read, Write, Manage Users, Manage Groups, Manage Enterprise Properties) in the “Application Scopes” section.
  1. Now, click on “Add a Public Key” in the “Add and Manage Public Keys” section and this can be added only if Multi-Factor Authentication is configured in Box. 
  1. Now, what is to be added to the “Public Key” textbox? So, here we will be adding the public cert info. So, let us create a Keystore using the following command. Go to CMD -> Copy and Execute this command.
keytool -genkeypair -keystore mule-box-keystore.jks -dname “CN=localhost, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown” -keypass pass1234 -storepass pass1234 -keyalg RSA -sigalg SHA1withRSA -keysize 2048 -alias mule-box -ext SAN=DNS:localhost,IP:127.0.0.1 -validity 9999
  1. Once the Keystore is created, we can extract the public certificate from it using the following command:
keytool -exportcert -alias mule-box -keystore mule-box-keystore.jks -rfc -file public.cert
  1. Now, open the “public.cert” file using notepad and copy all the content from it and paste it into the textbox in Box’s configuration and verify and save it.

Save the information that we have configured for the “Configuration” tab.

  1. Now, head on to “Authorization” tab and click on “Review and Submit”. After entering the “App Description”, click on submit and you will receive a email.
  1. Click on “Review App Details” and “Authorize” it. 

Here, we can see “Authorization Status” and “Enablement Status” are set to “Authorized” and “Enabled” respectively.

  1. Now, if we go to the “General Settings” tab, we can see a “Service Account ID” is created – it kind of acts as an Integration User.
  1. Now, go to https://app.box.com/folder/0 and create a folder in the “Invite Additional People”, add the “Service Account ID‘, and give editor access.
  1. Also, create a archive folder inside the folder created earlier.

So, we have pretty much covered all the things in the “configuring box”, except adding the CSV file to it. We will do it when we will run our Mule application for demonstration.

Mule Box Integration:

Steps:

  1. Let’s create a Mule application. Start Anypoint Studio, create a new Mule Project, and add a “Box Connector” to your Mule Palette by searching for it on exchange and downloading it to your project.
  1. Add “mule-box-keystore.jks” file created earlier into src/main/resources folder.
  1. Now, let’s do the box-connector configuration with JWT:

You can configure the Client ID and Client Secret which we copied earlier.

Public Key ID: You get in the “Configuration” tab in box where we added public certificate info.

Entity ID: If you select “Box sub type” as “ENTERPRISE” then you can get the enterprise ID from the “General Settings” tab of Box.

Click on Test Connection and it should be working/successful.

  1. Since I have shared the JAR file of the Mule application here, I will just discuss the box connector operations being used and their configs.
  2. In the new file in the folder:
    1. This connector is placed in the source section, which gets triggered whenever a new file is placed in the folder present in the box.
  1. Here we have to configure the “Folder ID”, which is nothing but a way to know which folder is to be used for looking for new files being placed.

**Easy way to find the folder ID is to look at the URL in the top. It is the folder ID.

  1. It sends the following as the response whenever it picks up the new file uploaded to the box.
{ “created_at”: “2022-12-02T03:28:42-08:00”, “modified_at”: “2022-12-02T03:28:42-08:00”, “id”: “1081036634933”, “name”: “north_america_customers_file.csv”, “event_type”: “ITEM_UPLOAD”}
– -“Id” here, refers to file ID of the “north_america_customers_file.csv” file, which is used in download file operation.
  1. Download File:
    1. This connector is used to fetch/download file from Box.
  1. It requires sending File ID i.e. which file is to be fetched.
  1. Upload File:
    1. This connector is used to upload the file to the Box. Here, in our use case, we are going to upload it to the Archive folder.
  1. In Attributes – Body, we need to pass a JSON object specifying the file name and folder ID where we need to upload a file.
e.g 
{ “name”: “north_america_customers_file.csv”, “parent”: { “id”: “184555932384” }}
  1. File – Body is the raw data that needs to be passed inside the file.
  1. Delete File:
    1. This connector is used to Delete files from the Box’s folder.
  1. It expects a file ID to be passed i.e which file is to be deleted.

Note: If the file is uploaded successfully, it returns “201” as the status code whereas if the deletion of the file is successful, then it returns “204” as the status code.

The below is just to demonstrate this POC and how it works.

  • So, after running the Mule application, we place the file in the folder in Box.
  • MuleSoft picks it up and performs the download, uploads the file to the archive folder, and deletes it from the location from where it was picked up.
  • On refreshing the page, we can observe that file is deleted and uploaded to the archive folder.

Archive Folder:

Mule Logs:

Mule Application Flow:

ACCESS JAR FILE HERE 

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.