Skip to content

uploads.upload_file failing and ending in 'Stream ended unexpectedly' #595

@dangelo96

Description

@dangelo96

Description of the Issue

I have an application running as a service on Windows that I've coded some months ago.
The application is responsible to understand changes in a folder and send them to BOX, using the uploads methods (and specifically DSE files, sent from client.uploads.upload_file function).
I have different sizes of files in this application, in a range from 300kB to 3.5MB. Obviously I'm using streams to upload the file to BOX. In addition, I've developed a decorator to retry the upload (if the upload fail, my application tries more 4 times, waiting some ms before try again).
The problem: sometimes I'm getting the 400 HTTP code, and the reason is "Stream ended unexpectedly". And I'm getting this error only when working with files up to 2MB. The application starts to send the file and actually send something, but every time this error happens, the file size is smaller than the original file. So I think that is happening something that blocks the upload while the same is in process to complete.
In addition, sometimes I have to "force" the upload and, for some files, I have to try +8 times to upload the same file until I actually make it work.
This is happening only for files with more than 2MB. I have researched the documentation and it seems like I could have a problem with +50MB files, but is not the case.
I'll share the code as well the log that is recording this error.

Steps to Reproduce

The code that is uploading files:

with open(file_, "rb") as stream:
    date_format = "%Y-%m-%dT%H:%M:%S+00:00"
    uploaded_files = client.uploads.upload_file(
        attributes={
            "name": filepath.name,
            "parent": {"id": folder},
            "content_created_at": f"{datetime.fromtimestamp(filepath.stat().st_ctime):{date_format}}",
            "content_modified_at": f"{datetime.fromtimestamp(filepath.stat().st_mtime):{date_format}}",
        },
        file=stream,
    )

Expected Behavior

Files that the application is identifying is being sent successfully to BOX, with no differences between them (size and information itself).

Error Message, Including Stack Trace

Traceback (most recent call last):
  File "yokogawa\retry.py", line 26, in wrapper_func
    return func(*args, **kwargs)
  File "yokogawa\box_operations.py", line 57, in upload_file
    uploaded_files = client.uploads.upload_file(
  File "box_sdk_gen\managers\uploads.py", line 346, in upload_file
  File "box_sdk_gen\networking\fetch.py", line 151, in fetch
  File "box_sdk_gen\networking\fetch.py", line 288, in __raise_on_unsuccessful_request
box_sdk_gen.box.errors.BoxAPIError: 	
Timestamp: 2025-05-22 10:29:31.345506
Underlying error: None
Message: 400 Stream ended unexpectedly; Request ID: 
Request: 
	Method: POST
	URL: https://upload.box.com/api/2.0/files/content
	Query params: 
{}
	Headers: 
{       'Authorization': 'Bearer Token,
        'Content-Type': 'multipart/form-data; '
                        'boundary=<boundary>',
        'User-Agent': 'box-python-generated-sdk-1.4.0',
        'X-Box-UA': 'agent=box-python-generated-sdk/1.4.0; env=python/3.9.7'}
	Body: 
<MultipartEncoder: OrderedDict([('attributes', '{"name": "<file_sample>.DSE", "parent": {"id": "<folder_id>"}, "content_created_at": "2025-05-22T10:29:19+00:00", "content_modified_at": "2025-05-22T10:29:30+00:00"}'), ('file', ('', <_io.BufferedReader name='<full path of file>'>, None))])>
Response: 
	Status code: 400
	Headers: 
{       'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
        'Content-Length': '140',
        'content-type': 'application/json;charset=utf-8',
        'date': 'Thu, 22 May 2025 13:29:31 GMT',
        'server': 'nginx',
        'strict-transport-security': 'max-age=31536000',
        'via': '1.1 google',
        'x-envoy-upstream-service-time': '14'}
	Code: bad_request
	Context Info: 
{}
	Request Id: None
	Help Url: http://developers.box.com/docs/#errors
	Body: 
{       'code': 'bad_request',
        'help_url': 'http://developers.box.com/docs/#errors',
        'message': 'Stream ended unexpectedly',
        'status': 400,
        'type': 'error'}
	Raw body: {"code":"bad_request","help_url":"http://developers.box.com/docs/#errors","status":400,"message":"Stream ended unexpectedly","type":"error"}

Screenshots

NA

Versions Used

Python SDK: box-sdk-gen[jwt]==1.4.0
Python: 3.9.7

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions