【问题标题】:Microsoft Azure Timer Function not uploading files to blob storageMicrosoft Azure 计时器功能未将文件上传到 blob 存储
【发布时间】:2022-06-10 20:17:37
【问题描述】:

我正在尝试做一个计时器触发器,每隔 5 分钟,该函数会将一个文件上传到我的 blob 存储中。

当我在本地运行我的代码时,它可以工作,但在 Azure 上部署时它会失败。任何帮助将不胜感激。

主要方法

device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
PATH_TO_FILE = wget.download("link-of-something", out=os.getcwd()) 
device_client.connect()
blob_name = os.path.basename(PATH_TO_FILE)
storage_info = device_client.get_storage_info_for_blob(blob_name)
store_blob(storage_info, PATH_TO_FILE)
device_client.shutdown()

辅助方法

def store_blob(blob_info, file_name):
    try:
        sas_url = "https://{}/{}/{}{}".format(
            blob_info["hostName"],
            blob_info["containerName"],
            blob_info["blobName"],
            blob_info["sasToken"]
        )

        print("\nUploading file: {} to Azure Storage as blob: {} in container {}\n".format(file_name, blob_info["blobName"], blob_info["containerName"]))

        # Upload the specified file
        with BlobClient.from_blob_url(sas_url) as blob_client:
            with open(file_name, "rb") as f:
                result = blob_client.upload_blob(f, overwrite=True)
                return (True, result)

    except FileNotFoundError as ex:
        # catch file not found and add an HTTP status code to return in notification to IoT Hub
        ex.status_code = 404
        return (False, ex)

    except AzureError as ex:
        # catch Azure errors that might result from the upload operation
        return (False, ex)

【问题讨论】:

    标签: python azure-functions azure-blob-storage


    【解决方案1】:

    您可以做的是使用 docker 将函数容器化,并在容器内放置文件,以便稍后读取文件。

    因为如果不将函数容器化,只会部署函数的代码,不会部署文件。

    请参阅此documentation 以获得深入的解释。

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 2014-03-17
      • 2017-01-24
      • 2017-08-19
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多