Search in sources :

Example 1 with ListBlobContainersOptions

use of com.azure.storage.blob.models.ListBlobContainersOptions in project ats-framework by Axway.

the class BlobStorageOperations method listContainers.

private PagedIterable<BlobContainerItem> listContainers(String containerNamePrefix, long retrieveTimeoutSeconds, boolean retrieveMetadata) {
    try {
        StringBuilder message = new StringBuilder();
        message.append("Listing");
        BlobContainerListDetails bcld = new BlobContainerListDetails();
        ListBlobContainersOptions lbco = new ListBlobContainersOptions();
        message.append(" containers");
        if (containerNamePrefix != null && !containerNamePrefix.isEmpty()) {
            message.append(" with prefix '" + containerNamePrefix + "'");
            lbco.setPrefix(containerNamePrefix);
        }
        if (log.isInfoEnabled()) {
            message.append(" ...");
            log.info(message.toString());
        }
        bcld.setRetrieveMetadata(retrieveMetadata);
        lbco.setDetails(bcld);
        if (retrieveTimeoutSeconds <= 0) {
            // just a little less than too much
            retrieveTimeoutSeconds = Integer.MAX_VALUE / 2;
        }
        PagedIterable<BlobContainerItem> blobContainers = serviceClient.listBlobContainers(lbco, Duration.ofSeconds(retrieveTimeoutSeconds));
        log.info("Successfully listed " + blobContainers.stream().count() + " containers.");
        return blobContainers;
    } catch (Exception e) {
        throw new AtsBlobStorageException("Could not list containers " + (!StringUtils.isNullOrEmpty(containerNamePrefix) ? "with prefix '" + containerNamePrefix + "'" : "") + " in " + retrieveTimeoutSeconds + " seconds", e);
    }
}
Also used : BlobContainerItem(com.azure.storage.blob.models.BlobContainerItem) ListBlobContainersOptions(com.azure.storage.blob.models.ListBlobContainersOptions) BlobContainerListDetails(com.azure.storage.blob.models.BlobContainerListDetails) BlobStorageException(com.azure.storage.blob.models.BlobStorageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with ListBlobContainersOptions

use of com.azure.storage.blob.models.ListBlobContainersOptions in project ambry by linkedin.

the class AzureTokenResetTool method resetTokens.

/**
 * Reset the offset token by deleting the blob from the container.
 * @param containerPrefix the prefix used to filter on Azure containers
 *                       (in case the storage account hosts multiple Ambry clusters).
 * @return the number of tokens successfully reset.
 */
public static int resetTokens(String containerPrefix) throws BlobStorageException {
    AtomicInteger tokensDeleted = new AtomicInteger(0);
    ListBlobContainersOptions listOptions = new ListBlobContainersOptions().setPrefix(containerPrefix);
    storageClient.listBlobContainers(listOptions, null).iterator().forEachRemaining(blobContainer -> {
        BlockBlobClient blobClient = storageClient.getBlobContainerClient(blobContainer.getName()).getBlobClient(ReplicationConfig.REPLICA_TOKEN_FILE_NAME).getBlockBlobClient();
        try {
            if (blobClient.exists()) {
                blobClient.delete();
                tokensDeleted.incrementAndGet();
                logger.info("Deleted token for partition {}", blobContainer.getName());
            }
        } catch (Exception ex) {
            logger.error("Failed delete for {}", blobContainer.getName(), ex);
        }
    });
    return tokensDeleted.get();
}
Also used : BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) ListBlobContainersOptions(com.azure.storage.blob.models.ListBlobContainersOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BlobStorageException(com.azure.storage.blob.models.BlobStorageException)

Aggregations

BlobStorageException (com.azure.storage.blob.models.BlobStorageException)2 ListBlobContainersOptions (com.azure.storage.blob.models.ListBlobContainersOptions)2 BlobContainerItem (com.azure.storage.blob.models.BlobContainerItem)1 BlobContainerListDetails (com.azure.storage.blob.models.BlobContainerListDetails)1 BlockBlobClient (com.azure.storage.blob.specialized.BlockBlobClient)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1