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

要使用 IoT 中心的文件上传功能,必须先将 Azure 存储帐户与 IoT 中心关联。To use the file upload functionality in IoT Hub, you must first associate an Azure storage account with your IoT hub. 可以使用现有存储帐户,也可以创建新的存储帐户。You can use an existing storage account or create a new one.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

要完成本教程,需要以下各项: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. 在 PowerShell 提示符下,运行 Connect-AzAccountAt the PowerShell prompt, run the Connect-AzAccount cmdlet:

    Connect-AzAccount -Environment AzureChinaCloud
    
  2. 如果有多个 Azure 订阅,则访问 Azure 即有权访问与凭据关联的所有 Azure 订阅。If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure subscriptions associated with your credentials. 使用以下命令,列出可供使用的 Azure 订阅:Use the following command to list the Azure subscriptions available for you to use:

    Get-AzSubscription
    

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

    Select-AzSubscription `
        -SubscriptionName "{your subscription name}"
    

检索存储帐户详细信息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:

Get-AzStorageAccountKey `
  -Name {your storage account name} `
  -ResourceGroupName {your storage account resource group}

记下 key1 存储帐户密钥值。Make a note of the key1 storage account key value. 在后续步骤中需要用到它。You need it in the following steps.

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

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

    $ctx = New-AzStorageContext `
        -StorageAccountName {your storage account name} `
        -StorageAccountKey {your storage account key}
    Get-AzStorageContainer -Context $ctx
    
  • 若要在存储帐户中创建 Blob 容器,请使用以下命令:To create a blob container in your storage account, use the following commands:

    $ctx = New-AzStorageContext `
        -StorageAccountName {your storage account name} `
        -StorageAccountKey {your storage account key}
    New-AzStorageContainer `
        -Name {your new container name} `
        -Permission Off `
        -Context $ctx
    

配置 IoT 中心Configure your IoT hub

现在可以使用存储帐户详细信息配置 IoT 中心以将文件上传到 IoT 中心You can now configure your IoT hub 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.

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

Set-AzIotHub `
    -ResourceGroupName "{your iot hub resource group}" `
    -Name "{your iot hub name}" `
    -FileUploadNotificationTtl "01:00:00" `
    -FileUploadSasUriTtl "01:00:00" `
    -EnableFileUploadNotifications $true `
    -FileUploadStorageConnectionString "DefaultEndpointsProtocol=https;AccountName={your storage account name};AccountKey={your storage account key};EndpointSuffix=core.chinacloudapi.cn" `
    -FileUploadContainerName "{your blob container name}" `
    -FileUploadNotificationMaxDeliveryCount 10

后续步骤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: