Skip to content

Commit 96837d3

Browse files
tchatonlexierule
authored andcommitted
Introduce Upload File endpoint (#14703)
* update * update * update * update * update * update * update * update * update * update [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci
1 parent c8ba2a4 commit 96837d3

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
66

77

8-
## [0.6.1] - 2022-09-16
8+
## [0.6.1] - 2022-09-19
9+
10+
### Added
11+
12+
- Add support to upload files to the Drive through an asynchronous `upload_file` endpoint ([#14703](https://github.com/Lightning-AI/lightning/pull/14703))
913

1014
### Changed
1115

src/lightning_app/core/api.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import traceback
66
from copy import deepcopy
77
from multiprocessing import Queue
8+
from tempfile import TemporaryDirectory
89
from threading import Event, Lock, Thread
910
from typing import Dict, List, Mapping, Optional
1011

1112
import uvicorn
1213
from deepdiff import DeepDiff, Delta
13-
from fastapi import FastAPI, HTTPException, Request, Response, status, WebSocket
14+
from fastapi import FastAPI, File, HTTPException, Request, Response, status, UploadFile, WebSocket
1415
from fastapi.middleware.cors import CORSMiddleware
1516
from fastapi.params import Header
1617
from fastapi.responses import HTMLResponse, JSONResponse
@@ -23,6 +24,7 @@
2324
from lightning_app.api.request_types import DeltaRequest
2425
from lightning_app.core.constants import FRONTEND_DIR
2526
from lightning_app.core.queues import RedisQueue
27+
from lightning_app.storage import Drive
2628
from lightning_app.utilities.app_helpers import InMemoryStateStore, Logger, StateStore
2729
from lightning_app.utilities.enum import OpenAPITags
2830
from lightning_app.utilities.imports import _is_redis_available, _is_starsessions_available
@@ -234,6 +236,29 @@ async def post_state(
234236
api_app_delta_queue.put(DeltaRequest(delta=Delta(deep_diff)))
235237

236238

239+
@fastapi_service.put("/api/v1/upload_file/{filename}")
240+
async def upload_file(filename: str, uploaded_file: UploadFile = File(...)):
241+
with TemporaryDirectory() as tmp:
242+
drive = Drive(
243+
"lit://uploaded_files",
244+
component_name="file_server",
245+
allow_duplicates=True,
246+
root_folder=tmp,
247+
)
248+
tmp_file = os.path.join(tmp, filename)
249+
250+
with open(tmp_file, "wb") as f:
251+
done = False
252+
while not done:
253+
# Note: The 8192 number doesn't have a strong reason.
254+
content = await uploaded_file.read(8192)
255+
f.write(content)
256+
done = content == b""
257+
258+
drive.put(filename)
259+
return f"Successfully uploaded '{filename}' to the Drive"
260+
261+
237262
@fastapi_service.get("/healthz", status_code=200)
238263
async def healthz(response: Response):
239264
"""Health check endpoint used in the cloud FastAPI servers to check the status periodically. This requires

0 commit comments

Comments
 (0)