gpt4 book ai didi

python - 将文件作为输入参数的 API

转载 作者:行者123 更新时间:2023-12-04 14:18:09 27 4
gpt4 key购买 nike

有人要求我创建一个 API,将文件作为输入并将该文件放在服务器上的目录中。这是为了消除应用程序直接与文件夹交互的需要。

我之前构建过 API(在 R 中使用 Plumber)并且可以处理字符串输入 - 我只是难以接受一个文件作为输入参数。管道工文档均未解释如何执行此操作。这甚至可以做到吗?有没有办法在 Python 中执行此操作?

最佳答案

您可以使用 plumberRook 通过 POST 上传文件。

这是一个小例子

api.R

library(plumber)
library(Rook)

#* Upload file
#* @param upload File to upload
#* @post /uploadfile
function(req, res){
fileInfo <- list(formContents = Rook::Multipart$parse(req))
## The file is downloaded in a temporary folder
tmpfile <- fileInfo$formContents$upload$tempfile
## Copy the file to a new folder, with its original name
fn <- file.path('~/Downloads', fileInfo$formContents$upload$filename)
file.copy(tmpfile, fn)
## Send a message with the location of the file
res$body <- paste0("Your file is now stored in ", fn, "\n")
res
}

运行服务器

plumber::plumb('api.R')$run(port = 1234)

使用 curl 发送文件 test.txt

curl -v -F upload=@test.txt http://localhost:1234/uploadfile

关于python - 将文件作为输入参数的 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58067675/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com