Integration & IT Modernization

Upload and Download Files From AWS S3 Bucket Using Mule Connector and Access Token

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/

The complication comes when you also want to use an access token in addition to the customer key and secrete. In order to that, you need a newer version of S3 connector “4.2.1.201704271749”. Once you update your connector, you will see the additional “Session Token” field. Put in your token here, you should be all good.

As you can see, the new connector has a release date stamp “2017-04-27”.  Here is a caveat, if you download the current (March 2018) Anypoint Studio Release 6.4.3, the S3 connector comes with the release is still old. However, when you try to update the connector, it says everything is up to date. You are stuck in between the worlds 🙁 go figure…

Before MuleSoft fixes the issue, here is a work around. Go find an older version of the studio and do an update, you most likely will see the newer S3 connector. I’m not going to dish out any advice what’s your best option from here. You can either develop your special project with this old version of Studio temporarily, or you can go figure out how to get the connector files from your older studio directory. Mine sits here .\my.studio.path\plugins\org.mule.tooling.ui.contribution.s3.3.5.0_4.2.1.201704271749.  Push to shove; you can skip S3 connector entirely. You can just use HTTP requester, follow the AWS SDK, do everything “manually”. I would strongly advise not to take this route, unless you have a compelling reason to do so.

Anyway, for the curious mind, here is the code snippet to upload and download a file to S3 bucket:

<s3:config name=“Amazon_S3__Configuration” accessKey=“#[flowVars.AccessKey]” secretKey=“#[flowVars.SecretKey]” sessionToken=“#[flowVars.Token]”     doc:name=“Amazon S3: Configuration” />

<http:request-config name=“HTTP_Request_Configuration”  protocol=“HTTPS” host=“xxx.com” port=“443”   doc:name=“HTTP Request Configuration” responseTimeout=“30000”>

<http:basic-authentication username=“xxx”   password=”xxx” preemptive=“true” />

<tcp:client-socket-properties connectionTimeout=“30000” />

</http:request-config>

<flow name=“s3-tokenFlow”>

<http:listener config-ref=“HTTP_Listener_Configuration”      path=“/s3” doc:name=“HTTP” />

Covid 19
COVID-19: Digital Insights For Enterprise Action

Access Perficient’s latest insights into how you can leverage digital technologies to not only respond to the pandemic, but drive your operations forward and deliver experiences your customers need.

Get Informed

<!—grab your token, whatever your setup is …–>

<http:request config-ref=“HTTP_Request_Configuration”       path=“/SO/api/S3Token” method=“GET” doc:name=“HTTPs” />

<json:json-to-object-transformer      doc:name=“JSON to Object” returnClass=java.util.HashMap />

<set-variable variableName=“Bucket” value=“#[payload.get(‘Bucket’)]”       doc:name=“Bucket” />

<set-variable variableName=“AccessKey”   value=“#[payload.get(‘AccessKeyId’)]”  doc:name=“AccessKey” />

<set-variable variableName=“SecretKey”  value=“#[payload.get(‘SecretAccessKey’)]”  doc:name=“SecretKey” />

<set-variable variableName=“Token” value=“#[payload.get(‘Token’)]”    doc:name=“Token” />

<set-payload value=“This is a test fie for the bucket” encoding=“US-ASCII”     mimeType=“text/plain” doc:name=“Set Payload” />

<logger level=“INFO” doc:name=“Logger”

message=“bucket=#[flowVars.Bucket], client=#[flowVars.AccessKey], sec=#[flowVars.SecretKey], token=#[Token], pay=#[payload]” />

<s3:create-object config-ref=“Amazon_S3__Configuration”

bucketName=“#[flowVars[‘Bucket’]]” key=“yourBucketKey/test.txt” acl=“PUBLIC_READ”     doc:name=“Amazon S3” />

<json:object-to-json-transformer    doc:name=“Object to JSON” />

<logger message=“resp=#[message.payloadAs(java.lang.String)]”       level=“INFO” doc:name=“Logger” />

<s3:get-object-content config-ref=“Amazon_S3__Configuration”

bucketName=“#[flowVars[‘Bucket’]]” key=“yourBucketKey/test.txt” doc:name=“Copy_of_Amazon S3” />

<object-to-byte-array-transformer       doc:name=“Object to Byte Array” />

<file:outbound-endpoint path=“c:\temp\blah.txt”       responseTimeout=“10000” doc:name=“File” />

<logger message=“resp=#[message.payloadAs(java.lang.String)]”       level=“INFO” doc:name=“Copy_of_Logger” />

 

</flow>

About the Author

More from this Author

Leave a Reply

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

Subscribe to the Weekly Blog Digest:

Sign Up