gpt4 book ai didi

python - Azure函数读取和写入Blob存储抛出内部500错误

转载 作者:行者123 更新时间:2023-12-03 03:46:54 28 4
gpt4 key购买 nike

我绝不是Python方面的专家,但我已经尽力了。我用 python 编写了一段简短的代码,它从 blob 存储中读取一个文本文件,并在其后面附加一些列名称,然后将其输出到目标文件夹。当我从 VS Code 运行时,代码可以正确执行。

当我尝试通过 Azure Function 运行它时,我发现它抛出一个错误。

是否是我的 function.json 绑定(bind)文件中的参数错误?

{“状态代码”:500,"message": "内部服务器错误"}

这是我的原始代码:

import logging

import azure.functions as func

from azure.storage.blob import BlobClient,BlobServiceClient

def main():
logging.info(' start Python Main function processed a request.')

#CONNECTION STRING
blob_service_client = BlobServiceClient.from_connection_string(CONN_STR)

# MAP SOURCE FILE
blob_client = blob_service_client.get_blob_client(container="source", blob="source.txt")

#SOURCE CONTENTS
content= blob_client.download_blob().content_as_text()

# WRITE HEADER TO A OUT PUTFILE
output_file_dest = blob_service_client.get_blob_client(container="target", blob="target.csv")

#INITIALIZE OUTPUT
output_str = ""

#STORE COULMN HEADERS
data= list()

data.append(list(["column1", "column2", "column3", "column4"]))

output_str += ('"' + '","'.join(data[0]) + '"\n')

output_file_dest.upload_blob(output_str,overwrite=True)
logging.info(' END OF FILE UPLOAD')

if __name__ == "__main__":
main()

这是我的 function.json

{
"scriptFile": "__init__.py",

"bindings": [
{
"type": "blob",
"direction": "in",
"name": "blob_client",
"path": "source/source.txt",
"connection": "STORAGE_CONN_STR"
},
{
"type": "blob",
"direction": "out",
"name": "output_file_dest",
"path": "target/target.csv",
"connection": "STORAGE_CONN_STR"
}
]
}

这是我的要求.txt

azure-functions==1.7.2
azure-storage-blob==12.8.1

最佳答案

尝试将连接字符串添加到 Function App 的应用程序设置中。应用程序设置可以在“配置”(“设置”下)下找到。单击新应用程序设置,输入名称并指定值。然后使用以下代码连接到 blob:

from azure.storage.blob import BlobClient,BlobServiceClient
import os

#CONNECTION STRING
blob_service_client = BlobServiceClient.from_connection_string(os.getenv('the name of your connection string in the application settings')

关于python - Azure函数读取和写入Blob存储抛出内部500错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68769200/

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