Search in sources :

Example 66 with CloudBlobClient

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

the class AzureStorageService method deleteBlob.

public void deleteBlob(String container, String blob) throws URISyntaxException, StorageException {
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    // Container name must be lower case.
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    LOGGER.trace(() -> new ParameterizedMessage("delete blob for container [{}], blob [{}]", container, blob));
    final CloudBlockBlob azureBlob = blobContainer.getBlockBlobReference(blob);
    LOGGER.trace(() -> new ParameterizedMessage("container [{}]: blob [{}] found. removing.", container, blob));
    azureBlob.delete(DeleteSnapshotsOption.NONE, 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) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob)

Example 67 with CloudBlobClient

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

the class AzureStorageService method writeBlob.

public void writeBlob(String container, String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) throws URISyntaxException, StorageException, IOException {
    LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {})", blobName, blobSize));
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    final CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName);
    try {
        final AccessCondition accessCondition = failIfAlreadyExists ? AccessCondition.generateIfNotExistsCondition() : AccessCondition.generateEmptyCondition();
        blob.upload(inputStream, blobSize, accessCondition, null, client.v2().get());
    } catch (final StorageException se) {
        if (failIfAlreadyExists && se.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT && StorageErrorCodeStrings.BLOB_ALREADY_EXISTS.equals(se.getErrorCode())) {
            throw new FileAlreadyExistsException(blobName, null, se.getMessage());
        }
        throw se;
    }
    LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {}) - done", blobName, blobSize));
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessCondition(com.microsoft.azure.storage.AccessCondition) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Supplier(java.util.function.Supplier) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) StorageException(com.microsoft.azure.storage.StorageException)

Example 68 with CloudBlobClient

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

the class AzureStorageService method children.

public Set<String> children(String account, String container, BlobPath path) throws URISyntaxException, StorageException {
    final var blobsBuilder = new HashSet<String>();
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    final String keyPath = path.buildAsString();
    final EnumSet<BlobListingDetails> enumBlobListingDetails = EnumSet.of(BlobListingDetails.METADATA);
    for (ListBlobItem blobItem : blobContainer.listBlobs(keyPath, false, enumBlobListingDetails, null, client.v2().get())) {
        if (blobItem instanceof CloudBlobDirectory) {
            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 /.
            // Lastly, we add the length of keyPath to the offset to strip this container's path.
            final String uriPath = uri.getPath();
            blobsBuilder.add(uriPath.substring(1 + container.length() + 1 + keyPath.length(), uriPath.length() - 1));
        }
    }
    return Set.copyOf(blobsBuilder);
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) BlobListingDetails(com.microsoft.azure.storage.blob.BlobListingDetails) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) CloudBlobDirectory(com.microsoft.azure.storage.blob.CloudBlobDirectory) URI(java.net.URI) Supplier(java.util.function.Supplier) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) HashSet(java.util.HashSet)

Example 69 with CloudBlobClient

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

the class AzureStorageService method blobExists.

public boolean blobExists(String container, String blob) throws URISyntaxException, StorageException {
    // Container name must be lower case.
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    final CloudBlockBlob azureBlob = blobContainer.getBlockBlobReference(blob);
    return azureBlob.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) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob)

Example 70 with CloudBlobClient

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

the class AzureStorageService method getInputStream.

public InputStream getInputStream(String container, String blob) throws URISyntaxException, StorageException, IOException {
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlockBlob blockBlobReference = client.v1().getContainerReference(container).getBlockBlobReference(blob);
    LOGGER.trace(() -> new ParameterizedMessage("reading container [{}], blob [{}]", container, blob));
    return blockBlobReference.openInputStream(null, null, client.v2().get());
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) Supplier(java.util.function.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob)

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