Download all Objects from an Amazon S3 Bucket using MuleSoft

Here’s a quick example on how you can download all the Objects from an Amazon S3 Bucket using the Amazon S3 Connector.  You can see the flow and the corresponding code (minus the HTTP and Amazon S3 Configuration) below.

Mule Flow Screenshot
Flow to download all files from an S3 Bucket
<flow name="s3-exampleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/bucket" doc:name="HTTP" />
<s3:list-objects config-ref="Amazon_S3__Configuration" bucketName="s3-mule-bucket" doc:name="Amazon S3 - List Objects in Bucket" maxKeys="5" />
<foreach collection="#[payload.objectSummaries]" doc:name="For Each">
<set-variable variableName="fileKey" value="#[payload.getKey()]" doc:name="Store Filename" />
<s3:get-object-content config-ref="Amazon_S3__Configuration" bucketName="#[payload.getBucketName()]" key="#[payload.getKey()]" doc:name="Amazon S3 - Get Object" />
<object-to-byte-array-transformer doc:name="Object to Byte Array" />
<file:outbound-endpoint path="/Users/dejimjuang/Downloads" responseTimeout="10000" doc:name="Write File to Path" outputPattern="#[flowVars.fileKey] "></file:outbound-endpoint>
</foreach>
<set-property propertyName="Content-Type" value="application/json" doc:name="Content-Type" />
<set-payload value="{'result': 'ok'}" doc:name="Set Payload" />
</flow>

Some prerequisites before you begin. You’ll need the following:

Let me highlight some of the key areas in the code.

  • When you run the project, you’ll kick off the process by navigating to http://localhost:8081/bucket. This is set in the HTTP Listener.
  • If you’ve pasted my code above into your project, you’ll need to configure the Amazon S3 configuration and the HTTP configuration
  • For the S3 Connector, you’ll need your Amazon AWS Access Key and Secret Key when you set up the configuration. Be sure to click on Test Connection before hitting OK.

    Amazon S3 Global Configuration
    Amazon S3 Global Configuration
  • The first Amazon S3 Connector grabs a list of all the Objects with in a Bucket. The field Bucket Name is hard-coded. You can change this to be dynamic and use a message.inboundProperties value instead.
  • You’ll need to open the properties for the File Connector and set the Path to a local directory before running the project.

With this example, you can see how easy it is to integrate with Amazon S3 without having to write any custom code. The connector provides a wrapper around the Amazon SDK to allow you the ability easily click-and-configure a project.


Comments

One response to “Download all Objects from an Amazon S3 Bucket using MuleSoft”

  1. […] For the most part, it should be straightforward to transfer files to and from AWS S3 buckets with Mule connector. S3 connector has been out there for a long time. If you just use customer key and secrete, you can see an example here http://www.dejim.com/blog/2016/03/10/amazon-s3-connector-download-bucket/ […]

Leave a Reply to Upload and Download Files From AWS S3 Bucket Using Mule Connector and Access Token – Perficient Blogs Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.