Search in sources :

Example 1 with BlobProperties

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

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

the class BlobStorageOperations method getBlobInfo.

/**
 * Get blob information/properties
 * @param containerName - the container name
 * @param blobName - the blob name
 * @throws AtsBlobStorageException - if exception occurred
 */
@PublicAtsApi
public BlobInfo getBlobInfo(String containerName, String blobName) {
    try {
        log.info("Getting info for blob '" + blobName + "' from container '" + containerName + "' ...");
        BlobProperties props = serviceClient.getBlobContainerClient(containerName).getBlobClient(blobName).getProperties();
        return new BlobInfo(containerName, blobName, props);
    } catch (Exception e) {
        if (ExceptionUtils.containsMessage("Status code 404, (empty body)", e, true)) {
            throw new AtsBlobStorageException("Blob '" + blobName + "' does not exist in container '" + containerName + "'", e);
        } else {
            throw new AtsBlobStorageException("Could not get blob info for blob '" + blobName + "' in container '" + containerName + "'", e);
        }
    }
}
Also used : BlobProperties(com.azure.storage.blob.models.BlobProperties) BlobStorageException(com.azure.storage.blob.models.BlobStorageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 3 with BlobProperties

use of com.azure.storage.blob.models.BlobProperties in project ambry by linkedin.

the class AzureBlobDataAccessor method updateBlobMetadata.

/**
 * Update the metadata for the specified blob.
 * @param blobId The {@link BlobId} to update.
 * @param updateFields Map of field names and new values to modify.
 * @param cloudUpdateValidator {@link CloudUpdateValidator} validator for the update passed by the caller.
 * @return a {@link AzureCloudDestination.UpdateResponse} with the updated metadata.
 * @throws BlobStorageException if the blob does not exist or an error occurred.
 * @throws IllegalStateException on request timeout.
 */
public AzureCloudDestination.UpdateResponse updateBlobMetadata(BlobId blobId, Map<String, Object> updateFields, CloudUpdateValidator cloudUpdateValidator) throws Exception {
    Objects.requireNonNull(blobId, "BlobId cannot be null");
    updateFields.keySet().forEach(field -> Objects.requireNonNull(updateFields.get(field), String.format("%s cannot be null", field)));
    try {
        Timer.Context storageTimer = azureMetrics.blobUpdateTime.time();
        try {
            BlobProperties blobProperties = storageClient.getPropertiesWithResponse(blobId, defaultRequestConditions, requestTimeout);
            // Note: above throws 404 exception if blob does not exist.
            String etag = blobProperties.getETag();
            Map<String, String> metadata = blobProperties.getMetadata();
            if (!cloudUpdateValidator.validateUpdate(CloudBlobMetadata.fromMap(metadata), blobId, updateFields)) {
                return new AzureCloudDestination.UpdateResponse(false, metadata);
            }
            // Update only if any of the values have changed
            Map<String, String> changedFields = updateFields.entrySet().stream().filter(entry -> !String.valueOf(entry.getValue()).equals(metadata.get(entry.getKey()))).collect(Collectors.toMap(Map.Entry::getKey, entry -> String.valueOf(entry.getValue())));
            if (changedFields.size() > 0) {
                changedFields.forEach(metadata::put);
                if (updateCallback != null) {
                    try {
                        updateCallback.call();
                    } catch (Exception ex) {
                        logger.error("Error in update callback", ex);
                    }
                }
                // Set condition to ensure we don't clobber a concurrent update
                BlobRequestConditions blobRequestConditions = new BlobRequestConditions().setIfMatch(etag);
                storageClient.setMetadataWithResponse(blobId, metadata, blobRequestConditions, requestTimeout, Context.NONE);
                return new AzureCloudDestination.UpdateResponse(true, metadata);
            } else {
                return new AzureCloudDestination.UpdateResponse(false, metadata);
            }
        } finally {
            storageTimer.stop();
        }
    } catch (BlobStorageException e) {
        if (isNotFoundError(e.getErrorCode())) {
            logger.warn("Blob {} not found, cannot update {}.", blobId, updateFields.keySet());
        }
        if (e.getErrorCode() == BlobErrorCode.CONDITION_NOT_MET) {
            azureMetrics.blobUpdateConflictCount.inc();
        }
        throw e;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Context(com.azure.core.util.Context) BlobStorageException(com.azure.storage.blob.models.BlobStorageException) LoggerFactory(org.slf4j.LoggerFactory) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) CloudConfig(com.github.ambry.config.CloudConfig) ProxyOptions(com.azure.core.http.ProxyOptions) CloudUpdateValidator(com.github.ambry.cloud.CloudUpdateValidator) BlobRequestConditions(com.azure.storage.blob.models.BlobRequestConditions) Duration(java.time.Duration) Map(java.util.Map) BlobBatchClient(com.azure.storage.blob.batch.BlobBatchClient) BlobLayout(com.github.ambry.cloud.azure.AzureBlobLayoutStrategy.BlobLayout) OutputStream(java.io.OutputStream) BlobProperties(com.azure.storage.blob.models.BlobProperties) Logger(org.slf4j.Logger) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) BlobServiceClient(com.azure.storage.blob.BlobServiceClient) BlobErrorCode(com.azure.storage.blob.models.BlobErrorCode) List(java.util.List) Timer(com.codahale.metrics.Timer) CloudBlobMetadata(com.github.ambry.cloud.CloudBlobMetadata) Response(com.azure.core.http.rest.Response) BlobId(com.github.ambry.commons.BlobId) InputStream(java.io.InputStream) Timer(com.codahale.metrics.Timer) BlobProperties(com.azure.storage.blob.models.BlobProperties) BlobRequestConditions(com.azure.storage.blob.models.BlobRequestConditions) Map(java.util.Map) BlobStorageException(com.azure.storage.blob.models.BlobStorageException) BlobStorageException(com.azure.storage.blob.models.BlobStorageException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 4 with BlobProperties

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

the class AzureBlobStoreFileSystem method toMatchResult.

private MatchResult toMatchResult(AzfsResourceId path) {
    BlobClient blobClient = client.get().getBlobContainerClient(path.getContainer()).getBlobClient(path.getBlob());
    BlobProperties blobProperties;
    try {
        blobProperties = blobClient.getProperties();
    } catch (BlobStorageException e) {
        if (e.getStatusCode() == 404) {
            return MatchResult.create(MatchResult.Status.NOT_FOUND, new FileNotFoundException());
        }
        return MatchResult.create(MatchResult.Status.ERROR, new IOException(e));
    }
    return MatchResult.create(MatchResult.Status.OK, ImmutableList.of(toMetadata(path.withSize(blobProperties.getBlobSize()).withLastModified(Date.from(blobProperties.getLastModified().toInstant())), blobProperties.getContentEncoding(), blobProperties.getETag())));
}
Also used : BlobClient(com.azure.storage.blob.BlobClient) BlobProperties(com.azure.storage.blob.models.BlobProperties) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BlobStorageException(com.azure.storage.blob.models.BlobStorageException)

Example 5 with BlobProperties

use of com.azure.storage.blob.models.BlobProperties in project ambry by linkedin.

the class AzureBlobDataAccessor method getBlobMetadata.

/**
 * Retrieve the metadata for the specified blob.
 * @param blobId The {@link BlobId} to retrieve.
 * @return The {@link CloudBlobMetadata} if the blob was found, or null otherwise.
 * @throws BlobStorageException
 */
public CloudBlobMetadata getBlobMetadata(BlobId blobId) throws BlobStorageException {
    BlobProperties blobProperties = null;
    try {
        blobProperties = storageClient.getPropertiesWithResponse(blobId, defaultRequestConditions, requestTimeout);
        if (blobProperties == null) {
            logger.debug("Blob {} not found.", blobId);
            return null;
        }
    } catch (BlobStorageException e) {
        if (isNotFoundError(e.getErrorCode())) {
            logger.debug("Blob {} not found.", blobId);
            return null;
        }
        throw e;
    }
    Map<String, String> metadata = blobProperties.getMetadata();
    return CloudBlobMetadata.fromMap(metadata);
}
Also used : BlobProperties(com.azure.storage.blob.models.BlobProperties) BlobStorageException(com.azure.storage.blob.models.BlobStorageException)

Aggregations

BlobProperties (com.azure.storage.blob.models.BlobProperties)9 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)5 IOException (java.io.IOException)3 BlobClient (com.azure.storage.blob.BlobClient)2 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)2 CloudBlobMetadata (com.github.ambry.cloud.CloudBlobMetadata)2 BlobId (com.github.ambry.commons.BlobId)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Pattern (java.util.regex.Pattern)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 ProxyOptions (com.azure.core.http.ProxyOptions)1 Response (com.azure.core.http.rest.Response)1 Context (com.azure.core.util.Context)1 BlobServiceClient (com.azure.storage.blob.BlobServiceClient)1 BlobBatchClient (com.azure.storage.blob.batch.BlobBatchClient)1 BlobErrorCode (com.azure.storage.blob.models.BlobErrorCode)1 BlobItem (com.azure.storage.blob.models.BlobItem)1