tus / tus-py-client

A Python client for the tus resumable upload protocol
https://tus.io/
MIT License
166 stars 45 forks source link

Use Python to upload without saving locally? #94

Open hiven opened 5 months ago

hiven commented 5 months ago

Do I really have to save my file locally to use TUS?

The code:

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        return jsonify({'error': 'No file part'})

    file = request.files['file']
    if file.filename == '':
        return jsonify({'error': 'No selected file'})

    tus_client = client.TusClient('http://tusd.tusdemo.net/files/')
    uploader = tus_client.uploader(file.stream, chunk_size=200)

    try:
        # Upload the entire file
        upload_response = uploader.upload()
        file_url = upload_response.headers['Location']
        return jsonify({'file_url': file_url})
    except Exception as e:
        return jsonify({'error': str(e)})

The error: TypeError: stat: path should be string, bytes, os.PathLike or integer, not SpooledTemporaryFile

Acconut commented 5 months ago

Yes, right now uploads with tus-py-client must be backed by a file on disk, so the client can seek around and resume the upload if necessary.