使用 Azure CLI 配置 IoT 中心文件上传Configure IoT Hub file uploads using Azure CLI

若要从设备上传文件,必须先将 Azure 存储帐户与 IoT 中心关联。To upload files from a device, you must first associate an Azure Storage account with your IoT hub. 可以使用现有存储帐户,也可以创建新的存储帐户。You can use an existing storage account or create a new one.

要完成本教程,需要以下各项:To complete this tutorial, you need the following:

登录并设置 Azure 帐户Sign in and set your Azure account

登录到 Azure 帐户,并选择订阅。Sign in to your Azure account and select your subscription.

  1. 在命令提示符中,运行 login 命令At the command prompt, run the login command:

    az login
    

    按照说明使用代码进行身份验证,并通过 Web 浏览器登录 Azure 帐户。Follow the instructions to authenticate using the code and sign in to your Azure account through a web browser.

  2. 如果有多个 Azure 订阅,登录 Azure 可获得与凭据关联的所有 Azure 帐户的访问权限。If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure accounts associated with your credentials. 使用以下命令列出可供使用的 Azure 帐户Use the following command to list the Azure accounts available for you to use:

    az account list
    

    使用以下命令,选择想要用于运行命令以创建 IoT 中心的订阅。Use the following command to select the subscription that you want to use to run the commands to create your IoT hub. 可使用上一命令输出中的订阅名称或 ID:You can use either the subscription name or ID from the output of the previous command:

    az account set --subscription {your subscription name or id}
    

检索存储帐户详细信息Retrieve your storage account details

以下步骤假设已使用 Resource Manager 部署模型而不经典部署模型创建了存储帐户。The following steps assume that you created your storage account using the Resource Manager deployment model, and not the Classic deployment model.

若要从设备配置文件上传,需要 Azure 存储帐户的连接字符串。To configure file uploads from your devices, you need the connection string for an Azure storage account. 存储帐户必须与 IoT 中心位于同一订阅中。The storage account must be in the same subscription as your IoT hub. 还需要存储帐户中 Blob 容器的名称。You also need the name of a blob container in the storage account. 使用以下命令检索存储帐户密钥:Use the following command to retrieve your storage account keys:

az storage account show-connection-string --name {your storage account name} --resource-group {your storage account resource group}

记下 connectionString 值。Make a note of the connectionString value. 在后续步骤中需要用到它。You need it in the following steps.

可将现有的 Blob 容器用于文件上传,或新建一个容器:You can either use an existing blob container for your file uploads or create a new one:

  • 若要列出存储帐户中的现有 Blob 容器,请使用以下命令:To list the existing blob containers in your storage account, use the following command:

    az storage container list --connection-string "{your storage account connection string}"
    
  • 若要在存储帐户中创建 Blob 容器,请使用以下命令:To create a blob container in your storage account, use the following command:

    az storage container create --name {container name} --connection-string "{your storage account connection string}"
    

文件上传File upload

现在可以使用存储帐户详细信息配置 IoT 中心以将文件上传到 IoT 中心You can now configure your IoT hub to enable the ability to upload files to the IoT hub using your storage account details.

配置需要以下值:The configuration requires the following values:

存储容器:当前 Azure 订阅中要与 IoT 中心关联的 Azure 存储帐户中的 Blob 容器。Storage container: A blob container in an Azure storage account in your current Azure subscription to associate with your IoT hub. 检索在上一部分中必要的存储帐户信息。You retrieved the necessary storage account information in the preceding section. IoT 中心会自动生成对此 Blob 容器具有写入权限的 SAS URI,以供设备上传文件时使用。IoT Hub automatically generates SAS URIs with write permissions to this blob container for devices to use when they upload files.

接收已上传文件的通知:启用或禁用文件上传通知。 Receive notifications for uploaded files: Enable or disable file upload notifications.

SAS TTL:此设置是 IoT 中心返回给设备的 SAS URI 生存时间。SAS TTL: This setting is the time-to-live of the SAS URIs returned to the device by IoT Hub. 默认设置为一小时。Set to one hour by default.

文件通知设置默认 TTL:文件上传通知到期前的生存时间。File notification settings default TTL: The time-to-live of a file upload notification before it is expired. 默认设置为一天。Set to one day by default.

文件通知最大传送数:IoT 中心将尝试传送文件上传通知的次数。File notification maximum delivery count: The number of times the IoT Hub attempts to deliver a file upload notification. 默认设置为 10。Set to 10 by default.

使用以下 Azure CLI 命令在 IoT 中心内配置文件上传设置:Use the following Azure CLI commands to configure the file upload settings on your IoT hub:

在 bash shell 中使用:In a bash shell use:

az iot hub update --name {your iot hub name} \
  --set properties.storageEndpoints.'$default'.connectionString="{your storage account connection string}"

az iot hub update --name {your iot hub name} \
  --set properties.storageEndpoints.'$default'.containerName="{your storage container name}"

az iot hub update --name {your iot hub name} \
  --set properties.storageEndpoints.'$default'.sasTtlAsIso8601=PT1H0M0S

az iot hub update --name {your iot hub name} \
  --set properties.enableFileUploadNotifications=true

az iot hub update --name {your iot hub name} \
  --set properties.messagingEndpoints.fileNotifications.maxDeliveryCount=10

az iot hub update --name {your iot hub name} \
  --set properties.messagingEndpoints.fileNotifications.ttlAsIso8601=PT1H0M0S

可以使用以下命令在 IoT 中心内查看文件上传配置:You can review the file upload configuration on your IoT hub using the following command:

az iot hub show --name {your iot hub name}

后续步骤Next steps

有关 IoT 中心文件上传功能的详细信息,请参阅从设备上传文件For more information about the file upload capabilities of IoT Hub, see Upload files from a device.

若要了解有关如何管理 Azure IoT 中心的详细信息,请参阅以下链接:Follow these links to learn more about managing Azure IoT Hub:

若要进一步探索 IoT 中心的功能,请参阅:To further explore the capabilities of IoT Hub, see: