Search in sources :

Example 1 with BlobClient

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

use of com.azure.storage.blob.BlobClient in project beam by apache.

the class AzureBlobStoreFileSystem method copy.

@VisibleForTesting
void copy(AzfsResourceId sourcePath, AzfsResourceId destinationPath) throws IOException {
    checkArgument(sourcePath.getBlob() != null && destinationPath.getBlob() != null, "This method is intended to copy file-like resources, not directories.");
    // get source blob client
    BlobClient srcBlobClient = client.get().getBlobContainerClient(sourcePath.getContainer()).getBlobClient(sourcePath.getBlob());
    if (!srcBlobClient.exists()) {
        throw new FileNotFoundException("The copy source does not exist.");
    }
    // get destination blob client
    BlobContainerClient destBlobContainerClient = client.get().getBlobContainerClient(destinationPath.getContainer());
    if (!destBlobContainerClient.exists()) {
        client.get().createBlobContainer(destinationPath.getContainer());
        LOG.info("Created a container called {}", destinationPath.getContainer());
    }
    BlobClient destBlobClient = destBlobContainerClient.getBlobClient(destinationPath.getBlob());
    destBlobClient.copyFromUrl(srcBlobClient.getBlobUrl() + generateSasToken());
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobClient(com.azure.storage.blob.BlobClient) FileNotFoundException(java.io.FileNotFoundException) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Example 3 with BlobClient

use of com.azure.storage.blob.BlobClient in project beam by apache.

the class AzureBlobStoreFileSystem method open.

@Override
protected ReadableByteChannel open(AzfsResourceId resourceId) throws IOException {
    BlobContainerClient containerClient = client.get().getBlobContainerClient(resourceId.getContainer());
    if (!containerClient.exists()) {
        throw new FileNotFoundException("The requested file doesn't exist.");
    }
    BlobClient blobClient = containerClient.getBlobClient(resourceId.getBlob());
    if (!blobClient.exists()) {
        throw new FileNotFoundException("The requested file doesn't exist.");
    }
    LOG.info("Creating a ReadableByteChannel for {}", resourceId);
    return new AzureReadableSeekableByteChannel(blobClient);
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobClient(com.azure.storage.blob.BlobClient) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with BlobClient

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

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

BlobClient (com.azure.storage.blob.BlobClient)8 IOException (java.io.IOException)6 FileNotFoundException (java.io.FileNotFoundException)5 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)4 BlobClientBuilder (com.azure.storage.blob.BlobClientBuilder)3 FileUploadCompletionNotification (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification)3 FileUploadSasUriRequest (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)3 FileUploadSasUriResponse (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse)3 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 BlobItem (com.azure.storage.blob.models.BlobItem)1 BlobProperties (com.azure.storage.blob.models.BlobProperties)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 OutputStream (java.io.OutputStream)1 Scanner (java.util.Scanner)1 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)1