Java Code Examples for com.microsoft.azure.storage.blob.CloudBlobContainer#deleteIfExists()

The following examples show how to use com.microsoft.azure.storage.blob.CloudBlobContainer#deleteIfExists() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CloudFileTests.java    From azure-storage-android with Apache License 2.0 7 votes vote down vote up
@Test
@Category(SlowTests.class)
public void testCopyBlockBlobSas() throws Exception {
    // Create source on server.
    final CloudBlobContainer container = BlobTestHelper.getRandomContainerReference();
    try {
        container.create();
        final CloudBlockBlob source = container.getBlockBlobReference("source");

        source.getMetadata().put("Test", "value");
        final String data = "String data";
        source.uploadText(data, Constants.UTF8_CHARSET, null, null, null);

        final CloudFile destination = doCloudBlobCopy(source, data.length());

        final String copyData = destination.downloadText(Constants.UTF8_CHARSET, null, null, null);
        assertEquals(data, copyData);
    }
    finally {
        container.deleteIfExists();
    }
}
 
Example 2
Source File: AzureFileUtility.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
/**
 * This method will remove the container from Azure Storage.
 *
 * @param containerName
 * @return boolean
 */
public static boolean deleteContainer(String containerName) {
  if (StringUtils.isBlank(containerName)) {
    ProjectLogger.log("Container name can't be null or empty");
    return false;
  }
  CloudBlobContainer container = AzureConnectionManager.getContainer(containerName, true);
  if (container == null) {
    ProjectLogger.log("Unable to get Azure contains object");
    return false;
  }
  try {
    boolean response = container.deleteIfExists();
    if (!response) {
      ProjectLogger.log("Container not found..");
    } else {
      ProjectLogger.log("Container is deleted===");
    }
    return true;
  } catch (Exception e) {
    ProjectLogger.log(e.getMessage(), e);
  }
  return false;
}
 
Example 3
Source File: SecondaryTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryOn304() throws StorageException, IOException, URISyntaxException {
    OperationContext operationContext = new OperationContext();
    operationContext.getRetryingEventHandler().addListener(new StorageEvent<RetryingEvent>() {
        @Override
        public void eventOccurred(RetryingEvent eventArg) {
            fail("Request should not be retried.");
        }
    });

    CloudBlobContainer container = BlobTestHelper.getRandomContainerReference();
    try {
        container.create();
        CloudBlockBlob blockBlobRef = (CloudBlockBlob) BlobTestHelper.uploadNewBlob(container, BlobType.BLOCK_BLOB,
                "originalBlob", 1024, null);
        AccessCondition accessCondition = AccessCondition.generateIfNoneMatchCondition(blockBlobRef.getProperties().getEtag());
        blockBlobRef.download(new ByteArrayOutputStream(), accessCondition, null, operationContext);
        
        fail("Download should fail with a 304.");
    } catch (StorageException ex) {
        assertEquals("The condition specified using HTTP conditional header(s) is not met.", ex.getMessage());
    } finally {
        container.deleteIfExists();
    }
}
 
Example 4
Source File: CloudFileTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
@Category(SlowTests.class)
public void testCopyPageBlobSas() throws Exception {
    // Create source on server.
    final CloudBlobContainer container = BlobTestHelper.getRandomContainerReference();
    try {
        container.create();
        final CloudPageBlob source = container.getPageBlobReference("source");

        source.getMetadata().put("Test", "value");
        final int length = 512;
        final ByteArrayInputStream data = BlobTestHelper.getRandomDataStream(length);
        source.upload(data, length);

        final CloudFile destination = doCloudBlobCopy(source, length);

        final ByteArrayOutputStream copyData = new ByteArrayOutputStream();
        destination.download(copyData);
        BlobTestHelper.assertStreamsAreEqual(data, new ByteArrayInputStream(copyData.toByteArray()));
    }
    finally {
        container.deleteIfExists();
    }
}
 
Example 5
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public void cleanupContainer(String baseLocation) {
    String containerName = getContainerName(baseLocation);
    CloudBlobContainer cloudBlobContainer = getCloudBlobContainer(containerName);

    try {
        Log.log(LOGGER, format(" Removing Azure Adls Gen 2 Blob Container with Name: %s at Base Location: %s", containerName, baseLocation));
        cloudBlobContainer.deleteIfExists();
        Log.log(LOGGER, format(" Azure Adls Gen 2 Blob Container: %s delete has been initiated. ", containerName));
    } catch (StorageException e) {
        if (e.getHttpStatusCode() == 404) {
            LOGGER.error("Azure Adls Gen2 Blob Container does not present with name: {} at Base Location: {}", containerName, baseLocation);
            throw new TestFailException("Azure Adls Gen2 Blob Container does not present with name: " +  containerName
                    + " at Base Location: " + baseLocation);
        } else {
            LOGGER.error("Azure Adls Gen2 Blob Container delete cannot be succeed!", e);
            throw new TestFailException("Azure Adls Gen2 Blob Container delete cannot be succeed!" + e);
        }
    } finally {
        createCloudBlobContainer(containerName);
    }
}
 
Example 6
Source File: AzureStorageBlobService.java    From components with Apache License 2.0 5 votes vote down vote up
/**
 * This method delete the container if exist
 */
public boolean deleteContainerIfExist(final String containerName)
        throws StorageException, URISyntaxException, InvalidKeyException {
    CloudBlobClient cloudBlobClient = connection.getCloudStorageAccount().createCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.getContainerReference(containerName);
    return cloudBlobContainer.deleteIfExists(null, null, AzureStorageUtils.getTalendOperationContext());
}
 
Example 7
Source File: GenericTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Make sure that if a request throws an error when it is being built that the request is not sent.
 *
 * @throws URISyntaxException
 * @throws StorageException
 */
@Test
public void testExecutionEngineErrorHandling() throws URISyntaxException, StorageException {
    CloudBlobContainer container = BlobTestHelper.getRandomContainerReference();
    try {
        final ArrayList<Boolean> callList = new ArrayList<Boolean>();

        OperationContext opContext = new OperationContext();
        opContext.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {
            // insert a metadata element with an empty value
            @Override
            public void eventOccurred(SendingRequestEvent eventArg) {
                callList.add(true);
            }
        });

        container.getMetadata().put("key", " "); // invalid value
        try {
            container.uploadMetadata(null, null, opContext);
            fail(SR.METADATA_KEY_INVALID);
        } catch (StorageException e) {
            // make sure a request was not sent
            assertEquals(0, callList.size());

            assertEquals(SR.METADATA_VALUE_INVALID, e.getMessage());
        }
    } finally {
        container.deleteIfExists();
    }
}
 
Example 8
Source File: AzureClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public void deleteContainerInStorage(String resourceGroup, String storageName, String containerName) {
    LOGGER.debug("delete container: RG={}, storageName={}, containerName={}", resourceGroup, storageName, containerName);
    CloudBlobContainer container = getBlobContainer(resourceGroup, storageName, containerName);
    try {
        boolean existed = container.deleteIfExists();
        LOGGER.debug("Is container existed: " + existed);
    } catch (StorageException e) {
        throw new CloudConnectorException("can't delete container in storage, storage service error occurred", e);
    }
}
 
Example 9
Source File: GenericTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
public void testReadTimeoutIssue() throws URISyntaxException, StorageException, IOException {
    // part 1
    byte[] buffer = BlobTestHelper.getRandomBuffer(1 * 1024 * 1024);

    // set the maximum execution time
    BlobRequestOptions options = new BlobRequestOptions();
    options.setMaximumExecutionTimeInMs(5000);

    CloudBlobClient blobClient = TestHelper.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(generateRandomContainerName());

    String blobName = "testBlob";
    final CloudBlockBlob blockBlobRef = container.getBlockBlobReference(blobName);
    blockBlobRef.setStreamWriteSizeInBytes(1 * 1024 * 1024);

    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
    BlobOutputStream blobOutputStream = null;

    try {
        container.createIfNotExists();
        blobOutputStream = blockBlobRef.openOutputStream(null, options, null);
        try {
            blobOutputStream.write(inputStream, buffer.length);
        } finally {
            blobOutputStream.close();
        }
        assertTrue(blockBlobRef.exists());
    } finally {
        inputStream.close();
        container.deleteIfExists();
    }

    // part 2
    int length2 = 10 * 1024 * 1024;
    byte[] uploadBuffer2 = BlobTestHelper.getRandomBuffer(length2);

    CloudBlobClient blobClient2 = TestHelper.createCloudBlobClient();
    CloudBlobContainer container2 = blobClient2.getContainerReference(generateRandomContainerName());

    String blobName2 = "testBlob";
    final CloudBlockBlob blockBlobRef2 = container2.getBlockBlobReference(blobName2);

    ByteArrayInputStream inputStream2 = new ByteArrayInputStream(uploadBuffer2);

    try {
        container2.createIfNotExists();

        blockBlobRef2.upload(inputStream2, length2);
    } finally {
        inputStream2.close();
        container2.deleteIfExists();
    }
}
 
Example 10
Source File: MaximumExecutionTimeTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class, SlowTests.class })
public void testMaximumExecutionTimeBlobByteArray() throws URISyntaxException, StorageException, IOException {
    int length = 10 * 1024 * 1024;
    byte[] uploadBuffer = BlobTestHelper.getRandomBuffer(length);
    byte[] downloadBuffer = new byte[length];

    // set a delay in sending request
    OperationContext opContext = new OperationContext();
    setDelay(opContext, 2500);

    // set the maximum execution time
    BlobRequestOptions options = new BlobRequestOptions();
    options.setMaximumExecutionTimeInMs(2000);

    CloudBlobClient blobClient = TestHelper.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(generateRandomName("container"));

    String blobName = "testBlob";
    final CloudBlockBlob blockBlobRef = container.getBlockBlobReference(blobName);

    ByteArrayInputStream inputStream = new ByteArrayInputStream(uploadBuffer);

    try {
        container.createIfNotExists();

        blockBlobRef.upload(inputStream, length);
        assertTrue(blockBlobRef.exists());

        try {
            blockBlobRef.downloadToByteArray(downloadBuffer, 0, null, options, opContext);
            fail("Maximum execution time was reached but request did not fail.");
        }
        catch (StorageException e) {
            assertEquals(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION, e.getCause().getMessage());
        }
    }
    finally {
        inputStream.close();
        container.deleteIfExists();
    }
}
 
Example 11
Source File: AzureFileManager.java    From SEAL-Demo with MIT License 3 votes vote down vote up
/**
 * Delete a blob container with the given name
 *
 * @param containerName Container name
 * @throws URISyntaxException if containerName contains incorrect Uri syntax
 * @throws InvalidKeyException if containerName contains an invalid key
 * @throws StorageException if the blob client is unable to get a container reference
 * @throws ExecutionException if the blob client is unable to get a container reference
 * @throws InterruptedException if the blob client is unable to get a container reference
 */
public static void DeleteBlobContainer(String containerName)
        throws URISyntaxException,
               StorageException,
               ExecutionException,
               InterruptedException {
    CloudBlobContainer container = getContainer(containerName);
    container.deleteIfExists();
}