Gihan Anuruddha
Gihan Anuruddha

Reputation: 339

Mule3 Upload attachment to a SFTP server

I want to upload attachments as it is to an SFTP server that got from HTTP inbound. These files can be any type like XML, JSON, txt etc.

I tried these sample codes but the issue is upload files are not in the format, type I sent. It always stored in the FTP server like 1f144250-7b46-11ea-a605-38f9d3744a4d.dat.

    <flow name="FtpUp">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/attach1" doc:name="Copy_of_HTTP"/>
        <logger message="#[message.inboundAttachments.size()]" level="INFO" doc:name="Copy_of_Logger"/>
        <foreach collection="#[message.inboundAttachments]" doc:name="Copy_of_For Each">
            <set-payload value="#[payload.dataSource.content]" doc:name="Set Payload"/>
            <byte-array-to-string-transformer doc:name="Byte Array to String"/>
            <set-attachment attachmentName="test.txt" value="#[payload]" contentType="text/plain" doc:name="Attachment"/>
            <sftp:outbound-endpoint exchange-pattern="request-response" host="" port="22" path="" user="" password="" responseTimeout="10000" doc:name="SFTP"/>
        </foreach>
    </flow>
    <flow name="Copy_of_FtpUp">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/attach2" doc:name="Copy_of_Copy_of_HTTP"/>
        <foreach collection="#[message.inboundAttachments]" doc:name="Copy_of_Copy_of_For Each">
            <set-payload value="#[payload.dataSource.content]" doc:name="Set Payload"/>
            <sftp:outbound-endpoint exchange-pattern="request-response" host="" port="22" path="" user="" password="" responseTimeout="10000" doc:name="Copy_of_SFTP" disableTransportTransformer="true"/>
        </foreach>
    </flow>

enter image description here

Can someone please help me to figure out the missing part? I just want to upload the file as it is that I'm getting to HTTP inbound. I'm using Mulesoft 3 (3.9.4 EE).

Upvotes: 0

Views: 332

Answers (2)

Gihan Anuruddha
Gihan Anuruddha

Reputation: 339

Here is a working code snippet after following the @aled suggestion.

<flow name="FtpUp">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/attach2" doc:name="HTTP"/>
    <foreach collection="#[message.inboundAttachments]" doc:name="Iterate attachments ">
        <set-variable variableName="fileName" value="#[payload.dataSource.part.fileName]" doc:name="Set File Name"/>
        <set-payload value="#[payload.dataSource.content]" doc:name="Set Payload"/>
        <sftp:outbound-endpoint exchange-pattern="request-response" host="" port="22" path="" user="" password="" responseTimeout="10000" doc:name="SFTP Server" disableTransportTransformer="true" outputPattern="#[flowVars.fileName]"/>
    </foreach>
</flow>

Upvotes: 0

aled
aled

Reputation: 25802

The issue is probably that the SFTP outbound endpoint is not setting an outputPattern attribute to define the name of the output file. The default value is the message id, which explains the names you are getting.

outputPattern

The pattern to use when writing a file to disk. This can use the patterns supported by the filename-parser configured for this connector. By default the File Transport Reference is used. See this same document section for information on how to override the default parser.

Type: String

Default: The message ID, for example, ee241e68-c619-11de-986b-adeb3d6db038

Also, the flow is sending the files as attachments. The SFTP connector expects the contents of the file to be transferred to be in the payload.

Upvotes: 1

Related Questions