Is there a way to check if file exists in a Azure BLOB container using Python based on pattern using Azure Function? 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 =>
API_File1_20202008.CSV
API_File2_20202008.CSV
API_File3_20202008.CSV
If Parameter passed in Function = > API_File1.
Then the function should check if any blob in a specified with API_File1* exists, then process.
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.
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. 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.
=================================== Update ==================================
I don't think we can query the file name by "File5" + "*.CSV"
directly. But I can provide a workaround for your reference. Then function get the request parameter fileName
and then use the code below:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.