Search in sources :

Example 6 with CloudBlobClient

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

the class AzureStorageServiceImpl method deleteBlob.

@Override
public void deleteBlob(String account, LocationMode mode, String container, String blob) throws URISyntaxException, StorageException {
    logger.trace("delete blob for container [{}], blob [{}]", container, blob);
    // Container name must be lower case.
    CloudBlobClient client = this.getSelectedClient(account, mode);
    CloudBlobContainer blobContainer = client.getContainerReference(container);
    if (blobContainer.exists()) {
        logger.trace("container [{}]: blob [{}] found. removing.", container, blob);
        CloudBlockBlob azureBlob = blobContainer.getBlockBlobReference(blob);
        SocketAccess.doPrivilegedVoidException(azureBlob::delete);
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob)

Example 7 with CloudBlobClient

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

the class AzureStorageServiceImpl method listBlobsByPrefix.

@Override
public Map<String, BlobMetaData> listBlobsByPrefix(String account, LocationMode mode, 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!
    logger.debug("listing container [{}], keyPath [{}], prefix [{}]", container, keyPath, prefix);
    MapBuilder<String, BlobMetaData> blobsBuilder = MapBuilder.newMapBuilder();
    CloudBlobClient client = this.getSelectedClient(account, mode);
    CloudBlobContainer blobContainer = client.getContainerReference(container);
    SocketAccess.doPrivilegedVoidException(() -> {
        if (blobContainer.exists()) {
            for (ListBlobItem blobItem : blobContainer.listBlobs(keyPath + (prefix == null ? "" : prefix))) {
                URI uri = blobItem.getUri();
                logger.trace("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 /
                String blobPath = uri.getPath().substring(1 + container.length() + 1);
                CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobPath);
                // fetch the blob attributes from Azure (getBlockBlobReference does not do this)
                // this is needed to retrieve the blob length (among other metadata) from Azure Storage
                blob.downloadAttributes();
                BlobProperties properties = blob.getProperties();
                String name = blobPath.substring(keyPath.length());
                logger.trace("blob url [{}], name [{}], size [{}]", uri, name, properties.getLength());
                blobsBuilder.put(name, new PlainBlobMetaData(name, properties.getLength()));
            }
        }
    });
    return blobsBuilder.immutableMap();
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) PlainBlobMetaData(org.elasticsearch.common.blobstore.support.PlainBlobMetaData) BlobMetaData(org.elasticsearch.common.blobstore.BlobMetaData) PlainBlobMetaData(org.elasticsearch.common.blobstore.support.PlainBlobMetaData) BlobProperties(com.microsoft.azure.storage.blob.BlobProperties) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) URI(java.net.URI)

Example 8 with CloudBlobClient

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

the class AzureStorageServiceImpl method moveBlob.

@Override
public void moveBlob(String account, LocationMode mode, String container, String sourceBlob, String targetBlob) throws URISyntaxException, StorageException {
    logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}]", container, sourceBlob, targetBlob);
    CloudBlobClient client = this.getSelectedClient(account, mode);
    CloudBlobContainer blobContainer = client.getContainerReference(container);
    CloudBlockBlob blobSource = blobContainer.getBlockBlobReference(sourceBlob);
    if (blobSource.exists()) {
        CloudBlockBlob blobTarget = blobContainer.getBlockBlobReference(targetBlob);
        SocketAccess.doPrivilegedVoidException(() -> {
            blobTarget.startCopy(blobSource);
            blobSource.delete();
        });
        logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}] -> done", container, sourceBlob, targetBlob);
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob)

Example 9 with CloudBlobClient

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

the class AzureStorageServiceTests method testGetSelectedClientPrimary.

public void testGetSelectedClientPrimary() {
    AzureStorageServiceImpl azureStorageService = new AzureStorageServiceMock(settings);
    CloudBlobClient client = azureStorageService.getSelectedClient("azure1", LocationMode.PRIMARY_ONLY);
    assertThat(client.getEndpoint(), is(URI.create("https://azure1")));
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient)

Example 10 with CloudBlobClient

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

the class AzureStorageServiceTests method testGetSelectedClientNoTimeout.

public void testGetSelectedClientNoTimeout() {
    Settings timeoutSettings = Settings.builder().put("cloud.azure.storage.azure.account", "myaccount").put("cloud.azure.storage.azure.key", "mykey").build();
    AzureStorageServiceImpl azureStorageService = new AzureStorageServiceMock(timeoutSettings);
    CloudBlobClient client1 = azureStorageService.getSelectedClient("azure", LocationMode.PRIMARY_ONLY);
    assertThat(client1.getDefaultRequestOptions().getTimeoutIntervalInMs(), is(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