title | description | author | ms.service | services | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|
Configure file upload to IoT Hub using Azure CLI | Microsoft Docs |
How to configure file uploads to Azure IoT Hub using the cross-platform Azure CLI. |
dominicbetts |
iot-hub |
iot-hub |
conceptual |
08/08/2017 |
dobett |
[!INCLUDE iot-hub-file-upload-selector]
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:
-
An active Azure account. If you don't have an account, you can create a free account in just a couple of minutes.
-
An Azure IoT hub. If you don't have an IoT hub, you can use the
az iot hub create
command to create one or Create an IoT hub using the portal. -
An Azure Storage account. If you don't have an Azure Storage account, you can use the Azure CLI - Manage storage accounts to create one or use the portal to Create a storage account.
Sign in to your Azure account and select your subscription.
-
At the command prompt, run the login command:
az login
Follow the instructions to authenticate using the code and sign in to your Azure account through a web browser.
-
If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure accounts associated with your credentials. Use the following command to list the Azure accounts available for you to use:
az account list
Use the following command to select the subscription that you want to use to run the commands to create your IoT hub. 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}
The following steps assume that you created your storage account using the Resource Manager deployment model, and not the Classic deployment model.
To configure file uploads from your devices, you need the connection string for an Azure storage account. The storage account must be in the same subscription as your IoT hub. 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}
Make a note of the connectionString value. You need it in the following steps.
You can either use an existing blob container for your file uploads or create a new one:
-
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}"
-
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}"
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:
-
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 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: This setting is the time-to-live of the SAS URIs returned to the device by IoT Hub. Set to one hour by default.
-
File notification settings default TTL: The time-to-live of a file upload notification before it is expired. Set to one day by default.
-
File notification maximum delivery count: The number of times the IoT Hub attempts to deliver a file upload notification. Set to 10 by default.
Use the following Azure CLI commands to configure the file upload settings on your IoT hub:
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
You can review the file upload configuration on your IoT hub using the following command:
az iot hub show --name {your iot hub name}
For more information about the file upload capabilities of IoT Hub, see Upload files from a device.
Follow these links to learn more about managing Azure IoT Hub:
To further explore the capabilities of IoT Hub, see: