Search in sources :

Example 6 with FileUploadSasUriResponse

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse in project azure-iot-sdk-java by Azure.

the class FileUploadSasUriResponseTest method constructor_json_succeed.

@Test
public void constructor_json_succeed() {
    // arrange
    String validJson = createJson(VALID_HOST_NAME, VALID_CONTAINER_NAME, VALID_CORRELATION_ID, VALID_BLOB_NAME, VALID_SAS_TOKEN);
    // act
    FileUploadSasUriResponse fileUploadSasUriResponse = new FileUploadSasUriResponse(validJson);
    // assert
    assertFileUploadResponse(fileUploadSasUriResponse, VALID_HOST_NAME, VALID_CONTAINER_NAME, VALID_CORRELATION_ID, VALID_BLOB_NAME, VALID_SAS_TOKEN);
}
Also used : FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) Test(org.junit.Test)

Example 7 with FileUploadSasUriResponse

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse in project azure-iot-sdk-java by Azure.

the class FileUploadSample method uploadFile.

private static void uploadFile(DeviceClient client, String baseDirectory, String relativeFileName) throws IOException, URISyntaxException {
    File file = new File(baseDirectory, relativeFileName);
    try {
        if (relativeFileName.startsWith("\\")) {
            relativeFileName = relativeFileName.substring(1);
        }
        int index = fileNameList.size();
        fileNameList.add(relativeFileName);
        System.out.println("Getting SAS URI for upload file " + fileNameList.get(index));
        FileUploadSasUriResponse sasUriResponse = client.getFileUploadSasUri(new FileUploadSasUriRequest(file.getName()));
        try {
            System.out.println("Uploading file " + fileNameList.get(index) + " with the retrieved SAS URI...");
            BlobClient blobClient = new BlobClientBuilder().endpoint(sasUriResponse.getBlobUri().toString()).buildClient();
            blobClient.uploadFromFile(file.getPath());
        } catch (Exception e) {
            // Note that this is done even when the file upload fails. IoT Hub has a fixed number of SAS URIs allowed active
            // at any given time. Once you are done with the file upload, you should free your SAS URI so that other
            // SAS URIs can be generated. If a SAS URI is not freed through this API, then it will free itself eventually
            // based on how long SAS URIs are configured to live on your IoT Hub.
            FileUploadCompletionNotification completionNotification = new FileUploadCompletionNotification(sasUriResponse.getCorrelationId(), false);
            client.completeFileUpload(completionNotification);
            System.out.println("Failed to upload file " + fileNameList.get(index));
            e.printStackTrace();
            return;
        }
        FileUploadCompletionNotification completionNotification = new FileUploadCompletionNotification(sasUriResponse.getCorrelationId(), true);
        client.completeFileUpload(completionNotification);
        System.out.println("Finished file upload for file " + fileNameList.get(index));
    } finally {
        client.closeNow();
    }
}
Also used : BlobClient(com.azure.storage.blob.BlobClient) FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) BlobClientBuilder(com.azure.storage.blob.BlobClientBuilder) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) File(java.io.File) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)

Example 8 with FileUploadSasUriResponse

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse in project azure-iot-sdk-java by Azure.

the class FileUploadSimpleSample method main.

/**
 * Upload a single file to blobs using IoT Hub.
 *
 * @param args
 * args[0] = IoT Hub connection string
 * args[1] = File to upload
 */
public static void main(String[] args) throws IOException, URISyntaxException {
    String connString;
    String fullFileName;
    System.out.println("Starting...");
    System.out.println("Beginning setup.");
    if (args.length == 2) {
        connString = args[0];
        fullFileName = args[1];
    } else {
        System.out.format("Expected the following argument but received: %d.\n" + "The program should be called with the following args: \n" + "[Device connection string] - String containing Hostname, Device Id & Device Key in the following formats: HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>\n" + "[File to upload] - String containing the full path for the file to upload.\n", args.length);
        return;
    }
    // File upload will always use HTTPS, DeviceClient will use this protocol only
    // for the other services like Telemetry, Device Method and Device Twin.
    IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;
    System.out.println("Successfully read input parameters.");
    DeviceClient client = new DeviceClient(connString, protocol);
    System.out.println("Successfully created an IoT Hub client.");
    try {
        File file = new File(fullFileName);
        if (file.isDirectory()) {
            throw new IllegalArgumentException(fullFileName + " is a directory, please provide a single file name, or use the FileUploadSample to upload directories.");
        }
        System.out.println("Retrieving SAS URI from IoT Hub...");
        FileUploadSasUriResponse sasUriResponse = client.getFileUploadSasUri(new FileUploadSasUriRequest(file.getName()));
        System.out.println("Successfully got SAS URI from IoT Hub");
        System.out.println("Correlation Id: " + sasUriResponse.getCorrelationId());
        System.out.println("Container name: " + sasUriResponse.getContainerName());
        System.out.println("Blob name: " + sasUriResponse.getBlobName());
        System.out.println("Blob Uri: " + sasUriResponse.getBlobUri());
        System.out.println("Using the Azure Storage SDK to upload file to Azure Storage...");
        try {
            BlobClient blobClient = new BlobClientBuilder().endpoint(sasUriResponse.getBlobUri().toString()).buildClient();
            blobClient.uploadFromFile(fullFileName);
        } catch (Exception e) {
            System.out.println("Exception encountered while uploading file to blob: " + e.getMessage());
            System.out.println("Failed to upload file to Azure Storage.");
            System.out.println("Notifying IoT Hub that the SAS URI can be freed and that the file upload failed.");
            // Note that this is done even when the file upload fails. IoT Hub has a fixed number of SAS URIs allowed active
            // at any given time. Once you are done with the file upload, you should free your SAS URI so that other
            // SAS URIs can be generated. If a SAS URI is not freed through this API, then it will free itself eventually
            // based on how long SAS URIs are configured to live on your IoT Hub.
            FileUploadCompletionNotification completionNotification = new FileUploadCompletionNotification(sasUriResponse.getCorrelationId(), false);
            client.completeFileUpload(completionNotification);
            System.out.println("Notified IoT Hub that the SAS URI can be freed and that the file upload was a failure.");
            client.closeNow();
            return;
        }
        System.out.println("Successfully uploaded file to Azure Storage.");
        System.out.println("Notifying IoT Hub that the SAS URI can be freed and that the file upload was a success.");
        FileUploadCompletionNotification completionNotification = new FileUploadCompletionNotification(sasUriResponse.getCorrelationId(), true);
        client.completeFileUpload(completionNotification);
        System.out.println("Successfully notified IoT Hub that the SAS URI can be freed, and that the file upload was a success");
    } catch (Exception e) {
        System.out.println("On exception, shutting down \n" + " Cause: " + e.getCause() + " \nERROR: " + e.getMessage());
        System.out.println("Shutting down...");
        client.closeNow();
    }
    System.out.println("Press any key to exit...");
    Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name());
    scanner.nextLine();
    System.out.println("Shutting down...");
    client.closeNow();
}
Also used : Scanner(java.util.Scanner) BlobClient(com.azure.storage.blob.BlobClient) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubClientProtocol(com.microsoft.azure.sdk.iot.device.IotHubClientProtocol) FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) BlobClientBuilder(com.azure.storage.blob.BlobClientBuilder) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) File(java.io.File) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)

Aggregations

FileUploadSasUriResponse (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse)8 BlobClientBuilder (com.azure.storage.blob.BlobClientBuilder)4 FileUploadCompletionNotification (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification)4 FileUploadSasUriRequest (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 BlobClient (com.azure.storage.blob.BlobClient)3 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 BlockBlobClient (com.azure.storage.blob.specialized.BlockBlobClient)1 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 IotHubClientProtocol (com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)1 ResponseMessage (com.microsoft.azure.sdk.iot.device.ResponseMessage)1 IotHubServiceException (com.microsoft.azure.sdk.iot.device.exceptions.IotHubServiceException)1 FileUploadTask (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask)1 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)1 Scanner (java.util.Scanner)1 NonStrictExpectations (mockit.NonStrictExpectations)1 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)1 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)1