コンテンツへスキップ

UploadFile クラス

リクエストからファイルを受信するには、パスオペレーション関数のパラメータをUploadFile型として定義できます。

fastapiから直接インポートできます。

from fastapi import UploadFile

fastapi.UploadFile

UploadFile(file, *, size=None, filename=None, headers=None)

ベース: UploadFile

リクエストでアップロードされたファイル。

パスオペレーション関数(または依存関係)パラメータとして定義します。

通常のdef関数を使用している場合、upload_file.file属性を使用して、生の標準的なPythonファイル(ブロッキング、非同期ではない)にアクセスできます。これは非同期コードには便利であり、必要です。

リクエストファイルに関するFastAPIドキュメントで詳細をご覧ください。

from typing import Annotated

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.post("/files/")
async def create_file(file: Annotated[bytes, File()]):
    return {"file_size": len(file)}


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
    return {"filename": file.filename}
パラメータ 説明
file

型: BinaryIO

size

型: int | None デフォルト: None

filename

型: str | None デフォルト: None

headers

型: Headers | None デフォルト: None

starlette/datastructures.py のソースコード
414
415
416
417
418
419
420
421
422
423
424
425
def __init__(
    self,
    file: typing.BinaryIO,
    *,
    size: int | None = None,
    filename: str | None = None,
    headers: Headers | None = None,
) -> None:
    self.filename = filename
    self.file = file
    self.size = size
    self.headers = headers or Headers()

file インスタンス属性

file

標準的な Python ファイルオブジェクト(非同期ではない)。

filename インスタンス属性

filename

元のファイル名。

size インスタンス属性

size

ファイルサイズ(バイト単位)。

headers インスタンス属性

headers

リクエストのヘッダー。

content_type インスタンス属性

content_type

ヘッダーからのリクエストのコンテンツタイプ。

read 非同期

read(size=-1)

ファイルからバイトを読み取る。

非同期と互換性のある awaitable にするために、これはスレッドプールで実行されます。

パラメータ 説明
size

ファイルから読み取るバイト数。

型: int デフォルト: -1

fastapi/datastructures.py のソースコード
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
async def read(
    self,
    size: Annotated[
        int,
        Doc(
            """
            The number of bytes to read from the file.
            """
        ),
    ] = -1,
) -> bytes:
    """
    Read some bytes from the file.

    To be awaitable, compatible with async, this is run in threadpool.
    """
    return await super().read(size)

write async

write(data)

ファイルにバイトデータを書き込みます。

通常、リクエストで読み込んだファイルに対してこのメソッドを使用することはありません。

非同期と互換性のある awaitable にするために、これはスレッドプールで実行されます。

パラメータ 説明
data

ファイルに書き込むバイトデータ。

型: bytes

fastapi/datastructures.py のソースコード
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
async def write(
    self,
    data: Annotated[
        bytes,
        Doc(
            """
            The bytes to write to the file.
            """
        ),
    ],
) -> None:
    """
    Write some bytes to the file.

    You normally wouldn't use this from a file you read in a request.

    To be awaitable, compatible with async, this is run in threadpool.
    """
    return await super().write(data)

seek async

seek(offset)

ファイル内の位置を移動します。

次の読み込みまたは書き込み操作は、その位置から実行されます。

非同期と互換性のある awaitable にするために、これはスレッドプールで実行されます。

パラメータ 説明
offset

ファイル内で移動する位置(バイト単位)。

型: int

fastapi/datastructures.py のソースコード
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
async def seek(
    self,
    offset: Annotated[
        int,
        Doc(
            """
            The position in bytes to seek to in the file.
            """
        ),
    ],
) -> None:
    """
    Move to a position in the file.

    Any next read or write will be done from that position.

    To be awaitable, compatible with async, this is run in threadpool.
    """
    return await super().seek(offset)

close async

close()

ファイルを閉じます。

非同期と互換性のある awaitable にするために、これはスレッドプールで実行されます。

fastapi/datastructures.py のソースコード
133
134
135
136
137
138
139
async def close(self) -> None:
    """
    Close the file.

    To be awaitable, compatible with async, this is run in threadpool.
    """
    return await super().close()