Search in sources :

Example 6 with CloudStorageAccount

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

the class DeviceManagerImportSample method main.

public static void main(String[] args) throws Exception {
    System.out.println("Starting import sample...");
    // Creating Azure storage container and getting its URI
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(SampleUtils.storageConnectionString);
    CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(DeviceManagerImportSample.importContainerName);
    container.createIfNotExists();
    String containerSasUri = SampleUtils.getContainerSasUri(container);
    // Creating the list of devices to be submitted for import
    StringBuilder devicesToImport = new StringBuilder();
    for (int i = 0; i < 1; i++) {
        String deviceId = UUID.randomUUID().toString();
        Device device = Device.createFromId(deviceId, null, null);
        AuthenticationMechanism authentication = new AuthenticationMechanism(device.getSymmetricKey());
        ExportImportDevice deviceToAdd = new ExportImportDevice();
        deviceToAdd.setId(deviceId);
        deviceToAdd.setAuthentication(authentication);
        deviceToAdd.setStatus(DeviceStatus.Enabled);
        deviceToAdd.setImportMode(ImportMode.CreateOrUpdate);
        devicesToImport.append(gson.toJson(deviceToAdd));
        if (i < numberOfDevice - 1) {
            devicesToImport.append("\r\n");
        }
    }
    byte[] blobToImport = devicesToImport.toString().getBytes(StandardCharsets.UTF_8);
    // Creating the Azure storage blob and uploading the serialized string of devices
    System.out.println("Uploading " + blobToImport.length + " bytes into Azure storage.");
    InputStream stream = new ByteArrayInputStream(blobToImport);
    CloudBlockBlob importBlob = container.getBlockBlobReference(DeviceManagerImportSample.importBlobName);
    importBlob.deleteIfExists();
    importBlob.upload(stream, blobToImport.length);
    // Starting the import job
    RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
    JobProperties importJob = registryManager.importDevices(containerSasUri, containerSasUri);
    // Waiting for the import job to complete
    while (true) {
        importJob = registryManager.getJob(importJob.getJobId());
        if (importJob.getStatus() == JobProperties.JobStatus.COMPLETED || importJob.getStatus() == JobProperties.JobStatus.FAILED) {
            break;
        }
        Thread.sleep(500);
    }
    // Checking the result of the import job
    if (importJob.getStatus() == JobProperties.JobStatus.COMPLETED) {
        System.out.println("Import job completed. The new devices are now added to the hub.");
    } else {
        System.out.println("Import job failed. Failure reason: " + importJob.getFailureReason());
    }
    //Cleaning up the blob
    for (ListBlobItem blobItem : container.listBlobs()) {
        if (blobItem instanceof CloudBlob) {
            CloudBlob blob = (CloudBlockBlob) blobItem;
            blob.deleteIfExists();
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 7 with CloudStorageAccount

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

the class RegistryManagerIT method setUp.

@BeforeClass
public static void setUp() throws URISyntaxException, InvalidKeyException, StorageException {
    Map<String, String> env = System.getenv();
    for (String envName : env.keySet()) {
        if (envName.equals(iotHubonnectionStringEnvVarName.toString())) {
            iotHubConnectionString = env.get(envName);
        } else if (envName.equals(storageAccountConnectionStringEnvVarName.toString())) {
            storageAccountConnectionString = env.get(envName);
        }
    }
    String uuid = UUID.randomUUID().toString();
    deviceId = deviceId.concat("-" + uuid);
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageAccountConnectionString);
    blobClient = storageAccount.createCloudBlobClient();
    // Creating the export storage container and getting its URI
    String exportContainerName = "exportcontainersample-" + uuid;
    exportContainer = blobClient.getContainerReference(exportContainerName);
    exportContainer.createIfNotExists();
    // Creating the import storage container and getting its URI
    String importContainerName = "importcontainersample-" + uuid;
    importContainer = blobClient.getContainerReference(importContainerName);
    importContainer.createIfNotExists();
}
Also used : CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) BeforeClass(org.junit.BeforeClass)

Example 8 with CloudStorageAccount

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

the class AzureNativeFileSystemStore method connectUsingCredentials.

private void connectUsingCredentials(String accountName, StorageCredentials credentials, String containerName) throws URISyntaxException, StorageException, AzureException {
    URI blobEndPoint;
    if (isStorageEmulatorAccount(accountName)) {
        isStorageEmulator = true;
        CloudStorageAccount account = CloudStorageAccount.getDevelopmentStorageAccount();
        storageInteractionLayer.createBlobClient(account);
    } else {
        blobEndPoint = new URI(getHTTPScheme() + "://" + accountName);
        storageInteractionLayer.createBlobClient(blobEndPoint, credentials);
    }
    suppressRetryPolicyInClientIfNeeded();
    // Capture the container reference for debugging purposes.
    container = storageInteractionLayer.getContainerReference(containerName);
    rootDirectory = container.getDirectoryReference("");
    // Can only create container if using account key credentials
    canCreateOrModifyContainer = credentials instanceof StorageCredentialsAccountAndKey;
    // Configure Azure storage session.
    configureAzureStorageSession();
}
Also used : CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) StorageCredentialsAccountAndKey(com.microsoft.azure.storage.StorageCredentialsAccountAndKey) URI(java.net.URI)

Example 9 with CloudStorageAccount

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

the class LocalSASKeyGeneratorImpl method getRelativeBlobSASUri.

/**
   * Implementation for generation of Relative Path Blob SAS Uri.
   */
@Override
public URI getRelativeBlobSASUri(String accountName, String container, String relativePath) throws SASKeyGenerationException {
    CloudBlobContainer sc = null;
    CloudBlobClient client = null;
    try {
        CloudStorageAccount account = getSASKeyBasedStorageAccountInstance(accountName);
        client = account.createCloudBlobClient();
        sc = client.getContainerReference(container);
    } catch (URISyntaxException uriSyntaxEx) {
        throw new SASKeyGenerationException("Encountered URISyntaxException " + "while getting container references for container " + container + " inside storage account : " + accountName, uriSyntaxEx);
    } catch (StorageException stoEx) {
        throw new SASKeyGenerationException("Encountered StorageException while " + "getting  container references for container " + container + " inside storage account : " + accountName, stoEx);
    }
    CloudBlockBlob blob = null;
    try {
        blob = sc.getBlockBlobReference(relativePath);
    } catch (URISyntaxException uriSyntaxEx) {
        throw new SASKeyGenerationException("Encountered URISyntaxException while " + "getting Block Blob references for container " + container + " inside storage account : " + accountName, uriSyntaxEx);
    } catch (StorageException stoEx) {
        throw new SASKeyGenerationException("Encountered StorageException while " + "getting Block Blob references for container " + container + " inside storage account : " + accountName, stoEx);
    }
    try {
        return client.getCredentials().transformUri(blob.getUri());
    } catch (StorageException stoEx) {
        throw new SASKeyGenerationException("Encountered StorageException while " + "generating SAS key for Blob: " + relativePath + " inside " + "container : " + container + " in Storage Account : " + accountName, stoEx);
    } catch (URISyntaxException uriSyntaxEx) {
        throw new SASKeyGenerationException("Encountered URISyntaxException " + "while generating SAS key for Blob: " + relativePath + " inside " + "container: " + container + " in Storage Account : " + accountName, uriSyntaxEx);
    }
}
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) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) StorageException(com.microsoft.azure.storage.StorageException)

Example 10 with CloudStorageAccount

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

the class LocalSASKeyGeneratorImpl method getStorageAccountInstance.

/**
   * Helper method that creates CloudStorageAccount Instance using the
   * storage account key.
   * @param accountName Name of the storage account
   * @param accountKey Storage Account key
   * @return CloudStorageAccount instance for the storage account.
   * @throws SASKeyGenerationException
   */
private CloudStorageAccount getStorageAccountInstance(String accountName, String accountKey) throws SASKeyGenerationException {
    if (!storageAccountMap.containsKey(accountName)) {
        CloudStorageAccount account = null;
        try {
            account = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, accountKey));
        } catch (URISyntaxException uriSyntaxEx) {
            throw new SASKeyGenerationException("Encountered URISyntaxException " + "for account " + accountName, uriSyntaxEx);
        }
        storageAccountMap.put(accountName, account);
    }
    return storageAccountMap.get(accountName);
}
Also used : CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) URISyntaxException(java.net.URISyntaxException) StorageCredentialsAccountAndKey(com.microsoft.azure.storage.StorageCredentialsAccountAndKey)

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