Search in sources :

Example 1 with BlobItemProperties

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

PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 BlobItem (com.azure.storage.blob.models.BlobItem)1 BlobItemProperties (com.azure.storage.blob.models.BlobItemProperties)1 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)1 ListBlobsOptions (com.azure.storage.blob.models.ListBlobsOptions)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1