Java Code Examples for com.microsoft.azure.storage.blob.CloudBlobContainer#getDirectoryReference()

The following examples show how to use com.microsoft.azure.storage.blob.CloudBlobContainer#getDirectoryReference() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 7 votes vote down vote up
private void deleteBlobsInDirectory(CloudBlobContainer cloudBlobContainer, String directoryName)
        throws URISyntaxException, StorageException {

    CloudBlobDirectory blobDirectory = cloudBlobContainer.getDirectoryReference(directoryName);
    for (ListBlobItem blobItem : blobDirectory.listBlobs()) {
        if (blobItem instanceof CloudBlobDirectory) {
            deleteBlobsInDirectory(cloudBlobContainer, ((CloudBlobDirectory) blobItem).getPrefix());
        } else if (blobItem instanceof CloudPageBlob) {
            CloudPageBlob cloudPageBlob = cloudBlobContainer.getPageBlobReference(((CloudPageBlob) blobItem).getName());
            cloudPageBlob.deleteIfExists();
        } else if (blobItem instanceof CloudBlockBlob) {
            CloudBlockBlob cloudBlockBlob = cloudBlobContainer.getBlockBlobReference(((CloudBlockBlob) blobItem).getName());
            cloudBlockBlob.deleteIfExists();
        }
    }
}
 
Example 2
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 7 votes vote down vote up
private void listBlobsInDirectory(CloudBlobContainer cloudBlobContainer, String directoryName)
        throws URISyntaxException, StorageException {

    CloudBlobDirectory blobDirectory = cloudBlobContainer.getDirectoryReference(directoryName);

    for (ListBlobItem blobItem : blobDirectory.listBlobs()) {
        if (blobItem instanceof CloudBlobDirectory) {
            listBlobsInDirectory(cloudBlobContainer, ((CloudBlobDirectory) blobItem).getPrefix());
        } else if (blobItem instanceof CloudPageBlob) {
            Log.log(LOGGER, format(" Azure Adls Gen 2 Cloud Page Blob is present with Name: [%s] and with bytes of content: [%d] at URI: [%s] ",
                    ((CloudPageBlob) blobItem).getName(), ((CloudPageBlob) blobItem).getProperties().getLength(), blobItem.getUri().getPath()));
        } else if (blobItem instanceof CloudBlockBlob) {
            Log.log(LOGGER, format(" Azure Adls Gen 2 Cloud Block Blob is present with Name: [%s] and with bytes of content: [%d] at URI: [%s] ",
                    ((CloudBlockBlob) blobItem).getName(), ((CloudBlockBlob) blobItem).getProperties().getLength(), blobItem.getUri().getPath()));
        } else {
            LOGGER.error("Azure Adls Gen 2 Cloud Storage Item that is present at URI: [{}] cannot be classify as CloudBlob, CloudPageBlob and " +
                    "CloudBlockBlob. ", blobItem.getUri().getPath());
            throw new TestFailException(String.format("Azure Adls Gen 2 Cloud Storage Item that is present at URI: [%s] cannot be classify as" +
                    " CloudBlob, CloudPageBlob and CloudBlockBlob. ", blobItem.getUri().getPath()));
        }
    }
}
 
Example 3
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 7 votes vote down vote up
private void listBlobsInDirectoryWithValidation(CloudBlobContainer cloudBlobContainer, String directoryName, Boolean zeroContent)
        throws URISyntaxException, StorageException {

    CloudBlobDirectory blobDirectory = cloudBlobContainer.getDirectoryReference(directoryName);
    Set<String> blobsWithZeroLength = new HashSet<>();

    for (ListBlobItem blobItem : blobDirectory.listBlobs()) {
        if (blobItem instanceof CloudBlobDirectory) {
            listBlobsInDirectoryWithValidation(cloudBlobContainer, ((CloudBlobDirectory) blobItem).getPrefix(), zeroContent);
        } else if (blobItem instanceof CloudPageBlob) {
            validateBlobItemLength(blobItem, zeroContent, blobsWithZeroLength);
        } else if (blobItem instanceof CloudBlockBlob) {
            validateBlobItemLength(blobItem, zeroContent, blobsWithZeroLength);
        } else {
            LOGGER.error("Azure Adls Gen 2 Cloud Storage Item that is present at URI: {} cannot be classify as CloudBlob, CloudPageBlob and " +
                    "CloudBlockBlob. ", blobItem.getUri().getPath());
            throw new TestFailException(String.format("Azure Adls Gen 2 Cloud Storage Item that is present at URI: %s cannot be classify as" +
                    " CloudBlob, CloudPageBlob and CloudBlockBlob. ", blobItem.getUri().getPath()));
        }
    }
}
 
Example 4
Source File: AzureResource.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isDirectory() {
   try {
      CloudBlobContainer blobContainer = blobClient.getContainerReference(this.container);

      if (blobContainer.exists()) {
         CloudBlobDirectory blockBlobDir = blobContainer.getDirectoryReference(this.blob);
         // Blob Directories don't exists unless they have something underneath
         return blockBlobDir.listBlobs().iterator().hasNext();
      }
   }
   catch (Exception e) {
      logger.debug("isDirectory failed to lookup URI - [{}]", getUri(), e);
   }
   return false;
}
 
Example 5
Source File: VideoStorageAzure.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
public VideoStorageSession create(UUID recordingId, UUID cameraId, UUID accountId, UUID placeId, @Nullable UUID personId, long ttlInSeconds) throws Exception {
   long startTime = System.nanoTime();

   try {
      CloudBlobContainer container = getRandomContainer(recordingId, cameraId, placeId);
      CloudBlobDirectory dir = container.getDirectoryReference(placeId.toString());
      CloudAppendBlob blob = dir.getAppendBlobReference(recordingId.toString());

      blob.getProperties().setCacheControl("no-cache");
      blob.getProperties().setContentType("video/mp2t");

      blob.setStreamWriteSizeInBytes(4*1024*1024);
      VideoStorageSession result = new AzureStorageSession(recordingId, cameraId, accountId, placeId, ttlInSeconds, personId,  blob);

      VIDEO_STORAGE_AZURE_CREATE_SUCCESS.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
      return result;
   } catch (Exception ex) {
      VIDEO_STORAGE_AZURE_CREATE_FAIL.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
      throw ex;
   }
}
 
Example 6
Source File: AzureStorageRepository.java    From hawkbit-extensions with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void deleteByTenant(final String tenant) {

    try {
        final CloudBlobContainer container = getContainer();
        final CloudBlobDirectory tenantDirectory = container.getDirectoryReference(sanitizeTenant(tenant));

        LOG.info("Deleting Azure Storage blob folder (tenant) from container {} for tenant {}", container.getName(),
                tenant);

        final ResultSegment<ListBlobItem> blobs = tenantDirectory.listBlobsSegmented();
        ResultContinuation token = null;
        do {
            token = blobs.getContinuationToken();
            blobs.getResults().stream().filter(CloudBlob.class::isInstance).map(CloudBlob.class::cast)
                    .forEach(this::deleteBlob);
        } while (token != null);

    } catch (final URISyntaxException | StorageException e) {
        throw new ArtifactStoreException("Failed to delete tenant directory from Azure storage", e);
    }
}
 
Example 7
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public SdxTestDto deleteAllFolders(TestContext testContext, SdxTestDto sdxTestDto, SdxClient sdxClient) {
    String containerName = getContainerName(sdxTestDto.getRequest().getCloudStorage().getBaseLocation());
    CloudBlobContainer cloudBlobContainer = getCloudBlobContainer(containerName);

    try {
        for (ListBlobItem blob : cloudBlobContainer.listBlobs()) {
            String blobName = blob.getUri().getPath().split("/", 3)[2];
            String blobUriPath = blob.getUri().getPath();

            if (blob instanceof CloudBlob) {
                ((CloudBlob) blob).deleteIfExists();
            } else {
                if (blobName.endsWith("/")) {
                    blobName = blobName.replaceAll(".$", "");
                }
                CloudBlobDirectory blobDirectory = cloudBlobContainer.getDirectoryReference(blobName);
                deleteBlobsInDirectory(cloudBlobContainer, blobDirectory.getPrefix());
            }
        }
    } catch (StorageException | URISyntaxException e) {
        LOGGER.error("Azure Adls Gen 2 Blob couldn't process the call. So it has been returned with error!", e);
        throw new TestFailException(String.format("Azure Adls Gen 2 Blob couldn't process the call. So it has been returned the error: %s", e));
    }

    return sdxTestDto;
}
 
Example 8
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public void deleteAllFolders() throws StorageException, URISyntaxException {
    String containerName = getContainerName();
    CloudBlobContainer cloudBlobContainer = getCloudBlobContainer(containerName);

    try {
        for (ListBlobItem blob : cloudBlobContainer.listBlobs()) {
            String blobName = blob.getUri().getPath().split("/", 3)[2];
            String blobUriPath = blob.getUri().getPath();

            if (blob instanceof CloudBlob) {
                ((CloudBlob) blob).deleteIfExists();
            } else {
                if (blobName.endsWith("/")) {
                    blobName = blobName.replaceAll(".$", "");
                }
                CloudBlobDirectory blobDirectory = cloudBlobContainer.getDirectoryReference(blobName);
                deleteBlobsInDirectory(cloudBlobContainer, blobDirectory.getPrefix());
            }
        }
    } catch (StorageException | URISyntaxException e) {
        LOGGER.error("Azure Adls Gen 2 Blob couldn't process the call. So it has been returned with error!", e);
        throw e;
    }
}
 
Example 9
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public void listAllFolders(String baseLocation) {
    String containerName = getContainerName(baseLocation);
    CloudBlobContainer cloudBlobContainer = getCloudBlobContainer(containerName);

    Log.log(LOGGER, format(" Azure Blob Storage URI: %s", cloudBlobContainer.getStorageUri()));
    Log.log(LOGGER, format(" Azure Blob Container: %s", cloudBlobContainer.getName()));

    try {
        for (ListBlobItem blob : cloudBlobContainer.listBlobs()) {
            String blobName = blob.getUri().getPath().split("/", 3)[2];
            String blobUriPath = blob.getUri().getPath();

            if (blob instanceof CloudBlob) {
                if (((CloudBlob) blob).exists()) {
                    Log.log(LOGGER, format(" Azure Adls Gen 2 Blob is present with Name: %s and with bytes of content: %d at URI: %s ",
                            ((CloudBlob) blob).getName(), ((CloudBlob) blob).getProperties().getLength(), blobUriPath));
                }
            } else {
                if (blobName.endsWith("/")) {
                    blobName = blobName.replaceAll(".$", "");
                }
                CloudBlobDirectory blobDirectory = cloudBlobContainer.getDirectoryReference(blobName);
                listBlobsInDirectory(cloudBlobContainer, blobDirectory.getPrefix());
            }
        }
    } catch (StorageException | URISyntaxException e) {
        LOGGER.error("Azure Adls Gen 2 Blob couldn't process the call. So it has been returned with error!", e);
        throw new TestFailException(String.format("Azure Adls Gen 2 Blob couldn't process the call. So it has been returned the error: %s", e));
    }
}
 
Example 10
Source File: VideoStorageAzure.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
public VideoStorageSession create(VideoRecording recording) throws Exception {
   long startTime = System.nanoTime();

   try {
      if (recording == null || recording.storage == null) {
         VIDEO_STORAGE_AZURE_NO_LOCATION.inc();
         throw new Exception("no storage location");
      }

      CloudBlobContainer foundContainer = null;
      for (CloudBlobContainer container : containers) {
         URI uri = container.getUri();
         String uriString = uri.toString();
         if (recording.storage.startsWith(uriString)) {
            foundContainer = container;
            break;
         }
      }

      if (foundContainer == null) {
         VIDEO_STORAGE_AZURE_NO_CONTAINER.inc();
         throw new Exception("no azure container for storage location");
      }

      CloudBlobDirectory dir = foundContainer.getDirectoryReference(recording.placeId.toString());
      CloudAppendBlob blob = dir.getAppendBlobReference(recording.recordingId.toString());

      blob.getProperties().setCacheControl("no-cache");
      blob.getProperties().setContentType("video/mp2t");
      long expiration = recording.expiration;
      long ttlInSeconds = VideoV2Util.createActualTTL(recording.recordingId, expiration);
      
      VideoStorageSession result = new AzureStorageSession(recording.recordingId, recording.cameraId, recording.accountId, recording.placeId, ttlInSeconds, recording.personId, blob);

      VIDEO_STORAGE_AZURE_EXISTING_CREATE_SUCCESS.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
      return result;
   } catch (Exception ex) {
      VIDEO_STORAGE_AZURE_EXISTING_CREATE_FAIL.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
      throw ex;
   }
}
 
Example 11
Source File: AzureStorageRepository.java    From hawkbit-extensions with Eclipse Public License 1.0 4 votes vote down vote up
private CloudBlockBlob getBlob(final String tenant, final String sha1Hash16)
        throws URISyntaxException, StorageException {
    final CloudBlobContainer container = getContainer();
    final CloudBlobDirectory tenantDirectory = container.getDirectoryReference(sanitizeTenant(tenant));
    return tenantDirectory.getBlockBlobReference(sha1Hash16);
}
 
Example 12
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public void listSelectedDirectory(String baseLocation, String selectedDirectory, Boolean zeroContent) {
    String containerName = getContainerName(baseLocation);
    CloudBlobContainer cloudBlobContainer = getCloudBlobContainer(containerName);

    Log.log(LOGGER, format(" Azure Blob Storage URI: %s", cloudBlobContainer.getStorageUri()));
    Log.log(LOGGER, format(" Azure Blob Container: %s", cloudBlobContainer.getName()));
    Log.log(LOGGER, format(" Azure Blob Directory: %s", selectedDirectory));

    try {
        CloudBlobDirectory logsDirectory = cloudBlobContainer.getDirectoryReference("cluster-logs");
        CloudBlobDirectory selectedLogsDirectory = logsDirectory.getDirectoryReference(selectedDirectory);
        Set<String> blobsWithZeroLength = new HashSet<>();

        Iterable<ListBlobItem> blobListing = cloudBlobContainer.listBlobs("cluster-logs/" + selectedDirectory, true);
        List<ListBlobItem> listBlobItems = StreamSupport
                .stream(blobListing.spliterator(), false)
                .collect(Collectors.toList());
        Log.log(LOGGER, format(" Azure Blob Directory: %s contains %d sub-objects.",
                selectedDirectory, listBlobItems.size()));

        for (ListBlobItem blob : selectedLogsDirectory.listBlobs()) {
            String blobName = blob.getUri().getPath().split("/", 3)[2];
            String blobUriPath = blob.getUri().getPath();

            if (blob instanceof CloudBlob) {
                if (((CloudBlob) blob).exists()) {
                    validateBlobItemLength(blob, zeroContent, blobsWithZeroLength);
                } else {
                    LOGGER.error("Azure Adls Gen 2 Blob is NOT present with Name: {} and with bytes of content: {} at URI: {}",
                            ((CloudBlob) blob).getName(), ((CloudBlob) blob).getProperties().getLength(), blobUriPath);
                    throw new TestFailException(String.format("Azure Adls Gen 2 Blob is NOT present with Name: %s", ((CloudBlob) blob).getName()));
                }
            } else {
                if (blobName.endsWith("/")) {
                    blobName = blobName.replaceAll(".$", "");
                }
                CloudBlobDirectory blobDirectory = cloudBlobContainer.getDirectoryReference(blobName);
                listBlobsInDirectoryWithValidation(cloudBlobContainer, blobDirectory.getPrefix(), zeroContent);
            }
        }
    } catch (StorageException | URISyntaxException e) {
        LOGGER.error("Azure Adls Gen 2 Blob couldn't process the call. So it has been returned with error!", e);
        throw new TestFailException(String.format("Azure Adls Gen 2 Blob couldn't process the call. So it has been returned the error: %s", e));
    }
}