Search in sources :

Example 1 with ListBlobsOptions

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

use of com.azure.storage.blob.models.ListBlobsOptions in project ats-framework by Axway.

the class BlobStorageOperations method listBlobs.

/**
 * List blobs from container
 * @param containerName - the container name
 * @param prefix - prefix for the blobs names or null for all blobs
 * @param directory - the directory which will be listed, or null to search the whole container
 * @param retrieveTimeout - the maximum amount of time (in seconds) to wait for the operation to complete. If the operation did not complete in that time {@link BlobStorageException} will be thrown/raised. Pass 0 (zero) to use the default value
 * @return list {@link BlobInfo}
 * @throws AtsBlobStorageException - if exception occurred
 */
@PublicAtsApi
public List<BlobInfo> listBlobs(String containerName, String prefix, String directory, long retrieveTimeout) {
    try {
        StringBuilder sb = new StringBuilder();
        sb.append("Listing blobs");
        log.info(sb.toString());
        final List<BlobInfo> infos = new ArrayList<BlobInfo>();
        PagedIterable<BlobItem> blobs = null;
        ListBlobsOptions lbops = new ListBlobsOptions();
        if (!StringUtils.isNullOrEmpty(prefix)) {
            lbops.setPrefix(prefix);
            sb.append(" with prefix '" + prefix + "'");
        }
        if (retrieveTimeout <= 0) {
            // just a little less than too much
            retrieveTimeout = Integer.MAX_VALUE / 2;
        }
        if (!StringUtils.isNullOrEmpty(directory)) {
            String newPrefix = directory + "/" + prefix;
            lbops.setPrefix(newPrefix);
            sb.append(" from directory '" + directory + "'");
            sb.append(" in container '" + containerName + "' ...");
            log.info(sb.toString());
            blobs = serviceClient.getBlobContainerClient(containerName).listBlobsByHierarchy("/", lbops, Duration.ofSeconds(retrieveTimeout));
        } else {
            sb.append(" in container '" + containerName + "' ...");
            log.info(sb.toString());
            blobs = serviceClient.getBlobContainerClient(containerName).listBlobs(lbops, Duration.ofSeconds(retrieveTimeout));
        }
        if (blobs != null) {
            blobs.stream().forEach(new Consumer<BlobItem>() {

                public void accept(BlobItem blobItem) {
                    BlobInfo info = new BlobInfo();
                    BlobItemProperties properties = blobItem.getProperties();
                    info.setAccessTier(BlobInfo.toAtsAccessTier(properties.getAccessTier()));
                    info.setBlobName(blobItem.getName());
                    info.setBlobType(BlobInfo.toAtsBlobType(properties.getBlobType()));
                    info.setContainerName(containerName);
                    info.setContentType(properties.getContentType());
                    if (properties.getCreationTime() != null) {
                        info.setCreationTime(Date.from(properties.getCreationTime().toInstant()));
                    } else {
                        info.setCreationTime(null);
                    }
                    info.setETag(properties.getETag());
                    if (properties.getLastModified() != null) {
                        info.setLastModified(Date.from(properties.getLastModified().toInstant()));
                    } else {
                        info.setLastModified(null);
                    }
                    if (properties.getContentMd5() != null) {
                        info.setMd5(java.util.Base64.getEncoder().encodeToString(properties.getContentMd5()));
                    } else {
                        info.setMd5(null);
                    }
                    info.setMetadata(blobItem.getMetadata());
                    info.setSize(properties.getContentLength());
                    infos.add(info);
                }
            });
        }
        return infos;
    } catch (Exception e) {
        String errorMessage = "Could not list blobs from container '" + containerName + "'" + (!StringUtils.isNullOrEmpty(prefix) ? " with prefix '" + prefix + "'" : "") + (!StringUtils.isNullOrEmpty(directory) ? " in directory '" + directory + "'" : "") + " in " + retrieveTimeout + " seconds";
        throw new AtsBlobStorageException(errorMessage, e);
    }
}
Also used : BlobItem(com.azure.storage.blob.models.BlobItem) ListBlobsOptions(com.azure.storage.blob.models.ListBlobsOptions) BlobItemProperties(com.azure.storage.blob.models.BlobItemProperties) ArrayList(java.util.ArrayList) BlobStorageException(com.azure.storage.blob.models.BlobStorageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

BlobItem (com.azure.storage.blob.models.BlobItem)2 ListBlobsOptions (com.azure.storage.blob.models.ListBlobsOptions)2 ArrayList (java.util.ArrayList)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)1 BlobItemProperties (com.azure.storage.blob.models.BlobItemProperties)1 BlobProperties (com.azure.storage.blob.models.BlobProperties)1 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Pattern (java.util.regex.Pattern)1 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)1