简体   繁体   English

使用 Azure 函数中的 Python 根据 Azure Blob 存储中的模式匹配检查 Blob 是否存在

[英]Check Existence of Blob based on Pattern match in Azure Blob Storage using Python in Azure Functions

Is there a way to check if file exists in a Azure BLOB container using Python based on pattern using Azure Function?有没有办法根据使用 Azure Function 的模式使用 Python 检查文件是否存在于 Azure BLOB 容器中? I have different files incoming and I need to process them based on "FileName" Parameter.我有不同的文件传入,我需要根据“文件名”参数处理它们。 The Files dropped on the container would come with appended date.放在容器上的文件将带有附加日期。 I need to check if the file Exists, then work over it.我需要检查文件是否存在,然后处理它。

Sample Files at container =>容器中的示例文件 =>

  1. API_File1_20202008.CSV API_File1_20202008.CSV

  2. API_File2_20202008.CSV API_File2_20202008.CSV

  3. API_File3_20202008.CSV API_File3_20202008.CSV

If Parameter passed in Function = > API_File1.如果参数传入 Function => API_File1.
Then the function should check if any blob in a specified with API_File1* exists, then process.然后 function 应该检查指定的 API_File1* 中是否存在任何 blob,然后处理。

For Local OS I was able to use the following.对于本地操作系统,我能够使用以下内容。

for name in glob.glob(SourceFolder +'/' + FileName + '*.CSV'):
    print(name)

Any suggestion?有什么建议吗?

For this requirement, you can use the code below to implement it on azure.对于这个需求,可以使用下面的代码在azure上实现。

import logging

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


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

    #just focus on below part
    block_blob_service = BlockBlobService(account_name="<your storage name>", account_key="<storage key>")

    if block_blob_service.exists("<container name>", "file5.csv"):
            print("========found file=======");
    else:
            print("========not exist========");
    #just focus on the part above

    name = req.params.get('name')
    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.")
    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
        )

Before run the code, you need to install the modules.在运行代码之前,您需要安装模块。 Do not install pip install azure-storage-blob , I test with install azure-storage-blob but it occurs some problem.不要安装pip install azure-storage-blob ,我测试了 install azure-storage-blob但它出现了一些问题。 You can install pip install azure==4.0.0 (it may take some time) in your virtual environment in "Terminal" window, and then the code can run success.你可以在“终端”window你的虚拟环境中安装pip install azure==4.0.0 (可能需要一些时间),然后代码就可以运行成功了。

=================================== Update ================================== ===================================更新============== ====================

I don't think we can query the file name by "File5" + "*.CSV" directly.我认为我们不能直接通过"File5" + "*.CSV" .CSV”来查询文件名。 But I can provide a workaround for your reference.但我可以提供一个解决方法供您参考。 Then function get the request parameter fileName and then use the code below:然后function获取请求参数fileName然后使用下面的代码:

files = [blob for blob in block_blob_service.list_blobs("testcontainer") if blob.name.startswith(fileName)]

for file in files:
    logging.info(file.name)

The code is used to get all of the file which start with the fileName you send to the function.该代码用于获取以您发送到fileName的文件名开头的所有文件。

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

相关问题 使用python azure函数检查文件是否存在于blob存储中 - Check if file exists in blob storage using python azure functions 如何在 Python 中使用 Azure Functions 的 Azure Blob 存储绑定将 JSON 数据上传到 Azure 存储 blob - How to upload JSON data to Azure storage blob using Azure Blob storage bindings for Azure Functions in Python 使用 python azure 函数从 azure blob 存储读取文件 - Read files from azure blob storage using python azure functions 如何使用 Python 和 Azure 函数在 Azure 存储容器中创建 blob - How to create a blob in an Azure Storage Container using Python & Azure Functions 如何使用 Azure 函数从 Blob 存储中读取 json 文件 Python - How to read json file from blob storage using Azure Functions Blob Trigger with Python 使用 python 将文件上传到 azure blob 存储 - Uploading file to azure blob storage using python 使用 python 条件删除 Azure blob 存储 - Conditional Delete Azure blob storage using python 使用 Python 的 azure httptrigger blob 存储 - azure httptrigger blob storage using Python 使用 python 将图像上传到 azure blob 存储 - Upload image to azure blob storage using python python azure blob存储md5检查使用put_block_blob_from_path上传blob失败 - python azure blob storage md5 check fails on blob upload using put_block_blob_from_path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM