简体   繁体   English

从 Azure Blob 存储加载 Azure 函数中的 pickle 文件

[英]Load pickle file in Azure function from Azure Blob Storage

I try to build service via Azure functions that performs a matrix multiplication using a vector given by the http request and a fix numpy matrix.我尝试通过 Azure 函数构建服务,该函数使用 http 请求提供的向量和修复 numpy 矩阵执行矩阵乘法。 The matrix is stored in Azure blob storage as a pickle file and I want to load it via an input binding.矩阵作为泡菜文件存储在 Azure blob 存储中,我想通过输入绑定加载它。 However I do not manage to load the pickle file.但是我无法加载泡菜文件。 I am able to load plain text files.我能够加载纯文本文件。 Right now my approach looks like this:现在我的方法是这样的:

    def main(req: func.HttpRequest, blobIn: func.InputStream) -> func.HttpResponse:
       logging.info('Python HTTP trigger function processed a request.')
       matrix = pickle.loads(blobIn.read())
       vector = req.params.get('vector')
       result = matrix.dot(vector)
       return func.HttpResponse(json.dumps(result))

The error I get when running it that way is UnpicklingError: invalid load key, '\\xef' .以这种方式运行时遇到的错误是UnpicklingError: invalid load key, '\\xef' Another approach I tried after some googling was the following:我在谷歌搜索后尝试的另一种方法如下:

    def main(req: func.HttpRequest, blobIn: func.InputStream) -> func.HttpResponse:
       logging.info('Python HTTP trigger function processed a request.')
       blob_bytes = matrix.read()
       blob_to_read = BytesIO(blob_bytes)
       with blob_to_read as f: 
          A = pickle.load(f)
       vector = req.params.get('vector')
       result = matrix.dot(vector)
       return func.HttpResponse(json.dumps(result))

But it yields the same error.但它会产生相同的错误。 I also tried to save the matrix in a text file, get the string and build the matrix based on the string, but I encountered other issues.我还尝试将矩阵保存在文本文件中,获取字符串并根据字符串构建矩阵,但我遇到了其他问题。 So how can I load a pickle file in my Azure function?那么如何在 Azure 函数中加载 pickle 文件呢? Is it even the correct approach to use input bindings to load such files or is there a better way?使用输入绑定加载此类文件甚至是正确的方法还是有更好的方法? Many thanks for your help!非常感谢您的帮助!

Thanks for evilSnobu's contribution.感谢 evilSnobu 的贡献。

So when face this problem, that means the pickle file you get in your code is corrupt.因此,当遇到此问题时,这意味着您在代码中获得的泡菜文件已损坏。

The solution is add "dataType": "binary" to the input binding in function.json.解决方案是在"dataType": "binary"中的输入绑定中添加"dataType": "binary"

Like this:像这样:

{
  "name": "inputBlob",
  "type": "blob",
  "dataType": "binary",
  "direction": "in",
  "path": "xxx/xxx.xxx",
  "connection": "AzureWebJobsStorage"
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM