Contact Us 1-800-596-4880

Amazon S3 Connector 5.7 Examples

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 Listener, Create bucket, Get MuleSoft logo, Create logo object in S3 bucket, Get Image, 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

    Display Name

    Name for the connector instance, such as Create bucket.

    Extension Configuration

    Global configuration for the connector.

    Bucket Name

    ${config.bucket}

    Region

    Region, for example US_STANDARD (default).

    Canned ACL

    PRIVATE (default) access control list. A canned ACL is a predefined grant.

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

    Body

    #[payload]

    Target Value

    #[payload]

  9. Drag another Amazon S3 connector 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

    Name of your choice. In this example, the S3 connector is named Create logo object in S3 bucket.

    Extension Configuration

    Global configuration that you created.

    Bucket Name

    ${config.bucket}

    Key

    muledevlogo

    Content

    #[payload]

    Canned ACL

    PRIVATE (default)

    Storage Class

    STANDARD (default)

  10. Add another Amazon S3 connector to get the newly created MuleSoft logo image object from the bucket:

    S3 Connector Get Image General Tab
    Field Value

    Display Name

    Name for the connector instance. In this example the S3 connector is called Get Image.

    Extension Configuration

    Global configuration you create.

    Bucket Name

    ${config.bucket}

    Key

    muledevlogo

  11. Add another Amazon S3 connector to delete the bucket. Because the delete bucket operation’s return type is void, the payload contains the object returned by the get image operation.

    S3 Connector Delete S3 Bucket General Tab
    Field Value

    Bucket Name

    ${config.bucket}

    Force

    True

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">
  <configuration-properties file="mule-artifact.properties"/>
  <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="DOC_ID">
    <http:listener-connection host="127.0.0.1" port="8081"/>
  </http:listener-config>
  <s3:config name="Amazon_S3_Configuration" doc:name="Amazon S3 Configuration" doc:id="DOC_ID">
    <s3:basic-connection accessKey="${config.accessKey}" secretKey="${config.secretKey"/>
  </s3:config>
  <http:request-config name="HTTPS_Request_Configuration" doc:name="HTTP Request configuration" doc:id="DOC_ID">
    <http:request-connection protocol="HTTPS" host="www.mulesoft.com"/>
  </http:request-config>
  <flow name="s3docuFlow" doc:id="DOC_ID">
    <http:listener config-ref="HTTP_Listener_config" path="/" doc:name="Listener" doc:id="DOC_ID"/>
    <s3:create-bucket config-ref="Amazon_S3_Configuration" bucketName="${config.bucket}"
    doc:name="Create bucket" doc:id="DOC_ID"/>
    <http:request method="GET" path="/sites/default/files/new-application_network_diagram-01.svg"
    doc:name="Request" doc:id="DOC_ID" config-ref="HTTPS_Request_Configuration"/>
    <s3:create-object config-ref="Amazon_S3_Configuration" bucketName="${config.bucket}" key="muledevlogo"
    doc:name="Create object" doc:id="DOC_ID"/>
    <s3:get-object config-ref="Amazon_S3_Configuration" bucketName="${config.bucket}" key="muledevlogo"
    doc:name="Get object" doc:id="DOC_ID"/>
    <s3:delete-bucket config-ref="Amazon_S3_Configuration" bucketName="${config.bucket}"
    doc:name="Delete bucket" doc:id="DOC_ID" force="true"/>
  </flow>
</mule>
View on GitHub