文件作为 pygeoapi 进程输入

问题描述 投票:0回答:1

我尝试构建一个可以从文件(只是包含“hello world”字符串的文本文件)读取数据的测试进程。

我设法使我的流程在流程休息端点可用:

http://localhost:5000/processes/myprocess

但我不知道如何将我的文本文件作为输入参数传递。我没有找到任何例子。

这是我的输入元数据(myprocess.py):

    'inputs': {
        'file': {
            'title': 'file',
            'description': 'what ever file',
            "schema": {
                "type": "string",
                "contentEncoding": "binary",
                "contentMediaType": "application/octet-stream"
            },
            'minOccurs': 1,
            'maxOccurs': 1,
            'metadata': None,
            'keywords': ['file']
        }
    },

这是进程应该执行的代码(myprocess.py):

    def execute(self, data):

        mimetype = 'application/json'
        file= data.get('file', None)
        with open(file, mode='rb') as myfile:  # b is important -> binary
            fileContent = myfile.read()
        
        outputs = {
            'result': fileContent
        }

        return mimetype, outputs

这是另一个独立的Python脚本,调用我的进程:

    filePath=r'C:\dev\test.txt'
    url='http://localhost:5000/processes/myprocess/execution'

    with open(filePath, "rb") as text_file:

        data = {
                  "inputs": {
                    "file": text_file
                  }
                }

        r = requests.post(url, json=data)
        print(r.content)

我尝试了很多方法,但在将二进制文件转换为 JSON 时收到错误消息:

TypeError: Object of type BufferedReader is not JSON serializable

或者我从进程中收到“缺少参数”消息(例如,如果我尝试对二进制文件进行 Base64 编码)

file process upload
1个回答
0
投票

我想发表评论,但我还没有足够的声誉。所以我会根据我对您问题的理解尝试回答。

您说要 POST 文件内容,但在您的示例中,您的 execute() 函数需要文件路径(而不是文件内容),在服务器上打开并读取文件,然后返回文件内容。

所以实际上,文件内容是您的

输出,而不是您的输入

但是您确实返回了 JSON,因此它尝试将二进制文件内容转换为 JSON,这就是它失败的地方。

如果您真正想要返回文件内容,请查看此示例:

'inputs': { 'filepath': { 'title': 'path to file on server', 'description': 'what ever file', "schema": { "type": "string", "contentMediaType": "application/json" }, 'minOccurs': 1, 'maxOccurs': 1, 'metadata': None, 'keywords': ['file'] } },
然后你的代码:

def execute(self, data): filepath= data.get('filepath', None) with open(filepath, mode='rb') as myfile: # b is important -> binary # Here you try to open a file that sits on the server! fileContent = myfile.read() mimetype = 'multipart/related' return mimetype, fileContent
OGC 规范中提到了 mimetype,您可能想查看一下:

https://docs.ogc.org/is/18-062r2/18-062r2.html#req_core_process-execute-sync-raw-价值多

调用它是:

filePath=r'C:\dev\test.txt' url='http://localhost:5000/processes/myprocess/execution' data = { "inputs": { "filepath": 'C:\dev\test.txt' } } r = requests.post(url, json=data) # Here you post json data! print(r.content)
    
© www.soinside.com 2019 - 2024. All rights reserved.