Search in sources :

Example 21 with CloudBlobContainer

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

the class TestContainerChecks method testContainerCreateAfterDoesNotExist.

@Test
public void testContainerCreateAfterDoesNotExist() throws Exception {
    testAccount = AzureBlobStorageTestAccount.create("", EnumSet.noneOf(CreateOptions.class));
    assumeNotNull(testAccount);
    CloudBlobContainer container = testAccount.getRealContainer();
    FileSystem fs = testAccount.getFileSystem();
    // Starting off with the container not there
    assertFalse(container.exists());
    // state to DoesNotExist
    try {
        assertNull(fs.listStatus(new Path("/")));
        assertTrue("Should've thrown.", false);
    } catch (FileNotFoundException ex) {
        assertTrue("Unexpected exception: " + ex, ex.getMessage().contains("does not exist."));
    }
    assertFalse(container.exists());
    // Create a container outside of the WASB FileSystem
    container.create();
    // Write should succeed
    assertTrue(fs.createNewFile(new Path("/foo")));
    assertTrue(container.exists());
}
Also used : Path(org.apache.hadoop.fs.Path) FileSystem(org.apache.hadoop.fs.FileSystem) FileNotFoundException(java.io.FileNotFoundException) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) Test(org.junit.Test)

Example 22 with CloudBlobContainer

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

the class TestContainerChecks method testContainerChecksWithSas.

@Test
public void testContainerChecksWithSas() throws Exception {
    Assume.assumeFalse(runningInSASMode);
    testAccount = AzureBlobStorageTestAccount.create("", EnumSet.of(CreateOptions.UseSas));
    assumeNotNull(testAccount);
    CloudBlobContainer container = testAccount.getRealContainer();
    FileSystem fs = testAccount.getFileSystem();
    // The container shouldn't be there
    assertFalse(container.exists());
    // A write should just fail
    try {
        fs.createNewFile(new Path("/foo"));
        assertFalse("Should've thrown.", true);
    } catch (AzureException ex) {
    }
    assertFalse(container.exists());
}
Also used : Path(org.apache.hadoop.fs.Path) FileSystem(org.apache.hadoop.fs.FileSystem) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) Test(org.junit.Test)

Example 23 with CloudBlobContainer

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

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

the class ManageWebAppStorageAccountConnection method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    // New resources
    final String suffix = ".azurewebsites.net";
    final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
    final String app1Url = app1Name + suffix;
    final String storageName = SdkContext.randomResourceName("jsdkstore", 20);
    final String containerName = SdkContext.randomResourceName("jcontainer", 20);
    final String rgName = SdkContext.randomResourceName("rg1NEMV_", 24);
    try {
        //============================================================
        // Create a storage account for the web app to use
        System.out.println("Creating storage account " + storageName + "...");
        StorageAccount storageAccount = azure.storageAccounts().define(storageName).withRegion(Region.US_WEST).withNewResourceGroup(rgName).create();
        String accountKey = storageAccount.getKeys().get(0).value();
        String connectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s", storageAccount.name(), accountKey);
        System.out.println("Created storage account " + storageAccount.name());
        //============================================================
        // Upload a few files to the storage account blobs
        System.out.println("Uploading 2 blobs to container " + containerName + "...");
        CloudBlobContainer container = setUpStorageAccount(connectionString, containerName);
        uploadFileToContainer(container, "helloworld.war", ManageWebAppStorageAccountConnection.class.getResource("/helloworld.war").getPath());
        uploadFileToContainer(container, "install_apache.sh", ManageWebAppStorageAccountConnection.class.getResource("/install_apache.sh").getPath());
        System.out.println("Uploaded 2 blobs to container " + container.getName());
        //============================================================
        // Create a web app with a new app service plan
        System.out.println("Creating web app " + app1Name + "...");
        WebApp app1 = azure.webApps().define(app1Name).withRegion(Region.US_WEST).withExistingResourceGroup(rgName).withNewWindowsPlan(PricingTier.STANDARD_S1).withJavaVersion(JavaVersion.JAVA_8_NEWEST).withWebContainer(WebContainer.TOMCAT_8_0_NEWEST).withConnectionString("storage.connectionString", connectionString, ConnectionStringType.CUSTOM).withAppSetting("storage.containerName", containerName).create();
        System.out.println("Created web app " + app1.name());
        Utils.print(app1);
        //============================================================
        // Deploy a web app that connects to the storage account
        // Source code: https://github.com/jianghaolu/azure-samples-blob-explorer
        System.out.println("Deploying azure-samples-blob-traverser.war to " + app1Name + " through FTP...");
        Utils.uploadFileToFtp(app1.getPublishingProfile(), "azure-samples-blob-traverser.war", ManageWebAppStorageAccountConnection.class.getResourceAsStream("/azure-samples-blob-traverser.war"));
        System.out.println("Deployment azure-samples-blob-traverser.war to web app " + app1.name() + " completed");
        Utils.print(app1);
        // warm up
        System.out.println("Warming up " + app1Url + "/azure-samples-blob-traverser...");
        curl("http://" + app1Url + "/azure-samples-blob-traverser");
        Thread.sleep(5000);
        System.out.println("CURLing " + app1Url + "/azure-samples-blob-traverser...");
        System.out.println(curl("http://" + app1Url + "/azure-samples-blob-traverser"));
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : StorageAccount(com.microsoft.azure.management.storage.StorageAccount) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) URISyntaxException(java.net.URISyntaxException) StorageException(com.microsoft.azure.storage.StorageException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) WebApp(com.microsoft.azure.management.appservice.WebApp)

Example 25 with CloudBlobContainer

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

Aggregations

CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)29 CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)13 CloudBlockBlob (com.microsoft.azure.storage.blob.CloudBlockBlob)9 CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)7 StorageException (com.microsoft.azure.storage.StorageException)7 URISyntaxException (java.net.URISyntaxException)6 Test (org.junit.Test)6 ListBlobItem (com.microsoft.azure.storage.blob.ListBlobItem)5 FileSystem (org.apache.hadoop.fs.FileSystem)5 Path (org.apache.hadoop.fs.Path)5 InvalidKeyException (java.security.InvalidKeyException)4 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 URI (java.net.URI)3 WebApp (com.microsoft.azure.management.appservice.WebApp)2 BlobContainerPermissions (com.microsoft.azure.storage.blob.BlobContainerPermissions)2 CloudBlob (com.microsoft.azure.storage.blob.CloudBlob)2 ArrayList (java.util.ArrayList)2 RepositoryException (org.elasticsearch.repositories.RepositoryException)2