dherault / dherault/serverless-offline
Problem with file upload, corrupted files
- Dominant language
- JavaScript
- Stars
- 5.3k
- Forks
- 811
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 3
Description
## Bug Report
**Current Behavior**
Using serverless-offline with Python and Flask with file upload corrupts the file.
Deployment to AWS Lambda resolves the issue. I had the same error on AWS prod env, but I have resolved it by setting `*/*` for `binaryMediaTypes`. It seems like the serverless-offline ignores `binaryMediaTypes` setting.
Upload of file via this CURL command fails with the following exception:
```bash
curl -X "POST" "http://localhost:3000" \
-H 'Content-Type: multipart/form-data' \
-F "file=@/Users/davidpivonka/Book1.xlsx"
```
This is an example of an uploaded file: [Book1.xlsx](https://github.com/dherault/serverless-offline/files/10371653/Book1.xlsx). Every file except plain .txt file is corrupted.
```
Traceback (most recent call last):
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/./app.py", line 10, in post_file_upload
excel = pandas.read_excel(file)
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/pandas/util/_decorators.py", line 211, in wrapper
return func(*args, **kwargs)
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/pandas/util/_decorators.py", line 331, in wrapper
return func(*args, **kwargs)
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 482, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 1652, in __init__
ext = inspect_excel_format(
File "/Users/davidpivonka/work/canada/http-test/aws-python-flask-api-project/venv/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 1543, in inspect_excel_format
with zipfile.ZipFile(stream) as zf:
File "/opt/homebrew/Cellar/python@3.8/3.8.16/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1269, in __init__
self._RealGetContents()
File "/opt/homebrew/Cellar/python@3.8/3.8.16/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1354, in _RealGetContents
fp.seek(self.start_dir, 0)
File "/opt/homebrew/Cellar/python@3.8/3.8.16/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tempfile.py", line 737, in seek
return self._file.seek(*args)
ValueError: negative seek value -151242
```
**Sample Code**
https://github.com/david-pivonka/aws-python-flask-api-project/blob/main/app.py#L10
**Expected behavior/code**
Correctly obtaining an uploaded file.
**Environment**
- `serverless` version: 3.26.0
- `serverless-offline` version: 12.0.4
- `node.js` version: 19.2.0
- `OS`: macOS 13.1 with MacBook Pro M1 chip
- `python` version: 3.8.16
Contributor guide
Assessment
This issue has not been assessed yet.