简体   繁体   English

如何从 FastAPI 中的 UploadFile 获取文件路径?

[英]How to get file path from UploadFile in FastAPI?

Basically, I'm trying to create an endpoint to Upload files to S3基本上,我正在尝试创建一个端点以将文件上传到 S3

async def upload_files(filepath: str, upload_file_list: List[UploadFile] = File(...)):
    for upload_file in upload_file_list:
        abs_file_path = "/manual/path/works" + upload_file.path
        # Replace above line to get absolute file path from UploadFile
        response = s3_client.upload_file(abs_file_path,bucket_name,
                                                   os.path.join(dest_path, upload_file.filename))

Above is my code to upload multiple files to the S3 bucket.以上是我将多个文件上传到 S3 存储桶的代码。 s3_client.upload_file() accepts an absolute file path of the file to upload. s3_client.upload_file()接受要上传的文件的绝对文件路径。 It's working when I manually put the full path.当我手动放置完整路径时它正在工作。

This didn't work:这没有用:

response = s3_client.upload_file(upload_file.filename, bucket_name,
                                                   os.path.join(dest_path, upload_file.filename))

Is there a way to get this absolute path in FastAPI ?有没有办法在FastAPI中获得这个绝对路径? Or any alternative with temp_path without copying or writing the file?或者在不复制或写入文件的情况下使用temp_path的任何替代方法?

If not, then any alternative with boto3 to upload files to S3 using FastAPI如果没有,那么FastAPI的任何替代方法都可以使用boto3将文件上传到 S3

UploadFile uses Python's SpooledTemporaryFile , which is a "file stored in memory", and "is destroyed as soon as it is closed". UploadFile 使用 Python 的SpooledTemporaryFile ,它是“存储在内存中的文件”,“一关闭就销毁”。 You can either read the file contents (ie, contents = await file.read()) and then upload these bytes to your server (if it permits), or please take a look at this answer on how to copy the contents of the uploaded file into a NamedTemporaryFile , which, unlike SpooledTemporaryFile, " is guaranteed to have a visible name in the file system" that "can be used to open the file".您可以读取文件内容(ie, contents = await file.read()) ,然后将这些字节上传到您的服务器(如果允许),或者请查看这个关于如何复制上传内容的答案文件放入NamedTemporaryFile ,与 SpooledTemporaryFile 不同,“保证在文件系统中具有可见的名称”,“可用于打开文件”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 FastAPI 中保存 UploadFile - How to save UploadFile in FastAPI FastAPI - 如何在使用 UploadFile 时读取 json 文件 - FastAPI - How to read an json file while using UploadFile 使用 fastapi UploadFile 验证文件类型和扩展名 - validate file type and extention with fastapi UploadFile 如何在 FastAPI 中为 UploadFile 创建 OpenAPI 架构? - How to create an OpenAPI schema for an UploadFile in FastAPI? 如何在 FastAPI 中读取 UploadFile? - How can I read UploadFile in FastAPI? 如何 select FastAPI 中 UploadFile 参数的磁盘位置? - How to select the disk location for UploadFile parameter in FastAPI? 如何使用 FastAPI UploadFile 在 Python 中将多类型/表单数据保存到硬文件? - How do you save multitype/form data to a hard file in Python with FastAPI UploadFile? 如何将作为 zip 文件的 FastAPI UploadFile 保存到磁盘 as.zip? - How do I save a FastAPI UploadFile which is a zip file to disk as .zip? FastAPI:如何从请求中获取原始 URL 路径? - FastAPI: How to get raw URL path from request? 从 csv 文件创建字典。 我将 csv 文件作为 fastapi 中 api 端点的输入(文件:UploadFile = File(...)) - Creating a dictionary from a csv file. I am taking the csv file as an input in an api endpoint in fastapi (file: UploadFile = File(...))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM