Search in sources :

Example 1 with CloudBlobClient

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

the class AzureStorageServiceImpl method getSelectedClient.

CloudBlobClient getSelectedClient(String account, LocationMode mode) {
    logger.trace("selecting a client for account [{}], mode [{}]", account, mode.name());
    AzureStorageSettings azureStorageSettings = null;
    if (this.primaryStorageSettings == null) {
        throw new IllegalArgumentException("No primary azure storage can be found. Check your elasticsearch.yml.");
    }
    if (Strings.hasLength(account)) {
        azureStorageSettings = this.secondariesStorageSettings.get(account);
    }
    // if account is not secondary, it's the primary
    if (azureStorageSettings == null) {
        if (Strings.hasLength(account) == false || primaryStorageSettings.getName() == null || account.equals(primaryStorageSettings.getName())) {
            azureStorageSettings = primaryStorageSettings;
        }
    }
    if (azureStorageSettings == null) {
        // We did not get an account. That's bad.
        throw new IllegalArgumentException("Can not find azure account [" + account + "]. Check your elasticsearch.yml.");
    }
    CloudBlobClient client = this.clients.get(azureStorageSettings.getAccount());
    if (client == null) {
        throw new IllegalArgumentException("Can not find an azure client for account [" + account + "]");
    }
    // NOTE: for now, just set the location mode in case it is different;
    // only one mode per storage account can be active at a time
    client.getDefaultRequestOptions().setLocationMode(mode);
    // Set timeout option if the user sets cloud.azure.storage.timeout or cloud.azure.storage.xxx.timeout (it's negative by default)
    if (azureStorageSettings.getTimeout().getSeconds() > 0) {
        try {
            int timeout = (int) azureStorageSettings.getTimeout().getMillis();
            client.getDefaultRequestOptions().setTimeoutIntervalInMs(timeout);
        } catch (ClassCastException e) {
            throw new IllegalArgumentException("Can not convert [" + azureStorageSettings.getTimeout() + "]. It can not be longer than 2,147,483,647ms.");
        }
    }
    return client;
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient)

Example 2 with CloudBlobClient

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

the class AzureStorageServiceImpl method doesContainerExist.

@Override
public boolean doesContainerExist(String account, LocationMode mode, String container) {
    try {
        CloudBlobClient client = this.getSelectedClient(account, mode);
        CloudBlobContainer blobContainer = client.getContainerReference(container);
        return SocketAccess.doPrivilegedException(blobContainer::exists);
    } catch (Exception e) {
        logger.error("can not access container [{}]", container);
    }
    return false;
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) URISyntaxException(java.net.URISyntaxException) StorageException(com.microsoft.azure.storage.StorageException) RepositoryException(org.elasticsearch.repositories.RepositoryException) PrivilegedActionException(java.security.PrivilegedActionException)

Example 3 with CloudBlobClient

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

the class AzureStorageServiceImpl method deleteFiles.

@Override
public void deleteFiles(String account, LocationMode mode, String container, String path) throws URISyntaxException, StorageException {
    logger.trace("delete files container [{}], path [{}]", container, path);
    // Container name must be lower case.
    CloudBlobClient client = this.getSelectedClient(account, mode);
    CloudBlobContainer blobContainer = client.getContainerReference(container);
    SocketAccess.doPrivilegedVoidException(() -> {
        if (blobContainer.exists()) {
            // We list the blobs using a flat blob listing mode
            for (ListBlobItem blobItem : blobContainer.listBlobs(path, true)) {
                String blobName = blobNameFromUri(blobItem.getUri());
                logger.trace("removing blob [{}] full URI was [{}]", blobName, blobItem.getUri());
                deleteBlob(account, mode, container, blobName);
            }
        }
    });
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer)

Example 4 with CloudBlobClient

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

the class AzureStorageServiceImpl method getOutputStream.

@Override
public OutputStream getOutputStream(String account, LocationMode mode, String container, String blob) throws URISyntaxException, StorageException {
    logger.trace("writing container [{}], blob [{}]", container, blob);
    CloudBlobClient client = this.getSelectedClient(account, mode);
    CloudBlockBlob blockBlobReference = client.getContainerReference(container).getBlockBlobReference(blob);
    return SocketAccess.doPrivilegedException(blockBlobReference::openOutputStream);
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob)

Example 5 with CloudBlobClient

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

the class AzureStorageServiceImpl method createClient.

void createClient(AzureStorageSettings azureStorageSettings) {
    try {
        logger.trace("creating new Azure storage client using account [{}], key [{}]", azureStorageSettings.getAccount(), azureStorageSettings.getKey());
        String storageConnectionString = "DefaultEndpointsProtocol=https;" + "AccountName=" + azureStorageSettings.getAccount() + ";" + "AccountKey=" + azureStorageSettings.getKey();
        // Retrieve storage account from connection-string.
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        // Create the blob client.
        CloudBlobClient client = storageAccount.createCloudBlobClient();
        // Register the client
        this.clients.put(azureStorageSettings.getAccount(), client);
    } catch (Exception e) {
        logger.error("can not create azure storage client: {}", e.getMessage());
    }
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) URISyntaxException(java.net.URISyntaxException) StorageException(com.microsoft.azure.storage.StorageException) RepositoryException(org.elasticsearch.repositories.RepositoryException) PrivilegedActionException(java.security.PrivilegedActionException)

Aggregations

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