# Loop Over and Process Files

In this tutorial we will build an integration that downloads and processes files stored in [Google Cloud Storage](https://prismatic.io/docs/components/google-cloud-storage.md), but similar concepts can be applied to files stored in Dropbox, Amazon S3, Azure Blob Storage, and SFTP server, or any other file storage system.

For this integration, assume that some third-party service writes an XML file to a Google Cloud Platform (GCP) storage bucket whenever they process an order.

We'll configure our integration to run every five minutes, and our integration will do the following:

* Look for files in the `unprocessed/` directory of our GCP Storage bucket

* For each file that we find:

  <!-- -->

  * Download the file
  * Deserialize the XML contained in the file
  * Perform data mapping
  * Post the transformed data to an HTTP endpoint
  * Move the file from the `unprocessed/` directory to a `processed/` directory

Our integration will leverage the [loop](https://prismatic.io/docs/components/loop.md) component to process files one by one.

## List files[​](#list-files "Direct link to List files")

We'll start by adding a Google Cloud Storage **List Files** action to our integration. This will automatically create a Google Cloud Storage connection to our config wizard.

We'll want our customers to be able to specify their own bucket, so let's add a [Select Bucket](https://prismatic.io/docs/components/google-cloud-storage.md#selectbucket) data source config variable to our config wizard:

![Google Cloud Storage - Select Bucket data source](/docs/assets/images/select-bucket-data-source-f4cee6e6f5cdcc85754e7a9175b1c59c.png)

We can also add two more string config variables to represent the `unprocessed/` and `processed/` directories in the bucket (which our customer may want to change, so it's handy to make these config variables).

Now, configure the **List Files** action to reference our config variables:

![Google Cloud Storage - List Files inputs](/docs/assets/images/list-files-inputs-96aabc48772fca44b78869a7b660f285.png)

Next, we'll open the **Test Configuration** drawer and select **Test-instance configuration** to set some test credentials and config variable values.

![Google Cloud Storage - Test config variables](/docs/assets/images/test-config-variables-93b237b6a7971ded9bbd1c221734e852.png)

Finally, we'll click **Run**. If you see any errors about permissions, ensure that the Google IAM account you created has the proper permissions to the bucket you created. You should see the files in your `unprocessed/` directory:

![Google Cloud Storage - List Files result](/docs/assets/images/list-files-results-846d8fc00d4769280a9850a11abc4966.png)

## Create our loop[​](#create-our-loop "Direct link to Create our loop")

Next, we'll loop over the files that our **List Files** step found. We'll add a **Repeat for Each** step.

Under the **Items** input we will reference the list of files our previous step returned:

![Repeat for each in Prismatic integration designer](/docs/assets/images/loop-step-46c1b9fac52a2472ccd33a26651433fd.png)

## Add tasks to the loop[​](#add-tasks-to-the-loop "Direct link to Add tasks to the loop")

Our loop is now configured to run once for each file that was found in the `unprocessed/` directory in our GCP bucket. Our loop will contain several steps to process and send the data to an external system.

### Download the file we're currently looping over[​](#download-the-file-were-currently-looping-over "Direct link to Download the file we're currently looping over")

First, we'll download the file we're currently processing. The item that we're currently processing from our loop is accessible using the `currentItem` key of the loop.

We'll add a **Download File** action from the GCP component.

* For **File Name** we'll reference the loop's `currentItem`.
* For **Bucket Name** we'll reference the bucket name config variable we created.
* Our **Connection** is already set up for us. For example, if there's a file named `unprocessed/order-123.xml` in our bucket, `loopOverEachFile.currentItem` would be equal to `"unprocessed/order-123.xml"`:

![Download current file inputs](/docs/assets/images/download-file-inputs-98002ee805ba39f4841d66db42f1cc34.png)

Because we're downloading an XML file, this action will return parsed XML in a string format.

![Download current file results](/docs/assets/images/download-file-results-cb56f7fc06e53d757b8f7e62e9b9b627.png)

### Deserialize the XML[​](#deserialize-the-xml "Direct link to Deserialize the XML")

Next, we'll use the [Deserialize XML](https://prismatic.io/docs/components/change-data-format.md#deserializefromxml) action to convert the XML string into a JavaScript object whose keys can be referenced by subsequent steps.

![Download current file results](/docs/assets/images/deserialize-xml-results-5fde8efc857a8251676a2ef354a0d6a7.png)

### Map the data[​](#map-the-data "Direct link to Map the data")

Next, suppose the API we're sending the data to expects a different format ("quantity" instead of "qty", etc). We can use the [Collection Tools](https://prismatic.io/docs/components/collection-tools.md) **Create Object** action to create a new object for us, referencing the results of the **Deserialize XML** step:

![Create object inputs](/docs/assets/images/create-object-inputs-609ad8c14f880c9c8b78c7720b36c6fb.png)

### Send the data[​](#send-the-data "Direct link to Send the data")

Next, we'll use the [HTTP](https://prismatic.io/docs/components/http.md) component's **POST Request** action to send the data we generated. As a placeholder for an external API, we'll post the data to [Postman's](https://www.postman.com/) `https://postman-echo.com/post` endpoint. For our **Data** input, we'll reference the **Create Object**'s results:

![HTTP POST inputs](/docs/assets/images/http-post-inputs-83afb755799afeb3ed5fa882dad820ef.png)

### Move the file to a processed directory[​](#move-the-file-to-a-processed-directory "Direct link to Move the file to a processed directory")

Finally, we'll move the file that we downloaded out of the way by moving the file from `unprocessed/` to `processed/`.

First, we need to replace the word `unprocessed` with `processed`. We'll use the [Text Manipulation](https://prismatic.io/docs/components/text-manipulation.md) component's **Find & Replace** action for that, once again referencing the loop's `currentItem`:

![Find-and-replace inputs](/docs/assets/images/find-and-replace-inputs-db2d157db5054e2dca5b6049d3b088e7.png)

We'll add a Google Cloud Storage **Move File** action to move our file from one directory to another:

![Move File inputs](/docs/assets/images/move-file-inputs-844dfbb1b8bcac888d90ffe3b2373f51.png)

## Conclusion[​](#conclusion "Direct link to Conclusion")

That's it! At this point we have an integration that loops over files in a directory, processes them, and sends the data to an HTTP endpoint. This integration can be published, and [instances](https://prismatic.io/docs/instances.md) of this integration can be configured and deployed to customers.
