question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Customizing UploadFile

See original GitHub issue

Description I found issue with Starlette’s UploadFile where it actually always buffers file to RAM fully and never switches to disk.

To overcome this until new version lands, I want to use my own version of UploadFile. This is what I tried:

import logging
from fastapi import FastAPI, File, UploadFile

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
app = FastAPI()

class FixedUploadFile(UploadFile):
    def __init__(self, *args, **kwargs):
        logger.info("FixedUploadFile is initializing...")
        super().__init__(self, *args, **kwargs)
        self.file.rollover()

@app.post("/uploadfile/")
async def create_upload_file(file: FixedUploadFile = File(...)):
    logger.info("Upload file handler")
    return {"filename": file.filename}

Running this I see that “FixedUploadFile is initializing…” is not called. How can I supply a custom class for UploadFile? (Either FastAPI’s or Starlette’s)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
haizaarcommented, Jul 16, 2019

Yeah… gross… Thanks though. Here is another one. We can start code-ugly competition 😃

from starlette.datastructures import UploadFile as StarletteUploadFile

def fix_starlette_upload_file():
    if hasattr(StarletteUploadFile, "_fixed"):
        return  # Occational double-call will chain initialization

    _init = StarletteUploadFile.__init__

    def new_init(self, *args, **kwargs):
        _init(self, *args, **kwargs)
        logger.info("Forcing rollover")
        self.file.rollover()

    StarletteUploadFile.__init__ = new_init
    StarletteUploadFile._fixed = True


fix_starlette_upload_file()

1reaction
haizaarcommented, Feb 21, 2020

@tiangolo this ticket was about customizing UploadFile in FastAPI, but the use case was a bug in then current UploadFile implementation. The bug was fixed at https://github.com/encode/starlette/issues/579 long time ago, but this ticket was left open since it was about customizing UploadFile which is still not possible without monkey patching.

I think it’s indeed resolved for now, because the only reason to customize was to work around a Starlette bug, hence the original use case became irrelevant and with it the whole reason to customize 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

File Upload Custom Question - RSVPify Help Center
To set up your file upload custom question you need to: Step 1: Navigate to Form Builder and locate the 'Custom Question' form...
Read more >
File Upload Question - Qualtrics
Import Responses · CSV/TSV Upload Issues · Retake Survey Link · Combining Responses · Response Editing; Creating New Fields. Custom Field Creation ...
Read more >
Create File Upload Questions - WebAssign
Create File Upload Questions · Click Questions > Create. · In Name, type a name for the question. · In Mode, select File-Upload....
Read more >
How to Add a File Upload Question in Google Forms and Why
For documents, images, and videos, have a respondent upload a file in Google Forms. Here's how to add and customize that question type....
Read more >
Attaching a File to a Custom Question - Smartwaiver Support
5. Within your new custom section, click and drag the Upload Files question option (from the left sidebar) into your custom question section....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found