Search in sources :

Example 1 with BlobUrlParts

use of com.azure.storage.blob.BlobUrlParts in project azure-vm-agents-plugin by jenkinsci.

the class IntegrationTest method containerExists.

protected boolean containerExists(String storageURI) {
    try {
        BlobUrlParts blobUrlParts = BlobUrlParts.parse(storageURI);
        String storageAccountName = blobUrlParts.getAccountName();
        final String containerName = blobUrlParts.getBlobContainerName();
        StorageAccount storageAccount = azureClient.storageAccounts().getByResourceGroup(testEnv.azureResourceGroup, storageAccountName);
        List<StorageAccountKey> storageKeys = storageAccount.getKeys();
        if (storageKeys.isEmpty()) {
            return false;
        } else {
            String storageAccountKey = storageKeys.get(0).value();
            BlobServiceClient account = new BlobServiceClientBuilder().credential(new StorageSharedKeyCredential(storageAccountName, storageAccountKey)).endpoint(storageAccount.endPoints().primary().blob()).buildClient();
            BlobContainerClient blobClient = account.getBlobContainerClient(containerName);
            return blobClient.exists();
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        return false;
    }
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobUrlParts(com.azure.storage.blob.BlobUrlParts) StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) StorageAccountKey(com.azure.resourcemanager.storage.models.StorageAccountKey) BlobServiceClient(com.azure.storage.blob.BlobServiceClient) BlobServiceClientBuilder(com.azure.storage.blob.BlobServiceClientBuilder) StorageSharedKeyCredential(com.azure.storage.common.StorageSharedKeyCredential) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) ManagementException(com.azure.core.management.exception.ManagementException) IOException(java.io.IOException)

Example 2 with BlobUrlParts

use of com.azure.storage.blob.BlobUrlParts in project azure-vm-agents-plugin by jenkinsci.

the class AzureVMManagementServiceDelegate method removeStorageBlob.

public void removeStorageBlob(URI blobURI, String resourceGroupName) throws Exception {
    // Obtain container, storage account, and blob name
    BlobUrlParts blobUrlParts = BlobUrlParts.parse(blobURI.toURL());
    String storageAccountName = blobUrlParts.getAccountName();
    String containerName = blobUrlParts.getBlobContainerName();
    String blobName = blobUrlParts.getBlobName();
    LOGGER.log(Level.INFO, "removeStorageBlob: Removing blob {0}, in container {1} of storage account {2}", new Object[] { blobName, containerName, storageAccountName });
    BlobContainerClient container = getCloudBlobContainer(azureClient, resourceGroupName, storageAccountName, containerName);
    container.getBlobClient(blobName).delete();
    if (containerName.startsWith("jnk")) {
        PagedIterable<BlobItem> blobs = container.listBlobs();
        if (blobs.iterator().hasNext()) {
            // the container is not empty
            return;
        }
        // the container is empty and we should delete it
        LOGGER.log(Level.INFO, "removeStorageBlob: Removing empty container ", containerName);
        container.delete();
    }
}
Also used : BlobItem(com.azure.storage.blob.models.BlobItem) BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobUrlParts(com.azure.storage.blob.BlobUrlParts)

Example 3 with BlobUrlParts

use of com.azure.storage.blob.BlobUrlParts in project azure-vm-agents-plugin by jenkinsci.

the class IntegrationTest method blobExists.

protected boolean blobExists(String storageURI) {
    try {
        BlobUrlParts blobUrlParts = BlobUrlParts.parse(storageURI);
        String storageAccountName = blobUrlParts.getAccountName();
        final String containerName = blobUrlParts.getBlobContainerName();
        final String blobName = blobUrlParts.getBlobName();
        StorageAccount storageAccount = azureClient.storageAccounts().getByResourceGroup(testEnv.azureResourceGroup, storageAccountName);
        List<StorageAccountKey> storageKeys = storageAccount.getKeys();
        if (storageKeys.isEmpty()) {
            return false;
        } else {
            String storageAccountKey = storageKeys.get(0).value();
            BlobServiceClient account = new BlobServiceClientBuilder().credential(new StorageSharedKeyCredential(storageAccountName, storageAccountKey)).endpoint(storageAccount.endPoints().primary().blob()).buildClient();
            BlobContainerClient blobClient = account.getBlobContainerClient(containerName);
            return blobClient.getBlobClient(blobName).exists();
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        return false;
    }
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobUrlParts(com.azure.storage.blob.BlobUrlParts) StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) StorageAccountKey(com.azure.resourcemanager.storage.models.StorageAccountKey) BlobServiceClient(com.azure.storage.blob.BlobServiceClient) BlobServiceClientBuilder(com.azure.storage.blob.BlobServiceClientBuilder) StorageSharedKeyCredential(com.azure.storage.common.StorageSharedKeyCredential) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) ManagementException(com.azure.core.management.exception.ManagementException) IOException(java.io.IOException)

Aggregations

BlobContainerClient (com.azure.storage.blob.BlobContainerClient)3 BlobUrlParts (com.azure.storage.blob.BlobUrlParts)3 ManagementException (com.azure.core.management.exception.ManagementException)2 StorageAccount (com.azure.resourcemanager.storage.models.StorageAccount)2 StorageAccountKey (com.azure.resourcemanager.storage.models.StorageAccountKey)2 BlobServiceClient (com.azure.storage.blob.BlobServiceClient)2 BlobServiceClientBuilder (com.azure.storage.blob.BlobServiceClientBuilder)2 StorageSharedKeyCredential (com.azure.storage.common.StorageSharedKeyCredential)2 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)2 IOException (java.io.IOException)2 BlobItem (com.azure.storage.blob.models.BlobItem)1