feat(upload): add support for streamed uploads of large files #1435
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Streamed uploads
The API also supports streamed uploads, in cases where the length of the file is not known before starting the upload.
This is useful for example when converting a video, to avoid buffering the entire converted video on the filesystem, each part is uploaded immediately as soon as it is produced by the encoder.
Everything works similarly to normal uploads, with a few key differences:
The client buffers part_size bytes (or less if the stream ends) before immediately uploading the part as described in the previous section.
A total_stream_size variable must be used to keep track of the total number of bytes read from the stream.
upload.saveBigFilePart must always be used, even if the stream turns out to be smaller than 10MB.
The file_total_parts field must be set to -1 for all parts except for the last one, using the following logic:
If the stream ends and the length of the buffered part is bigger than 0, upload it, setting file_total_parts=ceil(total_stream_size/part_size) (like for normal uploads)
If the stream ends and the length of the buffered part is equal to 0, upload it anyway (upload one last empty part), setting file_total_parts=ceil(total_stream_size/part_size) (like for normal uploads)
Note that streamed uploads cannot be used when uploading photos with inputMediaUploadedPhoto.