use of com.microsoft.azure.storage.blob.CloudBlobClient in project crate by crate.
the class AzureStorageService method deleteBlob.
public void deleteBlob(String container, String blob) throws URISyntaxException, StorageException {
final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
// Container name must be lower case.
final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
LOGGER.trace(() -> new ParameterizedMessage("delete blob for container [{}], blob [{}]", container, blob));
final CloudBlockBlob azureBlob = blobContainer.getBlockBlobReference(blob);
LOGGER.trace(() -> new ParameterizedMessage("container [{}]: blob [{}] found. removing.", container, blob));
azureBlob.delete(DeleteSnapshotsOption.NONE, null, null, client.v2().get());
}
use of com.microsoft.azure.storage.blob.CloudBlobClient in project crate by crate.
the class AzureStorageService method writeBlob.
public void writeBlob(String container, String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) throws URISyntaxException, StorageException, IOException {
LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {})", blobName, blobSize));
final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
final CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName);
try {
final AccessCondition accessCondition = failIfAlreadyExists ? AccessCondition.generateIfNotExistsCondition() : AccessCondition.generateEmptyCondition();
blob.upload(inputStream, blobSize, accessCondition, null, client.v2().get());
} catch (final StorageException se) {
if (failIfAlreadyExists && se.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT && StorageErrorCodeStrings.BLOB_ALREADY_EXISTS.equals(se.getErrorCode())) {
throw new FileAlreadyExistsException(blobName, null, se.getMessage());
}
throw se;
}
LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {}) - done", blobName, blobSize));
}
use of com.microsoft.azure.storage.blob.CloudBlobClient in project crate by crate.
the class AzureStorageService method children.
public Set<String> children(String account, String container, BlobPath path) throws URISyntaxException, StorageException {
final var blobsBuilder = new HashSet<String>();
final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
final String keyPath = path.buildAsString();
final EnumSet<BlobListingDetails> enumBlobListingDetails = EnumSet.of(BlobListingDetails.METADATA);
for (ListBlobItem blobItem : blobContainer.listBlobs(keyPath, false, enumBlobListingDetails, null, client.v2().get())) {
if (blobItem instanceof CloudBlobDirectory) {
final URI uri = blobItem.getUri();
LOGGER.trace(() -> new ParameterizedMessage("blob url [{}]", uri));
// uri.getPath is of the form /container/keyPath.* and we want to strip off the /container/
// this requires 1 + container.length() + 1, with each 1 corresponding to one of the /.
// Lastly, we add the length of keyPath to the offset to strip this container's path.
final String uriPath = uri.getPath();
blobsBuilder.add(uriPath.substring(1 + container.length() + 1 + keyPath.length(), uriPath.length() - 1));
}
}
return Set.copyOf(blobsBuilder);
}
use of com.microsoft.azure.storage.blob.CloudBlobClient in project crate by crate.
the class AzureStorageService method blobExists.
public boolean blobExists(String container, String blob) throws URISyntaxException, StorageException {
// Container name must be lower case.
final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
final CloudBlockBlob azureBlob = blobContainer.getBlockBlobReference(blob);
return azureBlob.exists(null, null, client.v2().get());
}
use of com.microsoft.azure.storage.blob.CloudBlobClient in project crate by crate.
the class AzureStorageService method getInputStream.
public InputStream getInputStream(String container, String blob) throws URISyntaxException, StorageException, IOException {
final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
final CloudBlockBlob blockBlobReference = client.v1().getContainerReference(container).getBlockBlobReference(blob);
LOGGER.trace(() -> new ParameterizedMessage("reading container [{}], blob [{}]", container, blob));
return blockBlobReference.openInputStream(null, null, client.v2().get());
}
Aggregations