Search in sources :

Example 6 with BlobClient

use of com.azure.storage.blob.BlobClient in project beam by apache.

the class AzureBlobStoreFileSystem method delete.

/**
 * This method will delete a virtual folder or a blob, not a container.
 */
@Override
protected void delete(Collection<AzfsResourceId> resourceIds) throws IOException {
    for (AzfsResourceId resourceId : resourceIds) {
        if (resourceId.getBlob() == null) {
            throw new IOException("delete does not delete containers.");
        }
        BlobContainerClient container = client.get().getBlobContainerClient(resourceId.getContainer());
        // deleting a blob that is not a directory
        if (!resourceId.isDirectory()) {
            BlobClient blob = container.getBlobClient(resourceId.getBlob());
            if (!blob.exists()) {
                throw new FileNotFoundException("The resource to delete does not exist.");
            }
            blob.delete();
        } else // deleting a directory (not a container)
        {
            PagedIterable<BlobItem> blobsInDirectory = container.listBlobsByHierarchy(resourceId.getBlob());
            blobsInDirectory.forEach(blob -> {
                String blobName = blob.getName();
                container.getBlobClient(blobName).delete();
            });
        }
    }
}
Also used : BlobItem(com.azure.storage.blob.models.BlobItem) BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobClient(com.azure.storage.blob.BlobClient) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 7 with BlobClient

use of com.azure.storage.blob.BlobClient in project beam by apache.

the class AzureBlobStoreFileSystem method toMatchResult.

private MatchResult toMatchResult(AzfsResourceId path) {
    BlobClient blobClient = client.get().getBlobContainerClient(path.getContainer()).getBlobClient(path.getBlob());
    BlobProperties blobProperties;
    try {
        blobProperties = blobClient.getProperties();
    } catch (BlobStorageException e) {
        if (e.getStatusCode() == 404) {
            return MatchResult.create(MatchResult.Status.NOT_FOUND, new FileNotFoundException());
        }
        return MatchResult.create(MatchResult.Status.ERROR, new IOException(e));
    }
    return MatchResult.create(MatchResult.Status.OK, ImmutableList.of(toMetadata(path.withSize(blobProperties.getBlobSize()).withLastModified(Date.from(blobProperties.getLastModified().toInstant())), blobProperties.getContentEncoding(), blobProperties.getETag())));
}
Also used : BlobClient(com.azure.storage.blob.BlobClient) BlobProperties(com.azure.storage.blob.models.BlobProperties) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BlobStorageException(com.azure.storage.blob.models.BlobStorageException)

Example 8 with BlobClient

use of com.azure.storage.blob.BlobClient in project beam by apache.

the class AzureBlobStoreFileSystem method create.

@Override
protected WritableByteChannel create(AzfsResourceId resourceId, CreateOptions createOptions) throws IOException {
    BlobContainerClient blobContainerClient = client.get().getBlobContainerClient(resourceId.getContainer());
    if (!blobContainerClient.exists()) {
        throw new FileNotFoundException("This container does not exist. Creating containers is not supported.");
    }
    BlobClient blobClient = blobContainerClient.getBlobClient(resourceId.getBlob());
    // so throw an error in this case to prevent data loss
    if (blobClient.exists()) {
        throw new IOException("This filename is already in use.");
    }
    OutputStream outputStream;
    try {
        outputStream = blobClient.getBlockBlobClient().getBlobOutputStream();
    } catch (BlobStorageException e) {
        throw (IOException) e.getCause();
    }
    return newChannel(outputStream);
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobClient(com.azure.storage.blob.BlobClient) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BlobStorageException(com.azure.storage.blob.models.BlobStorageException)

Aggregations

BlobClient (com.azure.storage.blob.BlobClient)8 IOException (java.io.IOException)6 FileNotFoundException (java.io.FileNotFoundException)5 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)4 BlobClientBuilder (com.azure.storage.blob.BlobClientBuilder)3 FileUploadCompletionNotification (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification)3 FileUploadSasUriRequest (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)3 FileUploadSasUriResponse (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse)3 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 BlobItem (com.azure.storage.blob.models.BlobItem)1 BlobProperties (com.azure.storage.blob.models.BlobProperties)1 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 IotHubClientProtocol (com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)1 IotHubServiceException (com.microsoft.azure.sdk.iot.device.exceptions.IotHubServiceException)1 OutputStream (java.io.OutputStream)1 Scanner (java.util.Scanner)1 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)1