Azure函数Python:blob.set方法通过输出绑定在blob容器中存储空文件

fzsnzjdm  于 2022-11-30  发布在  Python
关注(0)|答案(1)|浏览(116)

通过blob触发事件触发azure功能。

  • 触发器从new-container向函数提供myblob的csv文件。
  • 该函数还从base-container获取base.csv作为base
  • 两个CSV文件都通过Pandas库读取。
  • 进行一些处理以创建df_final
  • df_final通过to_csv()转换为字符串表示。
  • CSV的字符串表示形式转换为utf-8编码。
  • blobout.set()将编码为CSV的base-container存储为base.csv

一切都按预期运行,但唯一的问题是当base.csv通过blobout.set存储时,它是一个0字节大小的空bile,尽管df_final有记录。

初始化.py

import logging
import pandas as pd

import azure.functions as func
from io import BytesIO

def main(myblob: func.InputStream, base: func.InputStream, blobout: func.Out[bytes]):
    df_base = pd.read_csv(BytesIO(base.read()))
    df_new = pd.read_csv(BytesIO(myblob.read()))
    df_final = process_data(df_base, df_new)
    df_final = df_final.to_csv(index=False)
    blobout.set(df_final.encode())

函数.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "new-container/{name}",
      "connection": "my_storage"
    },
    {
      "type": "blob",
      "name": "base",
      "path": "base-container/base.csv",
      "connection": "my_storage",
      "direction": "in"
    },
    {
      "name": "blobout",
      "type": "blob",
      "direction": "out",
      "path": "base-container/base.csv",
      "connection": "my_storage"
  }
  ]
}
gcuhipw9

gcuhipw91#

从我的端重现之后,这是工作正常。我相信没有值从process_data得到返回。确保没有空值从process_data得到返回。下面是我在process_data中使用的一个示例代码,它给了我预期的结果。

def process_data(df_base,df_new):
    final_dataframe = pd.concat([df_base, df_new])
    return final_dataframe
    • 初始化py**
import logging
import pandas as pd

import azure.functions as func
from io import BytesIO

def main(myblob: func.InputStream, base: func.InputStream, blobout: func.Out[bytes]):
    df_base = pd.read_csv(BytesIO(base.read()))
    df_new = pd.read_csv(BytesIO(myblob.read()))
    df_final = process_data(df_base, df_new)
    df_final = df_final.to_csv(index=False)
    blobout.set(df_final.encode())
    
def process_data(df_base,df_new):
    final_dataframe = pd.concat([df_base, df_new])
    return final_dataframe
    • 函数. json**
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "new-container/{name}",
      "connection": "<STORAGE_CONNECTION>"
    },
    {
      "name": "base",
      "type": "blob",
      "path": "base-container/base.csv",
      "connection": "<STORAGE_CONNECTION>",
      "direction": "in"
    },
    {
      "name": "blobout",
      "type": "blob",
      "path": "base-container/base.csv",
      "connection": "<STORAGE_CONNECTION>",
      "direction": "out"
  }
  ]
}
  • 结果 *

相关问题