Search in sources :

Example 1 with BlobItem

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

use of com.azure.storage.blob.models.BlobItem in project azure-iot-sdk-java by Azure.

the class ExportImportTests method runExportJob.

private static List<ExportImportDevice> runExportJob(Optional<StorageAuthenticationType> storageAuthenticationType) throws Exception {
    Boolean excludeKeys = false;
    String containerSasUri = getContainerSasUri(exportContainer);
    boolean exportJobScheduled = false;
    JobProperties exportJob = null;
    while (!exportJobScheduled) {
        try {
            if (storageAuthenticationType.isPresent()) {
                JobProperties exportJobProperties = JobProperties.createForExportJob(containerSasUri, excludeKeys, storageAuthenticationType.get());
                exportJob = registryManager.exportDevices(exportJobProperties);
            } else {
                exportJob = registryManager.exportDevices(containerSasUri, excludeKeys);
            }
            exportJobScheduled = true;
        } catch (IotHubTooManyDevicesException e) {
            // test is being throttled, wait a while and try again
            Thread.sleep(10 * 1000);
        }
    }
    JobProperties.JobStatus jobStatus;
    long startTime = System.currentTimeMillis();
    while (true) {
        exportJob = registryManager.getJob(exportJob.getJobId());
        jobStatus = exportJob.getStatus();
        if (jobStatus == JobProperties.JobStatus.COMPLETED || jobStatus == JobProperties.JobStatus.FAILED) {
            break;
        }
        if (System.currentTimeMillis() - startTime > EXPORT_JOB_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for the export job to complete");
        }
        Thread.sleep(100);
    }
    String exportedDevicesJson = "";
    for (BlobItem blobItem : exportContainer.listBlobs()) {
        BlobInputStream stream = exportContainer.getBlobClient(blobItem.getName()).openInputStream();
        try (Scanner scanner = new Scanner(stream, StandardCharsets.UTF_8.name())) {
            exportedDevicesJson = scanner.next();
        }
    }
    List<ExportImportDevice> result = new ArrayList<>();
    Scanner scanner = new Scanner(exportedDevicesJson);
    while (scanner.hasNextLine()) {
        String exportImportDeviceJson = scanner.nextLine();
        ExportImportDeviceParser parser = new ExportImportDeviceParser(exportImportDeviceJson);
        ExportImportDevice device = Deencapsulation.newInstance(ExportImportDevice.class, new Class[] { ExportImportDeviceParser.class }, parser);
        device.setImportMode(ImportMode.CreateOrUpdate);
        result.add(device);
    }
    scanner.close();
    if (jobStatus != JobProperties.JobStatus.COMPLETED) {
        Assert.fail("The export job was not completed successfully");
    }
    return result;
}
Also used : Scanner(java.util.Scanner) JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) ArrayList(java.util.ArrayList) BlobInputStream(com.azure.storage.blob.specialized.BlobInputStream) IotHubTooManyDevicesException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException) BlobItem(com.azure.storage.blob.models.BlobItem) ExportImportDeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser)

Example 3 with BlobItem

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

use of com.azure.storage.blob.models.BlobItem 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)4 ArrayList (java.util.ArrayList)3 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)2 ListBlobsOptions (com.azure.storage.blob.models.ListBlobsOptions)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 BlobClient (com.azure.storage.blob.BlobClient)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 BlobInputStream (com.azure.storage.blob.specialized.BlobInputStream)1 ExportImportDeviceParser (com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser)1 ExportImportDevice (com.microsoft.azure.sdk.iot.service.ExportImportDevice)1 JobProperties (com.microsoft.azure.sdk.iot.service.JobProperties)1 IotHubTooManyDevicesException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException)1 Duration (java.time.Duration)1 Scanner (java.util.Scanner)1 Pattern (java.util.regex.Pattern)1 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)1