Search in sources :

Example 1 with BlobContainerClient

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

the class AzureBlobStoreFileSystem method expand.

/**
 * Expands a pattern into {@link MatchResult}.
 */
@VisibleForTesting
MatchResult expand(AzfsResourceId azfsPattern) {
    checkArgument(azfsPattern.isWildcard(), "The resource id should be a wildcard.");
    String blobPrefix = azfsPattern.getBlobNonWildcardPrefix();
    Pattern wildcardAsRegexp = Pattern.compile(wildcardToRegexp(azfsPattern.getBlob()));
    LOG.debug("matching files in container {}, prefix {} against pattern {}", azfsPattern.getContainer(), blobPrefix, wildcardAsRegexp.toString());
    ListBlobsOptions listOptions = new ListBlobsOptions().setPrefix(blobPrefix);
    Duration timeout = Duration.ofMinutes(1);
    String account = azfsPattern.getAccount();
    String container = azfsPattern.getContainer();
    BlobContainerClient blobContainerClient = client.get().getBlobContainerClient(container);
    PagedIterable<BlobItem> blobs = blobContainerClient.listBlobs(listOptions, timeout);
    List<MatchResult.Metadata> results = new ArrayList<>();
    blobs.forEach(blob -> {
        String name = blob.getName();
        if (wildcardAsRegexp.matcher(name).matches() && !name.endsWith("/")) {
            LOG.debug("Matched object: azfs://{}/{}/{}", account, container, name);
            BlobProperties properties = blobContainerClient.getBlobClient(name).getProperties();
            AzfsResourceId rid = AzfsResourceId.fromComponents(account, container, name).withSize(properties.getBlobSize()).withLastModified(Date.from(properties.getLastModified().toInstant()));
            results.add(toMetadata(rid, properties.getContentEncoding(), properties.getETag()));
        }
    });
    return MatchResult.create(MatchResult.Status.OK, results);
}
Also used : BlobItem(com.azure.storage.blob.models.BlobItem) Pattern(java.util.regex.Pattern) BlobContainerClient(com.azure.storage.blob.BlobContainerClient) ListBlobsOptions(com.azure.storage.blob.models.ListBlobsOptions) BlobProperties(com.azure.storage.blob.models.BlobProperties) ArrayList(java.util.ArrayList) Duration(java.time.Duration) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Example 2 with BlobContainerClient

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

the class AzureBlobStoreFileSystem method copy.

@VisibleForTesting
void copy(AzfsResourceId sourcePath, AzfsResourceId destinationPath) throws IOException {
    checkArgument(sourcePath.getBlob() != null && destinationPath.getBlob() != null, "This method is intended to copy file-like resources, not directories.");
    // get source blob client
    BlobClient srcBlobClient = client.get().getBlobContainerClient(sourcePath.getContainer()).getBlobClient(sourcePath.getBlob());
    if (!srcBlobClient.exists()) {
        throw new FileNotFoundException("The copy source does not exist.");
    }
    // get destination blob client
    BlobContainerClient destBlobContainerClient = client.get().getBlobContainerClient(destinationPath.getContainer());
    if (!destBlobContainerClient.exists()) {
        client.get().createBlobContainer(destinationPath.getContainer());
        LOG.info("Created a container called {}", destinationPath.getContainer());
    }
    BlobClient destBlobClient = destBlobContainerClient.getBlobClient(destinationPath.getBlob());
    destBlobClient.copyFromUrl(srcBlobClient.getBlobUrl() + generateSasToken());
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobClient(com.azure.storage.blob.BlobClient) FileNotFoundException(java.io.FileNotFoundException) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Example 3 with BlobContainerClient

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

the class AzureBlobStoreFileSystem method open.

@Override
protected ReadableByteChannel open(AzfsResourceId resourceId) throws IOException {
    BlobContainerClient containerClient = client.get().getBlobContainerClient(resourceId.getContainer());
    if (!containerClient.exists()) {
        throw new FileNotFoundException("The requested file doesn't exist.");
    }
    BlobClient blobClient = containerClient.getBlobClient(resourceId.getBlob());
    if (!blobClient.exists()) {
        throw new FileNotFoundException("The requested file doesn't exist.");
    }
    LOG.info("Creating a ReadableByteChannel for {}", resourceId);
    return new AzureReadableSeekableByteChannel(blobClient);
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobClient(com.azure.storage.blob.BlobClient) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with BlobContainerClient

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

the class AzureBlobStoreFileSystemTest method testGlobExpansion.

@Test
@Ignore
public void testGlobExpansion() throws IOException {
    // TODO: Write this test with mocks - see GcsFileSystemTest
    String container = "test-container" + randomUUID();
    BlobContainerClient blobContainerClient = azureBlobStoreFileSystem.getClient().createBlobContainer(container);
    // Create files
    List<String> blobNames = new ArrayList<>();
    blobNames.add("testdirectory/file1name");
    blobNames.add("testdirectory/file2name");
    blobNames.add("testdirectory/file3name");
    blobNames.add("testdirectory/otherfile");
    blobNames.add("testotherdirectory/file4name");
    for (String blob : blobNames) {
        blobContainerClient.getBlobClient(blob).uploadFromFile("src/test/resources/in.txt");
    }
    // Test patterns
    {
        AzfsResourceId pattern = AzfsResourceId.fromUri("azfs://account/" + container + "/testdirectory/file*");
        List<String> expectedFiles = ImmutableList.of("azfs://account/" + container + "/testdirectory/file1name", "azfs://account/" + container + "/testdirectory/file2name", "azfs://account/" + container + "/testdirectory/file3name");
        assertThat(expectedFiles, contains(toFilenames(azureBlobStoreFileSystem.expand(pattern)).toArray()));
    }
    {
        AzfsResourceId pattern = AzfsResourceId.fromUri("azfs://account/" + container + "/testdirectory/file[1-3]*");
        List<String> expectedFiles = ImmutableList.of("azfs://account/" + container + "/testdirectory/file1name", "azfs://account/" + container + "/testdirectory/file2name", "azfs://account/" + container + "/testdirectory/file3name");
        assertThat(expectedFiles, contains(toFilenames(azureBlobStoreFileSystem.expand(pattern)).toArray()));
    }
    {
        AzfsResourceId pattern = AzfsResourceId.fromUri("azfs://account/" + container + "/testdirectory/file?name");
        List<String> expectedFiles = ImmutableList.of("azfs://account/" + container + "/testdirectory/file1name", "azfs://account/" + container + "/testdirectory/file2name", "azfs://account/" + container + "/testdirectory/file3name");
        assertThat(expectedFiles, contains(toFilenames(azureBlobStoreFileSystem.expand(pattern)).toArray()));
    }
    {
        AzfsResourceId pattern = AzfsResourceId.fromUri("azfs://account/" + container + "/test*ectory/fi*name");
        List<String> expectedFiles = ImmutableList.of("azfs://account/" + container + "/testdirectory/file1name", "azfs://account/" + container + "/testdirectory/file2name", "azfs://account/" + container + "/testdirectory/file3name", "azfs://account/" + container + "/testotherdirectory/file4name");
        assertThat(expectedFiles, contains(toFilenames(azureBlobStoreFileSystem.expand(pattern)).toArray()));
    }
    // Clean up
    blobContainerClient.delete();
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with BlobContainerClient

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

the class AzureBlobStoreFileSystemTest method testMatch.

@Test
@Ignore
public void testMatch() throws Exception {
    // TODO: Write this test with mocks - see GcsFileSystemTest
    String container = "test-container" + randomUUID();
    BlobContainerClient blobContainerClient = azureBlobStoreFileSystem.getClient().createBlobContainer(container);
    // Create files
    List<String> blobNames = new ArrayList<>();
    blobNames.add("testdirectory/file1name");
    blobNames.add("testdirectory/file2name");
    blobNames.add("testdirectory/file3name");
    blobNames.add("testdirectory/file4name");
    blobNames.add("testdirectory/otherfile");
    blobNames.add("testotherdirectory/anotherfile");
    for (String blob : blobNames) {
        blobContainerClient.getBlobClient(blob).uploadFromFile("src/test/resources/in.txt");
    }
    List<String> specs = ImmutableList.of("azfs://account/" + container + "/testdirectory/file[1-3]*", "azfs://account/" + container + "/testdirectory/non-exist-file", "azfs://account/" + container + "/testdirectory/otherfile");
    List<MatchResult> matchResults = azureBlobStoreFileSystem.match(specs);
    // Confirm that match results are as expected
    assertEquals(3, matchResults.size());
    assertEquals(MatchResult.Status.OK, matchResults.get(0).status());
    assertThat(ImmutableList.of("azfs://account/" + container + "/testdirectory/file1name", "azfs://account/" + container + "/testdirectory/file2name", "azfs://account/" + container + "/testdirectory/file3name"), contains(toFilenames(matchResults.get(0)).toArray()));
    assertEquals(MatchResult.Status.NOT_FOUND, matchResults.get(1).status());
    assertEquals(MatchResult.Status.OK, matchResults.get(2).status());
    assertThat(ImmutableList.of("azfs://account/" + container + "/testdirectory/otherfile"), 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

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