[英]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.