Search in sources :

Example 11 with CloudStorageAccount

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

the class VirtualMachineScaleSetOperationsTests method canUpdateVirtualMachineScaleSetWithExtensionProtectedSettings.

@Test
public void canUpdateVirtualMachineScaleSetWithExtensionProtectedSettings() throws Exception {
    final String vmssName = generateRandomResourceName("vmss", 10);
    final String uname = "jvuser";
    final String password = "123OData!@#123";
    ResourceGroup resourceGroup = this.resourceManager.resourceGroups().define(RG_NAME).withRegion(REGION).create();
    StorageAccount storageAccount = this.storageManager.storageAccounts().define(generateRandomResourceName("stg", 15)).withRegion(REGION).withExistingResourceGroup(resourceGroup).create();
    List<StorageAccountKey> keys = storageAccount.getKeys();
    Assert.assertNotNull(keys);
    Assert.assertTrue(keys.size() > 0);
    String storageAccountKey = keys.get(0).value();
    final String storageConnectionString = String.format("DefaultEndpointsProtocol=http;AccountName=%s;AccountKey=%s", storageAccount.name(), storageAccountKey);
    // Get the script to upload
    //
    InputStream scriptFileAsStream = VirtualMachineScaleSetOperationsTests.class.getResourceAsStream("/install_apache.sh");
    // Get the size of the stream
    //
    int fileSize;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[256];
    int bytesRead;
    while ((bytesRead = scriptFileAsStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    fileSize = outputStream.size();
    outputStream.close();
    // Upload the script file as block blob
    //
    URI fileUri;
    if (IS_MOCKED) {
        fileUri = new URI("http://nonexisting.blob.core.windows.net/scripts/install_apache.sh");
    } else {
        CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient cloudBlobClient = account.createCloudBlobClient();
        CloudBlobContainer container = cloudBlobClient.getContainerReference("scripts");
        container.createIfNotExists();
        CloudBlockBlob blob = container.getBlockBlobReference("install_apache.sh");
        blob.upload(scriptFileAsStream, fileSize);
        fileUri = blob.getUri();
    }
    List<String> fileUris = new ArrayList<>();
    fileUris.add(fileUri.toString());
    Network network = this.networkManager.networks().define(generateRandomResourceName("vmssvnet", 15)).withRegion(REGION).withExistingResourceGroup(resourceGroup).withAddressSpace("10.0.0.0/28").withSubnet("subnet1", "10.0.0.0/28").create();
    VirtualMachineScaleSet virtualMachineScaleSet = this.computeManager.virtualMachineScaleSets().define(vmssName).withRegion(REGION).withExistingResourceGroup(resourceGroup).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0).withExistingPrimaryNetworkSubnet(network, "subnet1").withoutPrimaryInternetFacingLoadBalancer().withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(uname).withRootPassword(password).withUnmanagedDisks().withNewStorageAccount(generateRandomResourceName("stg", 15)).withExistingStorageAccount(storageAccount).defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", fileUris).withProtectedSetting("commandToExecute", "bash install_apache.sh").withProtectedSetting("storageAccountName", storageAccount.name()).withProtectedSetting("storageAccountKey", storageAccountKey).attach().create();
    // Validate extensions after create
    //
    Map<String, VirtualMachineScaleSetExtension> extensions = virtualMachineScaleSet.extensions();
    Assert.assertNotNull(extensions);
    Assert.assertEquals(1, extensions.size());
    Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
    VirtualMachineScaleSetExtension extension = extensions.get("CustomScriptForLinux");
    Assert.assertNotNull(extension.publicSettings());
    Assert.assertEquals(1, extension.publicSettings().size());
    Assert.assertNotNull(extension.publicSettingsAsJsonString());
    // Retrieve scale set
    VirtualMachineScaleSet scaleSet = this.computeManager.virtualMachineScaleSets().getById(virtualMachineScaleSet.id());
    // Validate extensions after get
    //
    extensions = virtualMachineScaleSet.extensions();
    Assert.assertNotNull(extensions);
    Assert.assertEquals(1, extensions.size());
    Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
    extension = extensions.get("CustomScriptForLinux");
    Assert.assertNotNull(extension.publicSettings());
    Assert.assertEquals(1, extension.publicSettings().size());
    Assert.assertNotNull(extension.publicSettingsAsJsonString());
    // Update VMSS capacity
    //
    int newCapacity = (int) (scaleSet.capacity() + 1);
    virtualMachineScaleSet.update().withCapacity(newCapacity).apply();
    // Validate extensions after update
    //
    extensions = virtualMachineScaleSet.extensions();
    Assert.assertNotNull(extensions);
    Assert.assertEquals(1, extensions.size());
    Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
    extension = extensions.get("CustomScriptForLinux");
    Assert.assertNotNull(extension.publicSettings());
    Assert.assertEquals(1, extension.publicSettings().size());
    Assert.assertNotNull(extension.publicSettingsAsJsonString());
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) InputStream(java.io.InputStream) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) URI(java.net.URI) StorageAccount(com.microsoft.azure.management.storage.StorageAccount) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) StorageAccountKey(com.microsoft.azure.management.storage.StorageAccountKey) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Test(org.junit.Test)

Example 12 with CloudStorageAccount

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

the class ManageWebAppStorageAccountConnection method setUpStorageAccount.

private static CloudBlobContainer setUpStorageAccount(String connectionString, String containerName) {
    try {
        CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
        // Create a blob service client
        CloudBlobClient blobClient = account.createCloudBlobClient();
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        container.createIfNotExists();
        BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
        // Include public access in the permissions object
        containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
        // Set the permissions on the container
        container.uploadPermissions(containerPermissions);
        return container;
    } catch (StorageException | URISyntaxException | InvalidKeyException e) {
        throw new RuntimeException(e);
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) BlobContainerPermissions(com.microsoft.azure.storage.blob.BlobContainerPermissions) URISyntaxException(java.net.URISyntaxException) InvalidKeyException(java.security.InvalidKeyException) StorageException(com.microsoft.azure.storage.StorageException)

Example 13 with CloudStorageAccount

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

the class ManageLinuxWebAppStorageAccountConnection method setUpStorageAccount.

private static CloudBlobContainer setUpStorageAccount(String connectionString, String containerName) {
    try {
        CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
        // Create a blob service client
        CloudBlobClient blobClient = account.createCloudBlobClient();
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        container.createIfNotExists();
        BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
        // Include public access in the permissions object
        containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
        // Set the permissions on the container
        container.uploadPermissions(containerPermissions);
        return container;
    } catch (StorageException | URISyntaxException | InvalidKeyException e) {
        throw new RuntimeException(e);
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) BlobContainerPermissions(com.microsoft.azure.storage.blob.BlobContainerPermissions) URISyntaxException(java.net.URISyntaxException) InvalidKeyException(java.security.InvalidKeyException) StorageException(com.microsoft.azure.storage.StorageException)

Example 14 with CloudStorageAccount

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

the class StorageAccoutUtils method getBlobSasUri.

public static String getBlobSasUri(String blobLink, String saKey) throws URISyntaxException, StorageException, InvalidKeyException, MalformedURLException {
    CloudStorageAccount cloudStorageAccount = getCloudStorageAccount(blobLink, saKey);
    // Create the blob client.
    CloudBlobClient blobClient = cloudStorageAccount.createCloudBlobClient();
    // Get container and blob name from the link
    String path = new URI(blobLink).getPath();
    if (path == null) {
        throw new IllegalArgumentException("Invalid blobLink: " + blobLink);
    }
    int containerNameEndIndex = path.indexOf("/", 1);
    String containerName = path.substring(1, containerNameEndIndex);
    if (containerName == null || containerName.isEmpty()) {
        throw new IllegalArgumentException("Invalid blobLink, can't find container name: " + blobLink);
    }
    String blobName = path.substring(path.indexOf("/", containerNameEndIndex) + 1);
    if (blobName == null || blobName.isEmpty()) {
        throw new IllegalArgumentException("Invalid blobLink, can't find blob name: " + blobLink);
    }
    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.getContainerReference(containerName);
    //CloudBlockBlob blob = container.getBlockBlobReference(blobName);
    SharedAccessBlobPolicy sharedAccessBlobPolicy = new SharedAccessBlobPolicy();
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.setTime(new Date());
    sharedAccessBlobPolicy.setSharedAccessStartTime(calendar.getTime());
    calendar.add(Calendar.HOUR, 23);
    sharedAccessBlobPolicy.setSharedAccessExpiryTime(calendar.getTime());
    sharedAccessBlobPolicy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ));
    BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
    container.uploadPermissions(containerPermissions);
    String signature = container.generateSharedAccessSignature(sharedAccessBlobPolicy, null);
    return blobLink + "?" + signature;
}
Also used : CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) URI(java.net.URI)

Example 15 with CloudStorageAccount

use of com.microsoft.azure.storage.CloudStorageAccount in project jackrabbit-oak by apache.

the class Utils method getBlobClient.

/**
     * Create CloudBlobClient from properties.
     *
     * @param connectionString connectionString to configure @link {@link CloudBlobClient}
     * @return {@link CloudBlobClient}
     */
public static CloudBlobClient getBlobClient(final String connectionString) throws URISyntaxException, InvalidKeyException {
    CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
    CloudBlobClient client = account.createCloudBlobClient();
    return client;
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount)

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