Search in sources :

Example 46 with CloudBlobClient

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

the class AzureStorageServiceImpl method blobExists.

@Override
public boolean blobExists(String account, LocationMode mode, String container, String blob) throws URISyntaxException, StorageException {
    // Container name must be lower case.
    CloudBlobClient client = this.getSelectedClient(account, mode);
    CloudBlobContainer blobContainer = client.getContainerReference(container);
    if (blobContainer.exists()) {
        CloudBlockBlob azureBlob = blobContainer.getBlockBlobReference(blob);
        return SocketAccess.doPrivilegedException(azureBlob::exists);
    }
    return false;
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob)

Example 47 with CloudBlobClient

use of com.microsoft.azure.storage.blob.CloudBlobClient 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 48 with CloudBlobClient

use of com.microsoft.azure.storage.blob.CloudBlobClient 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 49 with CloudBlobClient

use of com.microsoft.azure.storage.blob.CloudBlobClient 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 50 with CloudBlobClient

use of com.microsoft.azure.storage.blob.CloudBlobClient 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)

Aggregations

CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)74 CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)42 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 URI (java.net.URI)10 Supplier (java.util.function.Supplier)9 CloudBlob (com.microsoft.azure.storage.blob.CloudBlob)8 InvalidKeyException (java.security.InvalidKeyException)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)6 TypeLiteral (com.google.inject.TypeLiteral)5 RetryExponentialRetry (com.microsoft.azure.storage.RetryExponentialRetry)5 StorageCredentials (com.microsoft.azure.storage.StorageCredentials)5 BlobListingDetails (com.microsoft.azure.storage.blob.BlobListingDetails)5 IOException (java.io.IOException)5 BlobProperties (com.microsoft.azure.storage.blob.BlobProperties)4