Search in sources :

Example 6 with BlobContainerClient

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

the class AzureBlobStoreFileSystemTest method testMatchNonGlobs.

@Test
public void testMatchNonGlobs() throws Exception {
    // TODO: Write this test with mocks - see GcsFileSystemTest
    String container = "test-container" + randomUUID();
    BlobContainerClient blobContainerClient = azureBlobStoreFileSystem.getClient().createBlobContainer(container);
    List<String> blobNames = new ArrayList<>();
    blobNames.add("testdirectory/file1name");
    blobNames.add("testdirectory/dir2name/");
    blobNames.add("testdirectory/file4name");
    for (String blob : blobNames) {
        blobContainerClient.getBlobClient(blob).uploadFromFile("src/test/resources/in.txt");
    }
    List<String> specs = ImmutableList.of("azfs://account/" + container + "/testdirectory/file1name", "azfs://account/" + container + "/testdirectory/dir2name/", "azfs://account/" + container + "/testdirectory/file4name");
    List<MatchResult> matchResults = azureBlobStoreFileSystem.match(specs);
    assertEquals(3, matchResults.size());
    assertThat(ImmutableList.of("azfs://account/" + container + "/testdirectory/file1name"), contains(toFilenames(matchResults.get(0)).toArray()));
    assertThat(ImmutableList.of("azfs://account/" + container + "/testdirectory/dir2name/"), contains(toFilenames(matchResults.get(1)).toArray()));
    assertThat(ImmutableList.of("azfs://account/" + container + "/testdirectory/file4name"), contains(toFilenames(matchResults.get(2)).toArray()));
    blobContainerClient.delete();
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MatchResult(org.apache.beam.sdk.io.fs.MatchResult) Test(org.junit.Test)

Example 7 with BlobContainerClient

use of com.azure.storage.blob.BlobContainerClient 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 8 with BlobContainerClient

use of com.azure.storage.blob.BlobContainerClient 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

BlobContainerClient (com.azure.storage.blob.BlobContainerClient)8 BlobClient (com.azure.storage.blob.BlobClient)4 FileNotFoundException (java.io.FileNotFoundException)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 BlobItem (com.azure.storage.blob.models.BlobItem)2 IOException (java.io.IOException)2 MatchResult (org.apache.beam.sdk.io.fs.MatchResult)2 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)2 Ignore (org.junit.Ignore)2 BlobProperties (com.azure.storage.blob.models.BlobProperties)1 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)1 ListBlobsOptions (com.azure.storage.blob.models.ListBlobsOptions)1 OutputStream (java.io.OutputStream)1 Duration (java.time.Duration)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 ImmutableList (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList)1