Contact Us 1-800-596-4880

Amazon S3 Connector 6.3 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

Studio 7 Visual Studio Icon Flow. The flow shows HTTP Listener, Create bucket, Get MuleSoft logo, Create logo object in S3 bucket, Get Image, Delete object and Delete S3 bucket.
  1. Create a new Mule project in Studio.

  2. Drag an HTTP > Listener into the canvas, and select it to open the properties editor.

  3. Add a new HTTP Listener Config global element:

    1. In General Settings, click the + button:

    2. 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

    3. Reference the HTTP Listener Configuration global element. In the General tab, specify the / Path.

  4. Drag an Amazon S3 connector into the flow, and double-click the connector to open its properties editor.

  5. If you don’t have an existing Amazon S3 connector global element to choose, click the + sign next to Extension Configuration field.

  6. Configure the global element properties, and then click OK.

  7. Configure the connector parameters:

    Connector Properties - General tab
    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.

  8. Drag the Create Bucket operation into the flow.

    S3 Connector Create Bucket in S3 General Tab
    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.

  9. Add an HTTP > Connector to request the MuleSoft logo from MuleSoft.

    HTTP > Connector - General Tab
    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 as HTTPS, and Port as 80.

    URL or Path

    /sites/all/themes/muletheme/images/mulesoft_dev_logo_v2.svg.

  10. Drag the Put Object operation into the flow to create the requested MuleSoft logo in the selected Amazon S3 bucket.

    S3 Connector Create Logo Object in S3 Bucket General Tab
    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.

  11. Drag the Get Object operation into the flow to get the newly created MuleSoft logo image object from the bucket.

    S3 Connector Get Image General Tab
    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.

  12. 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.

    S3 Connector Delete S3 Bucket General Tab
    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.

  13. 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.

    S3 Connector Delete S3 Bucket General Tab
    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>
View on GitHub