简体   繁体   English

使用python azure函数检查文件是否存在于blob存储中

[英]Check if file exists in blob storage using python azure functions

I want to check if a file exists in azure blob storage using python azure functions.我想使用 python azure 函数检查 azure blob 存储中是否存在文件。 We can use the python library BlobServiceClient, but it needs a connection with the blob.我们可以使用 python 库 BlobServiceClient,但它需要与 blob 建立连接。 I'm looking for a methode without using connection keys because I'm already connected to my azure account via vs code.我正在寻找一种不使用连接密钥的方法,因为我已经通过 vs 代码连接到我的 azure 帐户。

Even though your environment is logged into azure account, you need access key or connection string to connect to your storage account.即使您的环境已登录到 azure 帐户,您也需要访问密钥或连接字符串才能连接到您的存储帐户。 After reproducing from our end here is how we could able to check if the blob is present or not in azure storage.在从我们这里复制之后,我们如何能够检查 blob 是否存在于 azure 存储中。

import logging

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


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    
    ContainerName="<YOUR_CONTAINER_NAME>";
    AccountName="<YOUR_ACCOUNT_NAME>";
    AccountKey="<ACCESS_KEY>";

    block_blob_service = BlockBlobService(account_name=AccountName, account_key=AccountKey)
    
    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
           return func.HttpResponse(f"Enter the blob you are searching for")
        else:
            name = req_body.get('name')

    if block_blob_service.exists(container_name=ContainerName, blob_name=name):
        return func.HttpResponse(f"{name}, Is already Present in container")
    else:
        return func.HttpResponse(
             "{name}, Is not Present in container",
             status_code=200
        )

RESULT:结果:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

暂无
暂无

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

相关问题 使用 Azure 函数中的 Python 根据 Azure Blob 存储中的模式匹配检查 Blob 是否存在 - Check Existence of Blob based on Pattern match in Azure Blob Storage using Python in 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 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 Stream 文件到 Azure Blob 存储中的 Zip 文件,使用 ZA7F5F35426B9627411FC3Z231 - Stream Files to Zip File in Azure Blob Storage using Python? 使用 python 创建 csv 文件并将其上传到 azure blob 存储 - Create and upload csv file to azure blob storage using python 使用python上载csv文件以使Blob存储蔚蓝 - Uploading csv file using python to azure blob storage 使用 Azure-Storage-Blob Python 读取 Blob 容器目录中每个 Blob 的文件大小 - Reading the File size for each blob inside a directory of a Blob Container using Azure-Storage-Blob Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM