Skip to Content
ファストAPI
book

ファストAPI

by Bill Lubanovic
March 2025
Intermediate to advanced
280 pages
3h 20m
Japanese
O'Reilly Media, Inc.
Content preview from ファストAPI

第15章 ファイル ファイル

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

プレビュー

APIリクエストやHTMLのような伝統的なコンテンツを扱うだけでなく、ウェブサーバは双方向のファイル転送を処理することが期待されている。 非常に大きなファイルは、システムのメモリをあまり使わないように、塊で転送する必要があるかもしれない。また、Stati⁠c​Files を使って、ファイル・ディレクトリ(および任意の深さのサブディレクトリ)へのアクセスを提供することもできる。

マルチパートのサポート

大きなファイルを処理するために、FastAPIのアップロードとダウンロード機能には、これらの余分なモジュールが必要である:

Pythonマルチパート

pip install python-multipart

aioファイル

pip install aiofiles

ファイルをアップロードする

FastAPIはAPI開発をターゲットにしており、本書のほとんどの例ではJSONリクエスト/レスポンスを使ってきた。 しかし次の章では、扱いが異なるフォームを見ることになる。 この章では、ある意味ではフォームのように扱われるファイルを取り上げる。

FastAPIはファイルアップロードに2つのテクニックを提供する:File()UploadFile

ファイル()

File() パス関数は同期式( )でも非同期式( )でも構わないが、ファイルをアップロードしている間、ウェブサーバに負荷をかけないという点で、非同期式の方が優れている。defasync def

FastAPIはファイルをチャンクでプルアップし、メモリ内で再アセンブルするので、File() 、比較的小さなファイルのみに使用すべきである。入力がJSONであると仮定する代わりに、FastAPIはファイルをフォーム要素としてエンコーディングする。

ファイルをリクエストし、それをテストするコードを書いてみよう。テストするファイルは、マシンにあるものでもよいし、Fastest Fishのようなサイトからダウンロードしてもよい。、私はそこから1KBのファイルを取得し、ローカルに1KB.binとして保存した。

例15-1ではmain.pyの先頭に以下の行を追加する。

例 15-1. FastAPI で小さなファイルアップロードを処理する
from fastapi import File

@app.post("/small")
async def upload_small_file(small_file: bytes = File()) -> str:
    return f"file size: {len(small_file)}"

Uvicornが再起動したら、例15-2の HTTPieテストを試す。

例15-2. HTTPieを使って小さなファイルをアップロードする
$ http -f -b POST http://localhost:8000/small small_file@1KB.bin
"file size: 1000"

以下、このテストに関するメモをいくつか記す:

  • ファイルがJSONテキストとしてではなく、フォームのようにアップロードされるため、-f (または--form )を含める必要がある。

  • small_file@1KB.bin:

    small_file

    例 15-1の FastAPI パス関数の変数名 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GitHubアクションを学ぶ

GitHubアクションを学ぶ

Brent Laster
仕事ではじめる機械学習 第2版

仕事ではじめる機械学習 第2版

有賀 康顕, 中山 心太, 西林 孝

Publisher Resources

ISBN: 9798341624658