| 347 | session = aiohttp.ClientSession() |
| 348 | |
| 349 | async def stream_generator(): |
| 350 | try: |
| 351 | async with session.get(link) as resp: |
| 352 | if resp.status != 200: |
| 353 | raise HTTPException( |
| 354 | status_code=resp.status, |
| 355 | detail=f"从S3获取文件失败: {resp.status}" |
| 356 | ) |
| 357 | # 设置块大小(例如64KB) |
| 358 | chunk_size = 65536 |
| 359 | while True: |
| 360 | chunk = await resp.content.read(chunk_size) |
| 361 | if not chunk: |
| 362 | break |
| 363 | yield chunk |
| 364 | finally: |
| 365 | await session.close() |
| 366 | |
| 367 | from fastapi.responses import StreamingResponse |
| 368 | encoded_filename = quote(filename, safe='') |