Amazon S3 Connector 6.2 Examples - Mule 4
The following example shows how to store an image from a URL in Amazon S3, and retrieve and display the image.
Before You Begin
For this example to work in Anypoint Studio, you must provide Amazon Web Services credentials. You can either:
-
Replace the variables with their values in the code.
-
Provide the values for each variable in the
src/main/resources/mule-artifact.properties
file.
Example Steps
-
Create a new Mule project in Studio.
-
Drag an HTTP > Listener into the canvas, and select it to open the properties editor.
-
Add a new HTTP Listener Config global element:
-
In General Settings, click the + button:
-
Configure the following HTTP parameters, retain the default values for the other fields, and click OK:
Field Value Name
HTTP_Listener_Configuration
Host
127.0.0.1
Port
8081
-
Reference the HTTP Listener Configuration global element. In the General tab, specify the
/
Path.
-
-
Drag an Amazon S3 connector into the flow, and double-click the connector to open its properties editor.
-
If you don’t have an existing Amazon S3 connector global element to choose, click the + sign next to Extension Configuration field.
-
Configure the global element properties, and then click OK.
-
Configure the connector parameters:
Field Value Name
Name for the connector instance, such as
Amazon_S3_Configuration
.General
General configuration for the connector.
Region Endpoint
Region, for example,
us-east-1
(default).Access key
Connection access key.
Secret key
Connection secret key.
-
Drag the Create Bucket operation into the flow.
Field Value Display Name
Display name of your choice, such as
Create new bucket
.Connector configuration
Global configuration for the connector.
Bucket name
Name of the newly created bucket.
ACL
PRIVATE (default) access control list. A canned ACL is a predefined grant.
-
Add an HTTP > Connector to request the MuleSoft logo from MuleSoft.
Field Value Display Name
Name for the connector instance. In this example, HTTP > Connector is named
Get MuleSoft logo
.Configuration
Configuration with the Host as
developer.mulesoft.com
, Protocol asHTTPS
, and Port as80
.URL or Path
/sites/all/themes/muletheme/images/mulesoft_dev_logo_v2.svg
. -
Drag the Put Object operation into the flow to create the requested MuleSoft logo in the selected Amazon S3 bucket.
Field Value Display Name
Display name of your choice, such as
Create logo object in S3 bucket
.Connector Configuration
Global configuration for the connector.
Bucket Name
Name of the bucket in which the new object is created.
Key
Name of the newly created object.
Content
Content of the object.
Object ACL
Access control list.
-
Drag the Get Object operation into the flow to get the newly created MuleSoft logo image object from the bucket.
Field Value Display Name
Display name of your choice, such as
Get Image
.Connector Configuration
Global configuration for the connector.
Bucket Name
Name of the bucket in which an object is stored.
Object Key
Name of the object to get.
-
Drag the Delete Object operation into the flow to delete the object. Because the Delete Object operation’s return type is void, the payload contains the object returned by the Get Object operation.
Field Value Display Name
Display name of your choice, such as
Delete created object
.Connector Configuration
Global configuration for the connector.
Bucket Name
Name of the bucket in which an object is stored.
Object Name
Name of the object to delete.
-
Drag the Delete Bucket operation into the flow to delete the bucket. Because the Delete Bucket operation’s return type is void, the payload contains the object returned by the Get Object operation.
Field Value Display Name
Display name of your choice, such as
Delete created bucket
.Connector Configuration
Global configuration for the connector.
Bucket Name
Name of the bucket to delete.
XML for the Example
Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule app:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:s3="http://www.mulesoft.org/schema/mule/s3"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/s3 http://www.mulesoft.org/schema/mule/s3/current/mule-s3.xsd">
<http:listener-config name="HTTP_Listener_config"
doc:name="HTTP Listener config" doc:id="DOC_ID">
<http:listener-connection host="0.0.0.0"
port="8081" />
</http:listener-config>
<configuration-properties doc:name="Configuration properties" doc:id="DOC_ID" file="mule-artifact.properties" />
<s3:config name="Amazon_S3_Configuration" doc:name="Amazon S3 Configuration" doc:id="DOC_ID" >
<s3:connection accessKey="${config.accessKey}" secretKey="${config.secretKey}" />
</s3:config>
<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" doc:id="DOC_ID">
<http:request-connection protocol="HTTPS" host="developer.mulesoft.com"/>
</http:request-config>
<flow name="docu-demoFlow" doc:id="DOC_ID" >
<http:listener doc:name="Listener" doc:id="DOC_ID" config-ref="HTTP_Listener_config" path="/"/>
<s3:create-bucket doc:name="Create new bucket" doc:id="DOC_ID" config-ref="Amazon_S3_Configuration" bucketName="${bucket.name}" acl="PRIVATE"/>
<http:request method="GET" doc:name="Get Mulesoft logo" doc:id="DOC_ID" path="/sites/all/themes/muletheme/images/mulesoft_dev_logo_v2.svg" config-ref="HTTP_Request_configuration"/>
<s3:put-object doc:name="Create logo object in S3 bucket" doc:id="DOC_ID" config-ref="Amazon_S3_Configuration" bucketName="${bucket.name}" key="${file.name}" objectACL="PRIVATE"/>
<s3:get-object doc:name="Get image" doc:id="DOC_ID" config-ref="Amazon_S3_Configuration" bucketName="${bucket.name}" key="${file.name}"/>
<s3:delete-object doc:name="Delete created object" doc:id="DOC_ID" config-ref="Amazon_S3_Configuration" bucketName="${bucket.name}" key="${file.name}"/>
<s3:delete-bucket doc:name="Delete created bucket" doc:id="DOC_ID" config-ref="Amazon_S3_Configuration" bucketName="${bucket.name}"/>
</flow>
</mule>