com.microsoft.azure.storage.blob.CloudBlob Java Examples

The following examples show how to use com.microsoft.azure.storage.blob.CloudBlob. 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: AzureBackuper.java    From cassandra-backup with Apache License 2.0 6 votes vote down vote up
private void deleteStaleBlobs() throws Exception {
    final Date expiryDate = Date.from(ZonedDateTime.now().minusWeeks(1).toInstant());

    final CloudBlobDirectory directoryReference = blobContainer.getDirectoryReference(request.storageLocation.clusterId + "/" + request.storageLocation.datacenterId);

    for (final ListBlobItem blob : directoryReference.listBlobs(null, true, EnumSet.noneOf(BlobListingDetails.class), null, null)) {
        if (!(blob instanceof CloudBlob)) {
            continue;
        }

        final BlobProperties properties = ((CloudBlob) blob).getProperties();
        if (properties == null || properties.getLastModified() == null) {
            continue;
        }

        if (properties.getLastModified().before(expiryDate)) {
            ((CloudBlob) blob).delete();
        }
    }
}
 
Example #2
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 #3
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 #4
Source File: LogBlobIterator.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Validates that the log given is of the correct log type.
 * 
 * @param current
 *            the current log
 * @return whether or not the log is of the correct type.
 */
private boolean isCorrectLogType(ListBlobItem current) {
    HashMap<String, String> metadata = ((CloudBlob) current).getMetadata();
    String logType = metadata.get("LogType");

    if (logType == null) {
        return true;
    }

    if (this.operations.contains(LoggingOperations.READ) && logType.contains("read")) {
        return true;
    }

    if (this.operations.contains(LoggingOperations.WRITE) && logType.contains("write")) {
        return true;
    }

    if (this.operations.contains(LoggingOperations.DELETE) && logType.contains("delete")) {
        return true;
    }

    return false;
}
 
Example #5
Source File: TestBlobService.java    From jframe with Apache License 2.0 6 votes vote down vote up
public void testDownloadBlob() {
    try {
        // Loop through each blob item in the container.
        for (ListBlobItem blobItem : container.listBlobs()) {
            // If the item is a blob, not a virtual directory.
            if (blobItem instanceof CloudBlob) {
                // Download the item and save it to a file with the same
                // name.
                CloudBlob blob = (CloudBlob) blobItem;
                blob.download(new FileOutputStream("C:\\mydownloads\\" + blob.getName()));
            }
        }
    } catch (Exception e) {
        // Output the stack trace.
        e.printStackTrace();
    }
}
 
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: AzureBlobResource.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@javax.enterprise.inject.Produces
@Named("azureBlobClient")
public CloudBlob createBlobClient() throws Exception {
    StorageCredentials credentials = StorageCredentials.tryParseCredentials(System.getProperty("azurite.credentials"));
    URI uri = new URI(System.getProperty("azurite.blob.service.url") + "camel-test/test");
    CloudBlockBlob cloudBlockBlob = new CloudBlockBlob(uri, credentials);
    return cloudBlockBlob;
}
 
Example #8
Source File: AzureConfigProvider.java    From exhibitor with Apache License 2.0 5 votes vote down vote up
private CloudBlob getConfigObject() throws Exception {
    try {
        CloudBlob object = azureClient.getBlob(arguments.getContainer(), arguments.getBlobName());
        object.downloadAttributes();
        if (object.getProperties().getLength() > 0) {
            return object;
        }
    } catch (StorageException e) {
        if (!isNotFoundError(e) && !isForbiddenError(e)) {
            throw e;
        }
    }
    return null;
}
 
Example #9
Source File: AzureStorageService.java    From crate with Apache License 2.0 5 votes vote down vote up
public Map<String, BlobMetaData> listBlobsByPrefix(String container, String keyPath, String prefix)
    throws URISyntaxException, StorageException {
    // NOTE: this should be here: if (prefix == null) prefix = "";
    // however, this is really inefficient since deleteBlobsByPrefix enumerates everything and
    // then does a prefix match on the result; it should just call listBlobsByPrefix with the prefix!
    final var blobsBuilder = new HashMap<String, BlobMetaData>();
    final EnumSet<BlobListingDetails> enumBlobListingDetails = EnumSet.of(BlobListingDetails.METADATA);
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    LOGGER.trace(() -> new ParameterizedMessage("listing container [{}], keyPath [{}], prefix [{}]", container, keyPath, prefix));
    for (final ListBlobItem blobItem : blobContainer.listBlobs(keyPath + (prefix == null ? "" : prefix), false,
        enumBlobListingDetails, null, client.v2().get())) {
        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 /
        final String blobPath = uri.getPath().substring(1 + container.length() + 1);
        if (blobItem instanceof CloudBlob) {
            final BlobProperties properties = ((CloudBlob) blobItem).getProperties();
            final String name = blobPath.substring(keyPath.length());
            LOGGER.trace(() -> new ParameterizedMessage("blob url [{}], name [{}], size [{}]", uri, name, properties.getLength()));
            blobsBuilder.put(name, new PlainBlobMetaData(name, properties.getLength()));
        }
    }

    return Map.copyOf(blobsBuilder);
}
 
Example #10
Source File: DeleteAzureBlobStorage.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();

    if(flowFile == null) {
        return;
    }

    final long startNanos = System.nanoTime();
    final String containerName = context.getProperty(AzureStorageUtils.CONTAINER).evaluateAttributeExpressions(flowFile).getValue();
    final String blobPath = context.getProperty(BLOB).evaluateAttributeExpressions(flowFile).getValue();
    final String deleteSnapshotOptions = context.getProperty(DELETE_SNAPSHOTS_OPTION).getValue();

    try {
        CloudBlobClient blobClient = AzureStorageUtils.createCloudBlobClient(context, getLogger(), flowFile);
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        CloudBlob blob = container.getBlockBlobReference(blobPath);

        final OperationContext operationContext = new OperationContext();
        AzureStorageUtils.setProxy(operationContext, context);
        blob.deleteIfExists(DeleteSnapshotsOption.valueOf(deleteSnapshotOptions), null, null, operationContext);
        session.transfer(flowFile, REL_SUCCESS);

        final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
        session.getProvenanceReporter().invokeRemoteProcess(flowFile, blob.getSnapshotQualifiedUri().toString(), "Blob deleted");
    } catch ( StorageException | URISyntaxException e) {
        getLogger().error("Failed to delete the specified blob {} from Azure Storage. Routing to failure", new Object[]{blobPath}, e);
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
    }
}
 
Example #11
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 #12
Source File: ManageLinuxWebAppStorageAccountConnection.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
private static void uploadFileToContainer(CloudBlobContainer container, String fileName, String filePath) {
    try {
        CloudBlob blob = container.getBlockBlobReference(fileName);
        blob.uploadFromFile(filePath);
    } catch (StorageException | URISyntaxException | IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #13
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void validateBlobItemLength(ListBlobItem blobItem, Boolean zeroContent, Set<String> blobsWithZeroLength) {
    if (((CloudBlob) blobItem).getProperties().getLength() == 0 && !zeroContent) {
        blobsWithZeroLength.add(((CloudBlob) blobItem).getName());
        Integer zeroBlobLengthToleration = azureProperties.getCloudstorage().getZeroBlobLengthToleration();
        if (blobsWithZeroLength.size() >= zeroBlobLengthToleration) {
            LOGGER.error("Zero blob length toleration limit ({}) reached! The following blobs has 0 bytes content: {}",
                    zeroBlobLengthToleration, StringUtils.join(blobsWithZeroLength, ", "));
            throw new TestFailException(String.format("Azure Adls Gen 2 Blob: %s has 0 bytes of content!", ((CloudBlob) blobItem).getName()));
        } else {
            LOGGER.warn(" Azure Adls Gen 2 Blob: {} has 0 bytes of content! (blobs with no content - occurrence: {}, limit: {})",
                    ((CloudBlob) blobItem).getName(), blobsWithZeroLength.size(), zeroBlobLengthToleration);
        }
    }
}
 
Example #14
Source File: ManageWebAppStorageAccountConnection.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
private static void uploadFileToContainer(CloudBlobContainer container, String fileName, String filePath) {
    try {
        CloudBlob blob = container.getBlockBlobReference(fileName);
        blob.uploadFromFile(filePath);
    } catch (StorageException | URISyntaxException | IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #15
Source File: AzureStorageListReader.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public boolean start() throws IOException {
    String mycontainer = properties.container.getValue();
    List<CloudBlob> blobs = new ArrayList<>();
    // build a list with remote blobs to fetch
    List<RemoteBlob> remoteBlobs = ((AzureStorageSource) getCurrentSource()).getRemoteBlobs();
    try {

        for (RemoteBlob rmtb : remoteBlobs) {
            for (ListBlobItem blob : azureStorageBlobService.listBlobs(mycontainer, rmtb.prefix, rmtb.include)) {
                if (blob instanceof CloudBlob) {
                    blobs.add((CloudBlob) blob);
                }
            }
        }

        startable = !blobs.isEmpty();
        blobsIterator = blobs.iterator();
    } catch (StorageException | URISyntaxException | InvalidKeyException e) {
        LOGGER.error(e.getLocalizedMessage());
        if (properties.dieOnError.getValue()) {
            throw new ComponentException(e);
        }
    }

    if (startable) {
        dataCount++;
        currentBlob = blobsIterator.next();
        IndexedRecord dataRecord = new GenericData.Record(properties.schema.schema.getValue());
        dataRecord.put(0, currentBlob.getName());
        Schema rootSchema = RootSchemaUtils.createRootSchema(properties.schema.schema.getValue(), properties.outOfBandSchema);
        currentRecord = new GenericData.Record(rootSchema);
        currentRecord.put(0, dataRecord);
        currentRecord.put(1, dataRecord);
    }
    return startable;
}
 
Example #16
Source File: BlobBasics.java    From storage-blob-java-getting-started with MIT License 5 votes vote down vote up
/**
 * Wait until the copy complete.
 *
 * @param blob Target of the copy operation
 *
 * @throws InterruptedException
 * @throws StorageException
 */
private static void waitForCopyToComplete(CloudBlob blob) throws InterruptedException, StorageException {
    CopyStatus copyStatus = CopyStatus.PENDING;
    while (copyStatus == CopyStatus.PENDING) {
        Thread.sleep(1000);
        blob.downloadAttributes();
        copyStatus = blob.getCopyState().getStatus();
    }
}
 
Example #17
Source File: AzureNativeFileSystemStore.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void purge(String prefix) throws IOException {
  try {

    // Attempts to purge may occur before opening any streams so first,
    // check if a session exists, if not create a session with the Azure
    // storage server.
    if (null == storageInteractionLayer) {
      final String errMsg = String.format(
          "Storage session expected for URI '%s' but does not exist.",
          sessionUri);
      throw new AssertionError(errMsg);
    }

    if (checkContainer(ContainerAccessType.ReadThenWrite) == ContainerState.DoesntExist) {
      // Container doesn't exist, no need to do anything.
      return;
    }
    // Get all blob items with the given prefix from the container and delete
    // them.
    Iterable<ListBlobItem> objects = listRootBlobs(prefix, false);
    for (ListBlobItem blobItem : objects) {
      ((CloudBlob) blobItem).delete(DeleteSnapshotsOption.NONE, null, null,
          getInstrumentedContext());
    }
  } catch (Exception e) {
    // Re-throw as an Azure storage exception.
    //
    throw new AzureException(e);
  }
}
 
Example #18
Source File: AzureStorageRepository.java    From hawkbit-extensions with Eclipse Public License 1.0 5 votes vote down vote up
private void deleteBlob(final CloudBlob blob) {
    try {
        blob.delete();
    } catch (final StorageException e) {
        throw new ArtifactStoreException("Failed to delete tenant directory from Azure storage", e);
    }
}
 
Example #19
Source File: AzureObjectSummariesIterator.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
public StorageObjectSummary next()
{
  ListBlobItem listBlobItem = itemIterator.next();

  if (!(listBlobItem instanceof CloudBlob))
  {
    // The only other possible type would a CloudDirectory
    // This should never happen since we are listing items as a flat list
    throw new IllegalArgumentException("Unexpected listBlobItem instace type");
  }

  return StorageObjectSummary.createFromAzureListBlobItem(listBlobItem);
}
 
Example #20
Source File: StorageObjectSummary.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
/**
 * Contructs a StorageObjectSummary object from Azure BLOB properties
 * Using factory methods to create these objects since Azure can throw,
 * while retrieving the BLOB properties
 *
 * @param listBlobItem an Azure ListBlobItem object
 * @return the ObjectSummary object created
 */
public static StorageObjectSummary createFromAzureListBlobItem(ListBlobItem listBlobItem)
throws StorageProviderException
{
  String location, key, md5;
  long size;

  // Retrieve the BLOB properties that we need for the Summary
  // Azure Storage stores metadata inside each BLOB, therefore the listBlobItem
  // will point us to the underlying BLOB and will get the properties from it
  // During the process the Storage Client could fail, hence we need to wrap the
  // get calls in try/catch and handle possible exceptions
  try
  {
    location = listBlobItem.getContainer().getName();

    CloudBlob cloudBlob = (CloudBlob) listBlobItem;
    key = cloudBlob.getName();

    BlobProperties blobProperties = cloudBlob.getProperties();
    // the content md5 property is not always the actual md5 of the file. But for here, it's only
    // used for skipping file on PUT command, hense is ok.
    md5 = convertBase64ToHex(blobProperties.getContentMD5());
    size = blobProperties.getLength();
  }
  catch (URISyntaxException | StorageException ex)
  {
    // This should only happen if somehow we got here with and invalid URI (it should never happen)
    // ...or there is a Storage service error. Unlike S3, Azure fetches metadata from the BLOB itself,
    // and its a lazy operation
    throw new StorageProviderException(ex);
  }
  return new StorageObjectSummary(location, key, md5, size);

}
 
Example #21
Source File: NativeAzureFileSystemBaseTest.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
// Acquire and free a Lease object. Wait for more than the lease
// timeout, to make sure the lease renews itself.
public void testSelfRenewingLease() throws IllegalArgumentException, IOException,
  InterruptedException, StorageException {

  SelfRenewingLease lease;
  final String FILE_KEY = "file";
  fs.create(new Path(FILE_KEY));
  NativeAzureFileSystem nfs = (NativeAzureFileSystem) fs;
  String fullKey = nfs.pathToKey(nfs.makeAbsolute(new Path(FILE_KEY)));
  AzureNativeFileSystemStore store = nfs.getStore();
  lease = store.acquireLease(fullKey);
  assertTrue(lease.getLeaseID() != null);

  // The sleep time for the keep-alive thread is 40 seconds, so sleep just
  // a little beyond that, to make sure the keep-alive thread wakes up
  // and renews the lease.
  Thread.sleep(42000);
  lease.free();

  // Check that the lease is really freed.
  CloudBlob blob = lease.getCloudBlob();

  // Try to acquire it again, using direct Azure blob access.
  // If that succeeds, then the lease was already freed.
  String differentLeaseID = null;
  try {
    differentLeaseID = blob.acquireLease(15, null);
  } catch (Exception e) {
    e.printStackTrace();
    fail("Caught exception trying to directly re-acquire lease from Azure");
  } finally {
    assertTrue(differentLeaseID != null);
    AccessCondition accessCondition = AccessCondition.generateEmptyCondition();
    accessCondition.setLeaseID(differentLeaseID);
    blob.releaseLease(accessCondition);
  }
}
 
Example #22
Source File: AzureNativeFileSystemStore.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void purge(String prefix) throws IOException {
  try {

    // Attempts to purge may occur before opening any streams so first,
    // check if a session exists, if not create a session with the Azure
    // storage server.
    if (null == storageInteractionLayer) {
      final String errMsg = String.format(
          "Storage session expected for URI '%s' but does not exist.",
          sessionUri);
      throw new AssertionError(errMsg);
    }

    if (checkContainer(ContainerAccessType.ReadThenWrite) == ContainerState.DoesntExist) {
      // Container doesn't exist, no need to do anything.
      return;
    }
    // Get all blob items with the given prefix from the container and delete
    // them.
    Iterable<ListBlobItem> objects = listRootBlobs(prefix, false);
    for (ListBlobItem blobItem : objects) {
      ((CloudBlob) blobItem).delete(DeleteSnapshotsOption.NONE, null, null,
          getInstrumentedContext());
    }
  } catch (Exception e) {
    // Re-throw as an Azure storage exception.
    //
    throw new AzureException(e);
  }
}
 
Example #23
Source File: NativeAzureFileSystemBaseTest.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
// Acquire and free a Lease object. Wait for more than the lease
// timeout, to make sure the lease renews itself.
public void testSelfRenewingLease() throws IllegalArgumentException, IOException,
  InterruptedException, StorageException {

  SelfRenewingLease lease;
  final String FILE_KEY = "file";
  fs.create(new Path(FILE_KEY));
  NativeAzureFileSystem nfs = (NativeAzureFileSystem) fs;
  String fullKey = nfs.pathToKey(nfs.makeAbsolute(new Path(FILE_KEY)));
  AzureNativeFileSystemStore store = nfs.getStore();
  lease = store.acquireLease(fullKey);
  assertTrue(lease.getLeaseID() != null);

  // The sleep time for the keep-alive thread is 40 seconds, so sleep just
  // a little beyond that, to make sure the keep-alive thread wakes up
  // and renews the lease.
  Thread.sleep(42000);
  lease.free();

  // Check that the lease is really freed.
  CloudBlob blob = lease.getCloudBlob();

  // Try to acquire it again, using direct Azure blob access.
  // If that succeeds, then the lease was already freed.
  String differentLeaseID = null;
  try {
    differentLeaseID = blob.acquireLease(15, null);
  } catch (Exception e) {
    e.printStackTrace();
    fail("Caught exception trying to directly re-acquire lease from Azure");
  } finally {
    assertTrue(differentLeaseID != null);
    AccessCondition accessCondition = AccessCondition.generateEmptyCondition();
    accessCondition.setLeaseID(differentLeaseID);
    blob.releaseLease(accessCondition);
  }
}
 
Example #24
Source File: MockStorageInterface.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public CloudBlob getBlob() {
  return null;
}
 
Example #25
Source File: AzureIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testAppendBlob() throws Exception {

    StorageCredentials creds = getStorageCredentials("camelblob", System.getenv(AZURE_STORAGE_BLOB));
    Assume.assumeNotNull("Credentials not null", creds);

    CamelContext camelctx = createCamelContext(creds);
    camelctx.addRoutes(new RouteBuilder() {
        public void configure() throws Exception {
            from("direct:start")
            .to("azure-blob://camelblob/container1/blobAppend?credentials=#creds&operation=updateAppendBlob");

            from("azure-blob://camelblob/container1/blobAppend?credentials=#creds&blobType=appendblob")
            .to("mock:read");

            from("direct:list")
            .to("azure-blob://camelblob/container1?credentials=#creds&operation=listBlobs");
        }
    });

    camelctx.start();
    try {
        MockEndpoint mockRead = camelctx.getEndpoint("mock:read", MockEndpoint.class);
        mockRead.expectedBodiesReceived("Append Blob");
        mockRead.expectedMessageCount(1);

        ProducerTemplate producer = camelctx.createProducerTemplate();

        Iterator<?> it = producer.requestBody("direct:list", null, Iterable.class).iterator();
        Assert.assertFalse("No Blob exists", it.hasNext());

        // append to blob
        producer.sendBody("direct:start", "Append Blob");
        mockRead.assertIsSatisfied();

        it = producer.requestBody("direct:list", null, Iterable.class).iterator();
        Assert.assertTrue("Blob exists", it.hasNext());
        CloudBlob blob = (CloudAppendBlob) it.next();
        blob.delete();

        it = producer.requestBody("direct:list", null, Iterable.class).iterator();
        Assert.assertFalse("No Blob exists", it.hasNext());

    } finally {
        camelctx.close();
    }
}
 
Example #26
Source File: AbstractAzureBlobStorageIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected void uploadTestBlob() throws Exception {
    CloudBlob blob = container.getBlockBlobReference(TEST_BLOB_NAME);
    byte[] buf = "0123456789".getBytes();
    InputStream in = new ByteArrayInputStream(buf);
    blob.upload(in, 10);
}
 
Example #27
Source File: ListAzureBlobStorage.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected List<BlobInfo> performListing(final ProcessContext context, final Long minTimestamp) throws IOException {
    String containerName = context.getProperty(AzureStorageUtils.CONTAINER).evaluateAttributeExpressions().getValue();
    String prefix = context.getProperty(PROP_PREFIX).evaluateAttributeExpressions().getValue();
    if (prefix == null) {
        prefix = "";
    }
    final List<BlobInfo> listing = new ArrayList<>();
    try {
        CloudBlobClient blobClient = AzureStorageUtils.createCloudBlobClient(context, getLogger(), null);
        CloudBlobContainer container = blobClient.getContainerReference(containerName);

        final OperationContext operationContext = new OperationContext();
        AzureStorageUtils.setProxy(operationContext, context);

        for (ListBlobItem blob : container.listBlobs(prefix, true, EnumSet.of(BlobListingDetails.METADATA), null, operationContext)) {
            if (blob instanceof CloudBlob) {
                CloudBlob cloudBlob = (CloudBlob) blob;
                BlobProperties properties = cloudBlob.getProperties();
                StorageUri uri = cloudBlob.getSnapshotQualifiedStorageUri();

                Builder builder = new BlobInfo.Builder()
                                          .primaryUri(uri.getPrimaryUri().toString())
                                          .blobName(cloudBlob.getName())
                                          .containerName(containerName)
                                          .contentType(properties.getContentType())
                                          .contentLanguage(properties.getContentLanguage())
                                          .etag(properties.getEtag())
                                          .lastModifiedTime(properties.getLastModified().getTime())
                                          .length(properties.getLength());

                if (uri.getSecondaryUri() != null) {
                    builder.secondaryUri(uri.getSecondaryUri().toString());
                }

                if (blob instanceof CloudBlockBlob) {
                    builder.blobType(AzureStorageUtils.BLOCK);
                } else {
                    builder.blobType(AzureStorageUtils.PAGE);
                }
                listing.add(builder.build());
            }
        }
    } catch (Throwable t) {
        throw new IOException(ExceptionUtils.getRootCause(t));
    }
    return listing;
}
 
Example #28
Source File: SelfRenewingLease.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public CloudBlob getCloudBlob() {
  return blobWrapper.getBlob();
}
 
Example #29
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));
    }
}
 
Example #30
Source File: MockStorageInterface.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public CloudBlob getBlob() {
  return null;
}