Awlexus
I’m trying to stream an xml file over ftp with Stream.resource/3 and SweetXml.stream_tags!/3, but I’m running into a strange issue where sweet_xml raises an error, before chunk arrives, unless I call Enum.to_list/1.
# Function definitions
def stream_file!(client) do
Stream.resource(fn -> start_file_stream(client) end, &next_file_chunk/1, &stop_file_stream/1)
end
defp start_file_stream(client) do
Logger.debug("Requesting to stream file")
with :ok <- :ftp.recv_chunk_start(client.conn, to_charlist(client.credentials.file_path)) do
client
end
end
defp next_file_chunk(%{conn: conn} = client) do
case :ftp.recv_chunk(conn) do
{:ok, binary} ->
Logger.debug("Stream file chunk #{byte_size(binary)} bytes)")
{[binary], client}
:ok ->
Logger.debug("Transfer completed successfully")
{:halt, client}
error ->
Logger.error(
credentials_id: client.credentials.id,
message: "Unable to stream file",
reason: error
)
{:halt, error}
end
end
defp next_file_chunk(error), do: {:halt, error}
defp stop_file_stream(_), do: []
# Code that is executed
# conn is the pid from :ftp.open/2
# Credentials holds infos about the file and the credentials for the ftp server
client = %{conn: conn, credentials: credentials}
client
|> stream_file!()
# For debugging
|> Stream.each(&IO.inspect(&1, label: :chunk))
# |> Enum.to_list()
|> SweetXml.stream_tags!(:Mitglied, discard: [:Mitglied])
|> Stream.map(fn {:Mitglied, elem} ->
# parse data here
end)
|> Stream.run()
Calling this code produces the following output. If I uncomment |> Enum.to_list() it will work fine and print all the chunks as they come. Any idea what might cause this?
[debug] Requesting to stream file
[error] 3917- fatal: :expected_element_start_tag
** (SweetXml.XmerlFatal) :expected_element_start_tag
(sweet_xml 0.7.3) lib/sweet_xml.ex:539: anonymous fn/1 in SweetXml.stream!/2
(elixir 1.14.4) lib/stream.ex:1619: Stream.do_resource/5
(elixir 1.14.4) lib/stream.ex:1813: Enumerable.Stream.do_each/4
(elixir 1.14.4) lib/stream.ex:689: Stream.run/1
For reference, I have a separate implementation for streaming files over sftp with sftp_client, which works perfectly. I’ve looked into their implementation, but couldn’t find any meaningful difference.
https://github.com/tlux/sftp_client/blob/v1.4.7/lib/sftp_client/stream.ex#L26-L51
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
dimitarvp
Does it work if you comment out the
Stream.eachpart?Awlexus
Unfortunatelly no, it doesn’t have an effect at all.
D4no0
Are you using a local ftp server, the ftp standard sometimes floats, especially when it comes to old servers.
Awlexus
It’s a remote server of a company we collaborate with, so I don’t know any further specifics about it. The same server has been used for a related project, that has been going since 2013. But still, I wonder how that could influence the stream in such a way
D4no0
It’s about quirks in protocol, you can query the server and see the software it runs on, at one of my jobs we had some nasty old ftp servers running on windows and erlang client had problems with them time to time.
dimitarvp
I am aware you likely don’t want to complicate your setup but if I was in your place I’d just periodically download those files via rclone and then process them locally with Elixir. (Or you can download them on-demand as well, Erlang/Elixir can interface with the rest of the CLI tools in the system via ports pretty well.)
Awlexus
I don’t think that’s necessary or worth it. The whole file can fit easily in memory and it’s probably never going to be too big for that. Being able to properly stream the file would have been a welcome optimization though.
dimitarvp
I get what you are saying but diving into a deep rabbit hole might burn you an order of magnitude more time. FTP is old, and not all servers strictly adhere to all parts of the spec. Chasing this down might cost you dearly.
Hence I’d eliminate any potential cause for things spiraling out of control from the get go.
Awlexus
Then I’ll just take this advice and just admit defeat.
Enum.to_listmay stay here for nowD4no0
The fun thing is that I had exact this issue at some point, and I can’t remember how I solved it, because it was working, maybe try to search about chunk size, however I might as well be mistaken this with sftp.