Download File in Chunks
Is there a way to download a file in chunks (with a specified bufferSize), but not download them to local disk, but only read that chunk in memory, and then upload it to S3 storage using multiPartUpload?
12 Replies
FIle is to be downloaded from a given URL
if you use streams you can read as much or as little of it as you want at a time
I tried using Streams, but I get such an error:
The response ended prematurely, with at least 9823402 additional bytes expected.
need to see code
It was something like this:
I am trying different methods, so the current state of my code has changed...
is there any article or stack overflow thread where I can rely?
or could you send me some example? @Jimmacle
Don't use async methods on MemoryStream for one thing
"the response ended prematurely" sounds like the other side of the connection closed for some reason
this particular method will dispose the response stream after reading a single chunk, are you sure you want that?
it does not happen when try to read the whole file 🤷♂️
What I want is to implement something that download a file part by part, and upload them to storage part by part...
Download Part 1, Upload Part 1
Continue Downloading the second part, Upload Part 2
...
How many parts are decided base on the total fileSize / some chunkSize (in bytes)
my point is i'm not sure it's valid to keep calling
.ReadAsStreamAsync()
and disposing the stream on the same HttpResponseMessage
you'd want to open the stream once and do the chunking within thereI can download it chunk by chunk and do a multi part upload, within a ReadAsStream...?
It feels like you don't want to be creating and disposing the stream inside this method, but rather pass in the stream on each call.
The stream has an internal pointer that advances as you read it so you don't need to seek or anything like that.
Although even having a separate method here is maybe overkill.
I will take a shot at this approach, thank you :)