Search in sources :

Example 26 with CloudBlobClient

use of com.microsoft.azure.storage.blob.CloudBlobClient in project cloudbreak by hortonworks.

the class AzureClient method getBlobContainer.

public CloudBlobContainer getBlobContainer(String resourceGroup, String storageName, String containerName) {
    LOGGER.debug("get blob container: RG={}, storageName={}, containerName={}", resourceGroup, storageName, containerName);
    List<StorageAccountKey> keys = getStorageAccountKeys(resourceGroup, storageName);
    String storageConnectionString = String.format("DefaultEndpointsProtocol=http;AccountName=%s;AccountKey=%s", storageName, keys.get(0).value());
    try {
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
        return blobClient.getContainerReference(containerName);
    } catch (URISyntaxException e) {
        throw new CloudConnectorException("can't get blob container, URI is not valid", e);
    } catch (InvalidKeyException e) {
        throw new CloudConnectorException("can't get blob container, credentials in the connection string contain an invalid key", e);
    } catch (StorageException e) {
        throw new CloudConnectorException("can't get blob container, storage service error occurred", e);
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) StorageAccountKey(com.microsoft.azure.management.storage.StorageAccountKey) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) URISyntaxException(java.net.URISyntaxException) InvalidKeyException(java.security.InvalidKeyException) StorageException(com.microsoft.azure.storage.StorageException)

Example 27 with CloudBlobClient

use of com.microsoft.azure.storage.blob.CloudBlobClient in project cloudbreak by hortonworks.

the class FilesystemUtil method createWasbContainer.

static void createWasbContainer(Map<String, String> cloudProviderParams, String accountName, String containerName) {
    try {
        String storageConnectionString = "DefaultEndpointsProtocol=http;" + "AccountName=" + accountName + ";AccountKey=" + cloudProviderParams.get("accountKeyWasb");
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        container.createIfNotExists();
    } catch (URISyntaxException e) {
        LOGGER.error("Error during creating the Wasb container, wrong URI syntax ", e);
    } catch (InvalidKeyException e) {
        LOGGER.error("Error during creating the Wasb container, invalid key ", e);
    } catch (StorageException e) {
        LOGGER.error("Error during creating the Wasb container, problem with storage ", e);
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) URISyntaxException(java.net.URISyntaxException) InvalidKeyException(java.security.InvalidKeyException) StorageException(com.microsoft.azure.storage.StorageException)

Example 28 with CloudBlobClient

use of com.microsoft.azure.storage.blob.CloudBlobClient in project cloudbreak by hortonworks.

the class FilesystemUtil method deleteWasbContainer.

private static void deleteWasbContainer(Map<String, String> cloudProviderParams, String accountName, String containerName) {
    try {
        String storageConnectionString = "DefaultEndpointsProtocol=http;" + "AccountName=" + accountName + ";AccountKey=" + cloudProviderParams.get("accountKeyWasb");
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        container.deleteIfExists();
    } catch (URISyntaxException e) {
        LOGGER.error("Error during deleting the Wasb container, wrong URI syntax ", e);
    } catch (InvalidKeyException e) {
        LOGGER.error("Error during deleting the Wasb container, invalid key ", e);
    } catch (StorageException e) {
        LOGGER.error("Error during deleting the Wasb container, problem with storage ", e);
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) URISyntaxException(java.net.URISyntaxException) InvalidKeyException(java.security.InvalidKeyException) StorageException(com.microsoft.azure.storage.StorageException)

Example 29 with CloudBlobClient

use of com.microsoft.azure.storage.blob.CloudBlobClient in project azure-tools-for-java by Microsoft.

the class JxBrowserUtil method downloadFromAzure.

/**
 * Download file from Azure and save to target path specified. Return true if download succeeds
 * @param filePathName
 * @return
 */
private static Boolean downloadFromAzure(@NotNull Path filePathName) {
    boolean result = true;
    try {
        URI cloudBlobUri = new URI(AZURE_BLOB_URI);
        CloudBlobClient serviceClient = new CloudBlobClient(cloudBlobUri);
        CloudBlobContainer container = serviceClient.getContainerReference("libcontainer1");
        CloudBlockBlob blob = container.getBlockBlobReference(filePathName.getFileName().toString());
        File downloadingFile = filePathName.toFile();
        blob.downloadToFile(downloadingFile.getAbsolutePath());
    } catch (Exception e) {
        result = false;
        log.warning("Fail to download file from Azure: " + e.getMessage());
    }
    return result;
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) URI(java.net.URI) File(java.io.File) IOException(java.io.IOException)

Example 30 with CloudBlobClient

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

the class DeviceManagerExportSample method main.

public static void main(String[] args) throws Exception {
    System.out.println("Starting export sample...");
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(SampleUtils.storageConnectionString);
    CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(DeviceManagerExportSample.sampleContainerName);
    container.createIfNotExists();
    String containerSasUri = SampleUtils.getContainerSasUri(container);
    RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
    JobProperties exportJob = registryManager.exportDevices(containerSasUri, excludeKeys);
    while (true) {
        exportJob = registryManager.getJob(exportJob.getJobId());
        if (exportJob.getStatus() == JobProperties.JobStatus.COMPLETED) {
            break;
        }
        Thread.sleep(500);
    }
    for (ListBlobItem blobItem : container.listBlobs()) {
        if (blobItem instanceof CloudBlob) {
            CloudBlob blob = (CloudBlob) blobItem;
            blob.download(new FileOutputStream(SampleUtils.exportFileLocation + blob.getName()));
        }
    }
    registryManager.close();
    System.out.println("Export job completed. Results are in " + SampleUtils.exportFileLocation);
}
Also used : CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) FileOutputStream(java.io.FileOutputStream) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer)

Aggregations

CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)71 CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)40 CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)19 StorageException (com.microsoft.azure.storage.StorageException)19 URISyntaxException (java.net.URISyntaxException)17 CloudBlockBlob (com.microsoft.azure.storage.blob.CloudBlockBlob)16 ListBlobItem (com.microsoft.azure.storage.blob.ListBlobItem)10 Supplier (java.util.function.Supplier)9 CloudBlob (com.microsoft.azure.storage.blob.CloudBlob)8 URI (java.net.URI)8 InvalidKeyException (java.security.InvalidKeyException)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)6 TypeLiteral (com.google.inject.TypeLiteral)5 BlobListingDetails (com.microsoft.azure.storage.blob.BlobListingDetails)5 IOException (java.io.IOException)5 BlobProperties (com.microsoft.azure.storage.blob.BlobProperties)4 HashMap (java.util.HashMap)4 Settings (org.elasticsearch.common.settings.Settings)4