Search in sources :

Example 1 with BlobClientBuilder

use of com.azure.storage.blob.BlobClientBuilder in project azure-iot-sdk-java by Azure.

the class FileUploadTask method run.

@Override
public void run() {
    Thread.currentThread().setName(THREAD_NAME);
    FileUploadSasUriResponse sasUriResponse;
    try {
        sasUriResponse = getFileUploadSasUri(new FileUploadSasUriRequest(this.blobName));
    } catch (IOException | IllegalArgumentException e) {
        log.error("File upload failed to get a SAS URI from Iot Hub", e);
        userCallback.execute(IotHubStatusCode.ERROR, userCallbackContext);
        return;
    }
    FileUploadCompletionNotification fileUploadCompletionNotification = new FileUploadCompletionNotification(sasUriResponse.getCorrelationId(), false, -1, "Failed to upload to storage.");
    try {
        BlobClient blobClient = new BlobClientBuilder().endpoint(sasUriResponse.getBlobUri().toString()).buildClient();
        blobClient.upload(inputStream, streamLength);
        fileUploadCompletionNotification = new FileUploadCompletionNotification(sasUriResponse.getCorrelationId(), true, 0, "Succeed to upload to storage.");
    } catch (Exception e) {
        log.error("File upload failed to upload the stream to the blob", e);
    } finally {
        try {
            sendNotification(fileUploadCompletionNotification);
        } catch (IOException e) {
            log.error("Failed to send file upload status", e);
        }
    }
    if (fileUploadCompletionNotification.getSuccess()) {
        userCallback.execute(IotHubStatusCode.OK, userCallbackContext);
    } else {
        userCallback.execute(IotHubStatusCode.ERROR, userCallbackContext);
    }
}
Also used : BlobClient(com.azure.storage.blob.BlobClient) FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) IOException(java.io.IOException) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) BlobClientBuilder(com.azure.storage.blob.BlobClientBuilder) IotHubServiceException(com.microsoft.azure.sdk.iot.device.exceptions.IotHubServiceException) IOException(java.io.IOException) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)

Example 2 with BlobClientBuilder

use of com.azure.storage.blob.BlobClientBuilder in project azure-iot-sdk-java by Azure.

the class FileUploadTests method uploadToBlobAsyncSingleFileGranular.

@Test(timeout = MAX_MILLISECS_TIMEOUT_KILL_TEST)
public void uploadToBlobAsyncSingleFileGranular() throws URISyntaxException, IOException, InterruptedException, IotHubException, GeneralSecurityException {
    // arrange
    DeviceClient deviceClient = setUpDeviceClient(testInstance.protocol);
    // act
    FileUploadSasUriResponse sasUriResponse = deviceClient.getFileUploadSasUri(new FileUploadSasUriRequest(testInstance.fileUploadState[0].blobName));
    BlockBlobClient blockBlobClient = new BlobClientBuilder().endpoint(sasUriResponse.getBlobUri().toString()).buildClient().getBlockBlobClient();
    blockBlobClient.upload(testInstance.fileUploadState[0].fileInputStream, testInstance.fileUploadState[0].fileLength);
    FileUploadCompletionNotification fileUploadCompletionNotification = new FileUploadCompletionNotification();
    fileUploadCompletionNotification.setCorrelationId(sasUriResponse.getCorrelationId());
    fileUploadCompletionNotification.setStatusCode(0);
    fileUploadCompletionNotification.setSuccess(true);
    fileUploadCompletionNotification.setStatusDescription("Succeed to upload to storage.");
    deviceClient.completeFileUpload(fileUploadCompletionNotification);
    // assert
    assertEquals(buildExceptionMessage("File upload status should be SUCCESS but was " + testInstance.fileUploadState[0].fileUploadStatus, deviceClient), SUCCESS, testInstance.fileUploadState[0].fileUploadStatus);
    tearDownDeviceClient(deviceClient);
}
Also used : BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) BlobClientBuilder(com.azure.storage.blob.BlobClientBuilder) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) Test(org.junit.Test)

Example 3 with BlobClientBuilder

use of com.azure.storage.blob.BlobClientBuilder 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 4 with BlobClientBuilder

use of com.azure.storage.blob.BlobClientBuilder 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

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 FileUploadSasUriResponse (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse)4 BlobClient (com.azure.storage.blob.BlobClient)3 IOException (java.io.IOException)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 IotHubServiceException (com.microsoft.azure.sdk.iot.device.exceptions.IotHubServiceException)1 Scanner (java.util.Scanner)1 Test (org.junit.Test)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