我正在尝试构建一个接收数据集名称的 Azure 函数,然后继续连接到 Azure Datalake 并从中下载文件(到本地磁盘)

I'm trying to build an Azure Function which receives dataset names, then proceeds to connect to and download files from Azure Datalake (to local disk)

我已经成功部署了一个 Azure 函数,可以将数据输入传递到该函数并对该数据进行操作。我的 objective 是捕获这些输入(table 用户需要的数据集的名称),然后从 Blob 存储下载这些数据集。对于下载部分,我有几段代码可以让我从 Azure Datatalake 成功下载给定文件(当我 运行 在本地 Python 代码时),但是当我将该代码放入要执行的 Azure 函数,未启动下载 - 我认为这可能是因为 Azure 函数没有引用需要将文件下载到的接收器。

当构建 SAS URL 并从 Azure 函数触发时,是否有任何方法可以将数据持久保存到本地磁盘?

%python

import logging
import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:

    logging.info('The API has initialized and will execute now.')

    # Open the payload sent to this function
    req_body = req.get_json()

    # Save the data request format type
    # dataset_format = req.params.get('dataset_format')
    dataset_format = req_body.get('dataset_format')

    logging.info("********   - Checkpoint 1 -   **********")
    # dataset list passed in as parameter 
    datasets = req_body.get('datasets')
    dataset_1 = datasets[0]
    dataset_2 = datasets[1]
    dataset_3 = datasets[2]

    # Download Option 1 (preference - when executing the SAS URL from a browser, it shows the downloads tab at the bottom of the browser with the downloaded file/s)
    import webbrowser
    sas_url = "https://apptestdatalake.blob.core.windows.net/**filesystem_name**/**blob_name**/Iris.csv?**sas token**"
    webbrowser.open(sas_url)

   # Download Option 2 
   from azure.storage.blob import BlobClient
   download_file_path = "C:/Users/**user**/Downloads/Requested Downloads/"
   print("\nDownloading blob data to \n\t" + download_file_path)

   try:
        os.makedirs(os.path.dirname("C:/Users/**user**/Downloads/Requested Downloads/Iris.csv"))
   except:
        pass
   with open(download_file_path, "wb") as download_file:
        blob_client = BlobClient.from_blob_url(sas_url)
        download_stream = blob_client.download_blob().readall()
        download_file.write(download_stream)

    print("Download Complete!")

    logging.info("********   - Checkpoint 2 -   **********")

    return func.HttpResponse(f"Hello! You've requested the {dataset_1}, {dataset_2}, {dataset_3} in {dataset_format}. This script has run successfully and your download(s) are complete!")```
    

Azure Functions 是一种 PaaS 层服务,可用于构建后端服务。它无法打开新的浏览器。如果你想这样做,我建议你可以在你的前端应用程序上使用 javascript 打开一个新的浏览器 session/tab.

所以根据您的需要,我建议您使用 Azure 函数为您的 blob 生成 sas 令牌,然后 return sas 令牌 URL 到前端应用程序并下载文件。关于如何下载,可以参考下面的代码

function myFunction(){
    var a = document.createElement("a");
  a.href = "";
 
  document.body.appendChild(a);
  a.click();
}