簡體   English   中英

無法從 Azure 函數中的 azure.storage.blob 導入 AppendBlobService

[英]Unable to import AppendBlobService from azure.storage.blob in Azure function

我正在使用以下 azure 函數,它將 http 觸發器作為輸入。

import logging
import azure.functions as func
from . import test

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    strng = req.params.get('strng')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.sum = {test.testfunc(strng)}")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

下面是我在init .py 中導入的 test.py 文件。

import json
import pandas as pd
from pandas import DataFrame
from azure.storage.blob import AppendBlobService
from datetime import datetime


def testfunc(strng):
    # return strng
    API = json.loads(strng)

    test = pd.json_normalize(parse, record_path='Vol', meta=['studyDate'])
    df = pd.DataFrame(test)
    df["x"] = df["Vol"] * 2
    df["y"] = df["Vol"] * 50
    df1 = df[['Date', 'x', 'y']]
    df2 = df1.to_json(orient='records')
    append_blob_service = AppendBlobService(account_name='actname',
                                         account_key='key')
    date = datetime.now()
    blobname = f"test_cal{date}.json"
    append_blob_service.create_blob('container', blobname, if_none_match="*")
    append_blob_service.append_blob_from_text('container', blobname, text=df2)
    return df2
    

當我在 pycharm 和 databricks 中運行上面的函數時,它可以工作。 但是,當我通過 Visuals Studio 代碼運行 Azure 函數時,出現以下錯誤。

Exception: ImportError: cannot import name 'AppendBlobService' from 'azure.storage.blob'

我在下面嘗試過,仍然得到同樣的錯誤。

pip install azure-storage --upgrade
pip install azure-storage-blob

請提供有關錯誤的幫助。

有沒有其他方法可以將 df2 變量保存到 Azure 存儲。

謝謝你。

根據文檔,它說,

如果找不到模塊(例如azure-storage-blob ),Python 會拋出ModuleNotFoundError. 如果找到,則加載模塊或其某些文件可能存在問題。 在這些情況下,Python 會拋出ImportError

  • 嘗試將python設置為環境變量FUNCTIONS_WORKER_RUNTIME
  • 嘗試將azure.core添加到您的requirements.txt文件中。

引用自:

ImportError:無法從“azure.storage.blob”導入名稱“BlockBlobService”

Azure Functions 部署時運行失敗

當前版本庫包含 blobserviceclient 而不是 blockblockblockservice。 這對我有用,使用 2.1.0 版可以解決它:

pip install azure-storage-blob==2.1.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM