Get excel file from CloudHub Mulesoft

142 Views Asked by At

My issue is I already deploy a project in cloudhub. The flow will transform a payload into excel format and the excel file will be in the path I set.

I understand the path for local and cloudhub is different so I set to this path:${mule.home}/apps/${app.name}/FIN.xlsx . Is there any way for me to see the excel file in cloudhub? Thank you for your time. Edited: This is my example code:

    <?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
    xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd 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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd">
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="8629addc-423c-4e55-b620-1291af64f3a2" >
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    <file:config name="File_Config" doc:name="File Config" doc:id="fe9d640c-d102-49d3-8d20-f459eee91837" />
    <flow name="myinsightsFlow" doc:id="85b09215-b002-4db7-bb76-ef70d131bcb6" >
        <http:listener doc:name="Listener" doc:id="e59ec33b-1df0-44b3-983e-f61e8ec09dbc" config-ref="HTTP_Listener_config" path="/myInsight"/>
        <set-variable value="#[payload]" doc:name="Set Variable" doc:id="0258fa4a-3f6e-4f20-8fca-9fca5a093dcc" variableName="firstPayload"/>
        <logger level="INFO" doc:name="Payload From Listener" doc:id="d335d098-35f5-46b0-94e1-c9fa6427451f" message="#[vars.firstPayload]"/>
        <ee:transform doc:name="Transform Message" doc:id="378829cf-8268-462d-9946-a24efa7b6955" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json

// Calculate the count of queries
var queryCount = sizeOf(vars.firstPayload)

---
{
  "queryCount": queryCount
}
]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="QueryCount" doc:id="19e5d841-e814-40bf-aa28-155564e7a64a" message="#[payload]"/>
        <ee:transform doc:name="Transform Message" doc:id="e88474af-ff3a-4cc9-a05c-953ab3401380" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/xlsx header=true

// Calculate the count of queries


---
{
    "Sheet1":vars.firstPayload
    
}

]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <file:write doc:name="Write" doc:id="ed392aa2-ecb8-41e0-9cf8-6fb6c89ea228" config-ref="File_Config" path="${mule.home}/apps/${app.name}/FIN.xlsx"/>
        
    </flow>
    
    
</mule>
3

There are 3 best solutions below

2
vanessen On BEST ANSWER

I had kind of a requirement similar to you, where in my flow I generate a csv file which is read and written by dedicated flows in the application. At the end of the process, I attach the csv to an email that I send (using smtp email send with attachment). Thus this is the proof that my file is well generated or not, and if in the correct format.

As requested,here is the code :

<flow name="send-error-email-flow" doc:description="Use this flow to send the error email.&#10;As input need to feed it the email content and body as payload" doc:id="5d0253f1-4515-4c2f-8162-2000ede33356">
    <logger level="INFO" doc:name="flow start" doc:id="8af0ad51-7773-4ac8-9f3f-f6021ab9b515" message="## START flow #[flow.name] ##"/>      
    <validation:is-true doc:name="subject variable set in payload" doc:id="b8dd5ef9-bfbf-4a67-a472-760ffb861821" config-ref="Validation_Config" expression="#[payload.subject?]" message="## payload is not as expected. Payload missing subject attribute."/>
    <validation:is-true doc:name="Is true" doc:id="028b6bd4-4dee-45e7-afe5-5be8aed49836" config-ref="Validation_Config" expression="#[payload.body?]" message="## payload is not as expected. Payload missing body attribute."/>
    <set-variable value="#[payload.subject]" doc:name="Set email subject" doc:id="500d3fb7-cc05-4a8e-9e05-fe2f6078f9cf" variableName="subject" />
    <set-variable value="#[payload.body]" doc:name="Set email body" doc:id="6b3d8e53-52bc-46f1-9f4b-6b2d80499871" variableName="body" />
    <file:read doc:name="Read" doc:id="41031a88-dc44-42b2-aef1-654a1a96163f" config-ref="File_Config" path="error.csv" />
                <email:send doc:name="Send" doc:id="c758dbf1-1e34-4f09-92d9-b61e679e4d09" config-ref="Email_SMTP" subject="#[vars.subject]">
            <email:to-addresses>
                <email:to-address value="[email protected]" />
            </email:to-addresses>
            <email:body contentType="text/plain">
                <email:content><![CDATA[#[vars.body]]]></email:content>
            </email:body>
            <email:attachments><![CDATA[#[{
'error.csv' : payload
}]]]></email:attachments>
</email:send>
    <logger level="INFO" doc:name="flow end" doc:id="4b9bc67c-c6a5-45ac-98d8-af64318f6c97" message="## END flow #[flow.name] ##"/>
0
aled On

Writing files inside a CloudHub application is a bad practice. CloudHub Workers that execute Mule applications are temporary so files won't survive a restart. Also there are size limitations of storage in workers. Under load or big files it may be easy to use all available space and cause issues. In a REST API or HTTP server implementation clients are not expected to read an output file. Rather the expectation is to receive the file in the HTTP response of the request. In this case removing the <file:write> operation at the end of the flow will cause the payload that already contains the Excel document to be sent as the body of the HTTP response. Another option is to write it as an attachment using the Multipart format for the response, instead of the full body of the response.

0
TwinZaia On

I find the solution, instead of saving in the local device, it will send the file to an email that I setup. That way it will be better since each request will produuce different file