title | description | author | manager | ms.author | ms.service | services | ms.devlang | ms.topic | ms.date | ms.custom | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Upload files from devices to Azure IoT Hub with Java | Microsoft Docs |
How to upload files from a device to the cloud using Azure IoT device SDK for Java. Uploaded files are stored in an Azure storage blob container. |
wesmc7777 |
philmea |
wesmc |
iot-hub |
iot-hub |
java |
conceptual |
06/28/2017 |
|
[!INCLUDE iot-hub-file-upload-language-selector]
This tutorial builds on the code in the Send cloud-to-device messages with IoT Hub tutorial to show you how to use the file upload capabilities of IoT Hub to upload a file to Azure blob storage. The tutorial shows you how to:
-
Securely provide a device with an Azure blob URI for uploading a file.
-
Use the IoT Hub file upload notifications to trigger processing the file in your app back end.
The Send telemetry from a device to an IoT hub quickstart and Send cloud-to-device messages with IoT Hub tutorial show the basic device-to-cloud and cloud-to-device messaging functionality of IoT Hub. The Configure message routing with IoT Hub tutorial describes a way to reliably store device-to-cloud messages in Azure blob storage. However, in some scenarios you cannot easily map the data your devices send into the relatively small device-to-cloud messages that IoT Hub accepts. For example:
- Large files that contain images
- Videos
- Vibration data sampled at high frequency
- Some form of preprocessed data.
These files are typically batch processed in the cloud using tools such as Azure Data Factory or the Hadoop stack. When you need to upland files from a device, you can still use the security and reliability of IoT Hub.
At the end of this tutorial you run two Java console apps:
-
simulated-device, a modified version of the app created in the [Send cloud-to-device messages with IoT Hub] tutorial. This app uploads a file to storage using a SAS URI provided by your IoT hub.
-
read-file-upload-notification, which receives file upload notifications from your IoT hub.
Note
IoT Hub supports many device platforms and languages (including C, .NET, and Javascript) through Azure IoT device SDKs. Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure IoT Hub.
[!INCLUDE iot-hub-include-x509-ca-signed-file-upload-support-note]
-
Java SE Development Kit 8. Make sure you select Java 8 under Long-term support to get to downloads for JDK 8.
-
An active Azure account. (If you don't have an account, you can create a free account in just a couple of minutes.)
-
Make sure that port 8883 is open in your firewall. The device sample in this article uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see Connecting to IoT Hub (MQTT).
[!INCLUDE iot-hub-associate-storage]
In this section, you modify the device app you created in Send cloud-to-device messages with IoT Hub to upload a file to IoT hub.
-
Copy an image file to the
simulated-device
folder and rename itmyimage.png
. -
Using a text editor, open the
simulated-device\src\main\java\com\mycompany\app\App.java
file. -
Add the variable declaration to the App class:
private static String fileName = "myimage.png";
-
To process file upload status callback messages, add the following nested class to the App class:
// Define a callback method to print status codes from IoT Hub. protected static class FileUploadStatusCallBack implements IotHubEventCallback { public void execute(IotHubStatusCode status, Object context) { System.out.println("IoT Hub responded to file upload for " + fileName + " operation with status " + status.name()); } }
-
To upload images to IoT Hub, add the following method to the App class to upload images to IoT Hub:
// Use IoT Hub to upload a file asynchronously to Azure blob storage. private static void uploadFile(String fullFileName) throws FileNotFoundException, IOException { File file = new File(fullFileName); InputStream inputStream = new FileInputStream(file); long streamLength = file.length(); client.uploadToBlobAsync(fileName, inputStream, streamLength, new FileUploadStatusCallBack(), null); }
-
Modify the main method to call the uploadFile method as shown in the following snippet:
client.open(); try { // Get the filename and start the upload. String fullFileName = System.getProperty("user.dir") + File.separator + fileName; uploadFile(fullFileName); System.out.println("File upload started with success"); } catch (Exception e) { System.out.println("Exception uploading file: " + e.getCause() + " \nERROR: " + e.getMessage()); } MessageSender sender = new MessageSender();
-
Use the following command to build the simulated-device app and check for errors:
mvn clean package -DskipTests
In this article you create a backend service to receive file upload notification messages from the IoT hub you created in Send telemetry from a device to an IoT hub. To receive file upload notification messages, your service needs the service connect permission. By default, every IoT Hub is created with a shared access policy named service that grants this permission.
[!INCLUDE iot-hub-include-find-service-connection-string]
In this section, you create a Java console app that receives file upload notification messages from IoT Hub.
-
Create a Maven project called read-file-upload-notification using the following command at your command prompt. Note this command is a single, long command:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=read-file-upload-notification -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
-
At your command prompt, navigate to the new
read-file-upload-notification
folder. -
Using a text editor, open the
pom.xml
file in theread-file-upload-notification
folder and add the following dependency to the dependencies node. Adding the dependency enables you to use the iothub-java-service-client package in your application to communicate with your IoT hub service:<dependency> <groupId>com.microsoft.azure.sdk.iot</groupId> <artifactId>iot-service-client</artifactId> <version>1.7.23</version> </dependency>
[!NOTE] You can check for the latest version of iot-service-client using Maven search.
-
Save and close the
pom.xml
file. -
Using a text editor, open the
read-file-upload-notification\src\main\java\com\mycompany\app\App.java
file. -
Add the following import statements to the file:
import com.microsoft.azure.sdk.iot.service.*; import java.io.IOException; import java.net.URISyntaxException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
-
Add the following class-level variables to the App class. Replace the
{Your IoT Hub connection string}
placeholder value with the IoT hub connection string that you copied previously in Get the IoT hub connection string:private static final String connectionString = "{Your IoT Hub connection string}"; private static final IotHubServiceClientProtocol protocol = IotHubServiceClientProtocol.AMQPS; private static FileUploadNotificationReceiver fileUploadNotificationReceiver = null;
-
To print information about the file upload to the console, add the following nested class to the App class:
// Create a thread to receive file upload notifications. private static class ShowFileUploadNotifications implements Runnable { public void run() { try { while (true) { System.out.println("Receive file upload notifications..."); FileUploadNotification fileUploadNotification = fileUploadNotificationReceiver.receive(); if (fileUploadNotification != null) { System.out.println("File Upload notification received"); System.out.println("Device Id : " + fileUploadNotification.getDeviceId()); System.out.println("Blob Uri: " + fileUploadNotification.getBlobUri()); System.out.println("Blob Name: " + fileUploadNotification.getBlobName()); System.out.println("Last Updated : " + fileUploadNotification.getLastUpdatedTimeDate()); System.out.println("Blob Size (Bytes): " + fileUploadNotification.getBlobSizeInBytes()); System.out.println("Enqueued Time: " + fileUploadNotification.getEnqueuedTimeUtcDate()); } } } catch (Exception ex) { System.out.println("Exception reading reported properties: " + ex.getMessage()); } } }
-
To start the thread that listens for file upload notifications, add the following code to the main method:
public static void main(String[] args) throws IOException, URISyntaxException, Exception { ServiceClient serviceClient = ServiceClient.createFromConnectionString(connectionString, protocol); if (serviceClient != null) { serviceClient.open(); // Get a file upload notification receiver from the ServiceClient. fileUploadNotificationReceiver = serviceClient.getFileUploadNotificationReceiver(); fileUploadNotificationReceiver.open(); // Start the thread to receive file upload notifications. ShowFileUploadNotifications showFileUploadNotifications = new ShowFileUploadNotifications(); ExecutorService executor = Executors.newFixedThreadPool(1); executor.execute(showFileUploadNotifications); System.out.println("Press ENTER to exit."); System.in.read(); executor.shutdownNow(); System.out.println("Shutting down sample..."); fileUploadNotificationReceiver.close(); serviceClient.close(); } }
-
Save and close the
read-file-upload-notification\src\main\java\com\mycompany\app\App.java
file. -
Use the following command to build the read-file-upload-notification app and check for errors:
mvn clean package -DskipTests
Now you are ready to run the applications.
At a command prompt in the read-file-upload-notification
folder, run the following command:
mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
At a command prompt in the simulated-device
folder, run the following command:
mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
The following screenshot shows the output from the simulated-device app:
The following screenshot shows the output from the read-file-upload-notification app:
You can use the portal to view the uploaded file in the storage container you configured:
In this tutorial, you learned how to use the file upload capabilities of IoT Hub to simplify file uploads from devices. You can continue to explore IoT hub features and scenarios with the following articles:
To further explore the capabilities of IoT Hub, see: