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);
}
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);
}
}
}
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;
}
}
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())));
}
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);
}
Aggregations