Search in sources :

Example 1 with CloudStorageAccount

use of com.microsoft.azure.storage.CloudStorageAccount in project hadoop by apache.

the class LocalSASKeyGeneratorImpl method getSASKeyBasedStorageAccountInstance.

/**
   * Helper method that creates a CloudStorageAccount instance based on
   *  SAS key for accountName
   *
   * @param accountName Storage Account Name
   * @return CloudStorageAccount instance created using SAS key for
   *   the Storage Account.
   * @throws SASKeyGenerationException
   */
private CloudStorageAccount getSASKeyBasedStorageAccountInstance(String accountName) throws SASKeyGenerationException {
    try {
        String accountNameWithoutDomain = getAccountNameWithoutDomain(accountName);
        CloudStorageAccount account = getStorageAccountInstance(accountNameWithoutDomain, AzureNativeFileSystemStore.getAccountKeyFromConfiguration(accountName, getConf()));
        return new CloudStorageAccount(new StorageCredentialsSharedAccessSignature(account.generateSharedAccessSignature(getDefaultAccountAccessPolicy())), false, account.getEndpointSuffix(), accountNameWithoutDomain);
    } catch (KeyProviderException keyProviderEx) {
        throw new SASKeyGenerationException("Encountered KeyProviderException" + " while retrieving Storage key from configuration for account " + accountName, keyProviderEx);
    } catch (InvalidKeyException invalidKeyEx) {
        throw new SASKeyGenerationException("Encoutered InvalidKeyException " + "while generating Account level SAS key for account" + accountName, invalidKeyEx);
    } catch (StorageException storeEx) {
        throw new SASKeyGenerationException("Encoutered StorageException while " + "generating Account level SAS key for account" + accountName, storeEx);
    } catch (URISyntaxException uriSyntaxEx) {
        throw new SASKeyGenerationException("Encountered URISyntaxException for" + " account " + accountName, uriSyntaxEx);
    }
}
Also used : StorageCredentialsSharedAccessSignature(com.microsoft.azure.storage.StorageCredentialsSharedAccessSignature) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) URISyntaxException(java.net.URISyntaxException) InvalidKeyException(java.security.InvalidKeyException) StorageException(com.microsoft.azure.storage.StorageException)

Example 2 with CloudStorageAccount

use of com.microsoft.azure.storage.CloudStorageAccount in project hadoop by apache.

the class LocalSASKeyGeneratorImpl method getContainerSASUri.

/**
   * Implementation to generate SAS Key for a container
   */
@Override
public URI getContainerSASUri(String accountName, String container) throws SASKeyGenerationException {
    try {
        CloudStorageAccount account = getSASKeyBasedStorageAccountInstance(accountName);
        CloudBlobClient client = account.createCloudBlobClient();
        return client.getCredentials().transformUri(client.getContainerReference(container).getUri());
    } catch (StorageException stoEx) {
        throw new SASKeyGenerationException("Encountered StorageException while" + " generating SAS Key for container " + container + " inside " + "storage account " + accountName, stoEx);
    } catch (URISyntaxException uriSyntaxEx) {
        throw new SASKeyGenerationException("Encountered URISyntaxException while" + " generating SAS Key for container " + container + " inside storage" + " account " + accountName, uriSyntaxEx);
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) URISyntaxException(java.net.URISyntaxException) StorageException(com.microsoft.azure.storage.StorageException)

Example 3 with CloudStorageAccount

use of com.microsoft.azure.storage.CloudStorageAccount in project elasticsearch by elastic.

the class AzureStorageServiceImpl method createClient.

void createClient(AzureStorageSettings azureStorageSettings) {
    try {
        logger.trace("creating new Azure storage client using account [{}], key [{}]", azureStorageSettings.getAccount(), azureStorageSettings.getKey());
        String storageConnectionString = "DefaultEndpointsProtocol=https;" + "AccountName=" + azureStorageSettings.getAccount() + ";" + "AccountKey=" + azureStorageSettings.getKey();
        // Retrieve storage account from connection-string.
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        // Create the blob client.
        CloudBlobClient client = storageAccount.createCloudBlobClient();
        // Register the client
        this.clients.put(azureStorageSettings.getAccount(), client);
    } catch (Exception e) {
        logger.error("can not create azure storage client: {}", e.getMessage());
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) URISyntaxException(java.net.URISyntaxException) StorageException(com.microsoft.azure.storage.StorageException) RepositoryException(org.elasticsearch.repositories.RepositoryException) PrivilegedActionException(java.security.PrivilegedActionException)

Example 4 with CloudStorageAccount

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

the class StorageAccoutUtils method getCloudStorageAccount.

private static CloudStorageAccount getCloudStorageAccount(String blobLink, String saKey) throws MalformedURLException, URISyntaxException, InvalidKeyException {
    if (blobLink == null || blobLink.isEmpty()) {
        throw new IllegalArgumentException("Invalid blob link, it's null or empty: " + blobLink);
    }
    if (saKey == null || saKey.isEmpty()) {
        throw new IllegalArgumentException("Invalid storage account key, it's null or empty: " + saKey);
    }
    // check the link is valic
    URI blobUri = new URL(blobLink).toURI();
    String host = blobUri.getHost();
    if (host == null) {
        throw new IllegalArgumentException("Invalid blobLink, can't find host: " + blobLink);
    }
    String storageAccountName = host.substring(0, host.indexOf("."));
    String storageConnectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s", storageAccountName, saKey);
    CloudStorageAccount cloudStorageAccount = CloudStorageAccount.parse(storageConnectionString);
    return cloudStorageAccount;
}
Also used : CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) URI(java.net.URI) URL(java.net.URL)

Example 5 with CloudStorageAccount

use of com.microsoft.azure.storage.CloudStorageAccount 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()));
        }
    }
    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

CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)15 CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)8 URISyntaxException (java.net.URISyntaxException)7 StorageException (com.microsoft.azure.storage.StorageException)6 CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)5 URI (java.net.URI)4 InvalidKeyException (java.security.InvalidKeyException)3 StorageCredentialsAccountAndKey (com.microsoft.azure.storage.StorageCredentialsAccountAndKey)2 BlobContainerPermissions (com.microsoft.azure.storage.blob.BlobContainerPermissions)2 CloudBlockBlob (com.microsoft.azure.storage.blob.CloudBlockBlob)2 InputStream (java.io.InputStream)2 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)1 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)1 StorageAccountKey (com.microsoft.azure.management.storage.StorageAccountKey)1 JobProperties (com.microsoft.azure.sdk.iot.service.JobProperties)1 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)1 StorageCredentialsSharedAccessSignature (com.microsoft.azure.storage.StorageCredentialsSharedAccessSignature)1 CloudBlob (com.microsoft.azure.storage.blob.CloudBlob)1 ListBlobItem (com.microsoft.azure.storage.blob.ListBlobItem)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1