gpt4 book ai didi

python - 使用 python azure 函数从 azure blob 存储读取文件

转载 作者:行者123 更新时间:2023-12-03 01:18:03 29 4
gpt4 key购买 nike

我在 Python 3.9 中创建了一个 Azure 函数,我将通过 Azure 数据工厂 使用 Http Trigger 执行该函数。

现在,在我的 python azure 函数中,我想从我的存储帐户访问存储容器并从同一容器读取文件,以便对文件数据执行少量数据操作。

我怎样才能实现这个目标?

我在这里发现了一个类似的问题: Read data from Azure blob storage in Azure Function in python

但是,在这个问题的解决方案中,作者明确访问了一个特定文件,但在我的例子中,我想一一访问容器的所有文件并读取它们的数据。

这就像从本地计算机的目录访问文件一样,并逐一读取每个访问文件的内容。

最佳答案

在我这边复制后,我可以使用BlockBlobService来实现您的要求。下面是对我有用的代码。

import logging

import azure.functions as func
from azure.storage.blob import BlockBlobService

ACCOUNT_NAME = "<ACCOUNT_NAME>"
SAS_TOKEN='<SAS_TOKEN>'

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

file=""
fileContent=""
blob_service = BlockBlobService(account_name=ACCOUNT_NAME,account_key=None,sas_token=SAS_TOKEN)
containername="<CONTAINER_NAME>"
generator = blob_service.list_blobs(container_name=containername) #lists the blobs inside containers
for blob in generator:
file=blob_service.get_blob_to_text(containername,blob.name)
logging.info(file.content)
fileContent+=blob.name+'\n'+file.content+'\n\n'

return func.HttpResponse(f"{fileContent}")

结果:

在我的存储帐户中

enter image description here

运行结果

enter image description here

关于python - 使用 python azure 函数从 azure blob 存储读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73570504/

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