Search in sources :

Example 31 with CloudBlobClient

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

the class AzureStorageService method deleteBlobDirectory.

void deleteBlobDirectory(String container, String path) throws URISyntaxException, StorageException, IOException {
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    for (final ListBlobItem blobItem : blobContainer.listBlobs(path, true)) {
        // uri.getPath is of the form /container/keyPath.* and we want to strip off the /container/
        // this requires 1 + container.length() + 1, with each 1 corresponding to one of the /
        final String blobPath = blobItem.getUri().getPath().substring(1 + container.length() + 1);
        try {
            deleteBlob(container, blobPath);
        } catch (URISyntaxException | StorageException e) {
            throw new IOException("Deleting directory [" + path + "] failed");
        }
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) Supplier(java.util.function.Supplier) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StorageException(com.microsoft.azure.storage.StorageException)

Example 32 with CloudBlobClient

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

the class AzureStorageService method doesContainerExist.

public boolean doesContainerExist(String container) throws URISyntaxException, StorageException {
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    return blobContainer.exists(null, null, client.v2().get());
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) Supplier(java.util.function.Supplier) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer)

Example 33 with CloudBlobClient

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

the class AzureStorageService method buildClient.

protected CloudBlobClient buildClient(AzureStorageSettings azureStorageSettings) throws InvalidKeyException, URISyntaxException {
    final CloudBlobClient client = createClient(azureStorageSettings);
    // Set timeout option if the user sets cloud.azure.storage.timeout or
    // cloud.azure.storage.xxx.timeout (it's negative by default)
    final long timeout = azureStorageSettings.getTimeout().getMillis();
    if (timeout > 0) {
        if (timeout > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("Timeout [" + azureStorageSettings.getTimeout() + "] exceeds 2,147,483,647ms.");
        }
        client.getDefaultRequestOptions().setTimeoutIntervalInMs((int) timeout);
    }
    // We define a default exponential retry policy
    client.getDefaultRequestOptions().setRetryPolicyFactory(new RetryExponentialRetry(RetryPolicy.DEFAULT_CLIENT_BACKOFF, azureStorageSettings.getMaxRetries()));
    client.getDefaultRequestOptions().setLocationMode(azureStorageSettings.getLocationMode());
    return client;
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) RetryExponentialRetry(com.microsoft.azure.storage.RetryExponentialRetry)

Example 34 with CloudBlobClient

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

the class AzureStorageService method listBlobsByPrefix.

public Map<String, BlobMetadata> listBlobsByPrefix(String container, String keyPath, String prefix) throws URISyntaxException, StorageException {
    // NOTE: this should be here: if (prefix == null) prefix = "";
    // however, this is really inefficient since deleteBlobsByPrefix enumerates everything and
    // then does a prefix match on the result; it should just call listBlobsByPrefix with the prefix!
    final var blobsBuilder = new HashMap<String, BlobMetadata>();
    final EnumSet<BlobListingDetails> enumBlobListingDetails = EnumSet.of(BlobListingDetails.METADATA);
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    LOGGER.trace(() -> new ParameterizedMessage("listing container [{}], keyPath [{}], prefix [{}]", container, keyPath, prefix));
    for (final ListBlobItem blobItem : blobContainer.listBlobs(keyPath + (prefix == null ? "" : prefix), false, enumBlobListingDetails, null, client.v2().get())) {
        final URI uri = blobItem.getUri();
        LOGGER.trace(() -> new ParameterizedMessage("blob url [{}]", uri));
        // uri.getPath is of the form /container/keyPath.* and we want to strip off the /container/
        // this requires 1 + container.length() + 1, with each 1 corresponding to one of the /
        final String blobPath = uri.getPath().substring(1 + container.length() + 1);
        if (blobItem instanceof CloudBlob) {
            final BlobProperties properties = ((CloudBlob) blobItem).getProperties();
            final String name = blobPath.substring(keyPath.length());
            LOGGER.trace(() -> new ParameterizedMessage("blob url [{}], name [{}], size [{}]", uri, name, properties.getLength()));
            blobsBuilder.put(name, new PlainBlobMetadata(name, properties.getLength()));
        }
    }
    return Map.copyOf(blobsBuilder);
}
Also used : BlobListingDetails(com.microsoft.azure.storage.blob.BlobListingDetails) CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) HashMap(java.util.HashMap) URI(java.net.URI) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) BlobProperties(com.microsoft.azure.storage.blob.BlobProperties) PlainBlobMetadata(org.elasticsearch.common.blobstore.support.PlainBlobMetadata) Supplier(java.util.function.Supplier) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 35 with CloudBlobClient

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

the class AzureStorageServiceTests method testGetSelectedClientDefaultTimeout.

public void testGetSelectedClientDefaultTimeout() {
    final Settings timeoutSettings = Settings.builder().put(buildClientCredSettings()).build();
    final AzureStorageService azureStorageService = storageServiceWithSettings(timeoutSettings);
    final CloudBlobClient client = azureStorageService.client().v1();
    assertThat(client.getDefaultRequestOptions().getTimeoutIntervalInMs(), nullValue());
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

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