Read/Download Object From S3 Using S3 Connector In Mule 4

 

In this tutorial we will demonstrate how can we read/download object from S3 using S3 connector in mule 4, we will use the below operations to upload and download the file

 

Create object: This operation used to upload an object to S3 and it supports input streams,files,strings and byte array

Create object presigned uri: This operation is used to access the object from S3 bucket using pre signed URL, this is useful to share the URL with other users so that they can access the object without login in to AWS console, there is limitation that this URL will be valid for certain period of time.

 

Note: The maximum expiration time for presigned url is one week from time of creation. So there is no way to have a presigned url without expiry time. It depends on how you generate the S3 pre-signed URL.

 

Ref: https://aws.amazon.com/premiumsupport/knowledge-center/presigned-url-s3-bucket-expiration/

TestData

[{
"CUSTOMER_ID":1,
"CUSTOMER_NAME":"TEST1",
"ADDRESS1":"Flat 101",
"ADDRESS2":"Viman Nagar",
"POSTALCODE":"201018",
"CITY":"PUNE"
},{
"CUSTOMER_ID":2,
"CUSTOMER_NAME":"TEST2",
"ADDRESS1":"Flat 102",
"ADDRESS2":"Viman Nagar 1",
"POSTALCODE":"201019",
"CITY":"PUNE"
},{
"CUSTOMER_ID":3,
"CUSTOMER_NAME":"TEST3",
"ADDRESS1":"Flat 103",
"ADDRESS2":"Viman Nagar",
"POSTALCODE":"201018",
"CITY":"PUNE"
},{
"CUSTOMER_ID":4,
"CUSTOMER_NAME":"TEST4",
"ADDRESS1":"Flat 104",
"ADDRESS2":"Viman Nagar 4",
"POSTALCODE":"201019",
"CITY":"PUNE"
}]

 

First we will create the CSV file using create object operation and test data mentioned above and then read/download the csv file using create object presigned uri

Create project in anypoint studio and configure the listener to trigger the request

Read/Download Object From S3 Using S3 Connector In Mule 4

 

Convert the JSON object to CSV Format

Read/Download Object From S3 Using S3 Connector In Mule 4

 

Drag and drop the create object operation from mule palette and configure the Bucket name, File name and object to create the csv file

Read/Download Object From S3 Using S3 Connector In Mule 4

 

Set the variable as timestamp and set the data which is valid for next 7 days

Read/Download Object From S3 Using S3 Connector In Mule 4

 

Drag and drop create object presigned uri and configure the Bucket name, File name and Expiration time and Select method as GET

Read/Download Object From S3 Using S3 Connector In Mule 4

 

Create the transform message to send the S3 URL as Json

Read/Download Object From S3 Using S3 Connector In Mule 4

 

We can see S3 url created as below

{

“S3URL”: https://muleisybucket.s3.amazonaws.com/employee.csv?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20201107T083859Z&X-Amz-SignedHeaders=host&X-Amz-Expires=518399&X-Amz-Credential=AKIARCT2ITNBNJRETG6O%2F20201107%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=440a98c70a1ee49df2353cae56f0073f5299d4ab80b4a6aabb0e30c1c77ebef9

}

 

Open the S3 URL in browser and we can see CSV file downloaded successfully

 

Sample application: ReadFileFromS3 Sample project

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments