Search in sources :

Example 1 with CloudBlobContainer

use of com.microsoft.azure.storage.blob.CloudBlobContainer in project camel by apache.

the class BlobServiceProducer method listBlobs.

private void listBlobs(Exchange exchange) throws Exception {
    CloudBlobContainer client = BlobServiceUtil.createBlobContainerClient(getConfiguration());
    BlobServiceRequestOptions opts = BlobServiceUtil.getRequestOptions(exchange);
    LOG.trace("Getting the blob list from the container [{}] from exchange [{}]...", getConfiguration().getContainerName(), exchange);
    BlobServiceConfiguration cfg = getConfiguration();
    EnumSet<BlobListingDetails> details = null;
    Object detailsObject = exchange.getIn().getHeader(BlobServiceConstants.BLOB_LISTING_DETAILS);
    if (detailsObject instanceof EnumSet) {
        @SuppressWarnings("unchecked") EnumSet<BlobListingDetails> theDetails = (EnumSet<BlobListingDetails>) detailsObject;
        details = theDetails;
    } else if (detailsObject instanceof BlobListingDetails) {
        details = EnumSet.of((BlobListingDetails) detailsObject);
    }
    Iterable<ListBlobItem> items = client.listBlobs(cfg.getBlobPrefix(), cfg.isUseFlatListing(), details, opts.getRequestOpts(), opts.getOpContext());
    ExchangeUtil.getMessageForResponse(exchange).setBody(items);
}
Also used : BlobListingDetails(com.microsoft.azure.storage.blob.BlobListingDetails) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) EnumSet(java.util.EnumSet) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer)

Example 2 with CloudBlobContainer

use of com.microsoft.azure.storage.blob.CloudBlobContainer in project camel by apache.

the class BlobServiceUtil method createBlobContainerClient.

public static CloudBlobContainer createBlobContainerClient(BlobServiceConfiguration cfg) throws Exception {
    URI uri = prepareStorageBlobUri(cfg, false);
    StorageCredentials creds = getAccountCredentials(cfg);
    return new CloudBlobContainer(uri, creds);
}
Also used : StorageCredentials(com.microsoft.azure.storage.StorageCredentials) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) URI(java.net.URI)

Example 3 with CloudBlobContainer

use of com.microsoft.azure.storage.blob.CloudBlobContainer in project hadoop by apache.

the class TestWasbUriAndConfiguration method testConnectUsingSASReadonly.

@Test
public void testConnectUsingSASReadonly() throws Exception {
    Assume.assumeFalse(runningInSASMode);
    // Create the test account with SAS credentials.
    testAccount = AzureBlobStorageTestAccount.create("", EnumSet.of(CreateOptions.UseSas, CreateOptions.CreateContainer, CreateOptions.Readonly));
    assumeNotNull(testAccount);
    // Create a blob in there
    final String blobKey = "blobForReadonly";
    CloudBlobContainer container = testAccount.getRealContainer();
    CloudBlockBlob blob = container.getBlockBlobReference(blobKey);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 1, 2, 3 });
    blob.upload(inputStream, 3);
    inputStream.close();
    // Make sure we can read it from the file system
    Path filePath = new Path("/" + blobKey);
    FileSystem fs = testAccount.getFileSystem();
    assertTrue(fs.exists(filePath));
    byte[] obtained = new byte[3];
    DataInputStream obtainedInputStream = fs.open(filePath);
    obtainedInputStream.readFully(obtained);
    obtainedInputStream.close();
    assertEquals(3, obtained[2]);
}
Also used : Path(org.apache.hadoop.fs.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) FileSystem(org.apache.hadoop.fs.FileSystem) AbstractFileSystem(org.apache.hadoop.fs.AbstractFileSystem) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 4 with CloudBlobContainer

use of com.microsoft.azure.storage.blob.CloudBlobContainer in project hadoop by apache.

the class TestContainerChecks method testContainerExistAfterDoesNotExist.

@Test
public void testContainerExistAfterDoesNotExist() throws Exception {
    testAccount = AzureBlobStorageTestAccount.create("", EnumSet.noneOf(CreateOptions.class));
    assumeNotNull(testAccount);
    CloudBlobContainer container = testAccount.getRealContainer();
    FileSystem fs = testAccount.getFileSystem();
    // Starting off with the container not there
    assertFalse(container.exists());
    // state to DoesNotExist
    try {
        fs.listStatus(new Path("/"));
        assertTrue("Should've thrown.", false);
    } catch (FileNotFoundException ex) {
        assertTrue("Unexpected exception: " + ex, ex.getMessage().contains("does not exist."));
    }
    assertFalse(container.exists());
    // Create a container outside of the WASB FileSystem
    container.create();
    // Add a file to the container outside of the WASB FileSystem
    CloudBlockBlob blob = testAccount.getBlobReference("foo");
    BlobOutputStream outputStream = blob.openOutputStream();
    outputStream.write(new byte[10]);
    outputStream.close();
    // Make sure the file is visible
    assertTrue(fs.exists(new Path("/foo")));
    assertTrue(container.exists());
}
Also used : Path(org.apache.hadoop.fs.Path) FileSystem(org.apache.hadoop.fs.FileSystem) FileNotFoundException(java.io.FileNotFoundException) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) BlobOutputStream(com.microsoft.azure.storage.blob.BlobOutputStream) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) Test(org.junit.Test)

Example 5 with CloudBlobContainer

use of com.microsoft.azure.storage.blob.CloudBlobContainer in project hadoop by apache.

the class TestContainerChecks method testContainerCreateOnWrite.

@Test
public void testContainerCreateOnWrite() throws Exception {
    testAccount = AzureBlobStorageTestAccount.create("", EnumSet.noneOf(CreateOptions.class));
    assumeNotNull(testAccount);
    CloudBlobContainer container = testAccount.getRealContainer();
    FileSystem fs = testAccount.getFileSystem();
    // Starting off with the container not there
    assertFalse(container.exists());
    // A list shouldn't create the container.
    try {
        fs.listStatus(new Path("/"));
        assertTrue("Should've thrown.", false);
    } catch (FileNotFoundException ex) {
        assertTrue("Unexpected exception: " + ex, ex.getMessage().contains("does not exist."));
    }
    assertFalse(container.exists());
    // Neither should a read.
    try {
        fs.open(new Path("/foo"));
        assertFalse("Should've thrown.", true);
    } catch (FileNotFoundException ex) {
    }
    assertFalse(container.exists());
    // Neither should a rename
    assertFalse(fs.rename(new Path("/foo"), new Path("/bar")));
    assertFalse(container.exists());
    // But a write should.
    assertTrue(fs.createNewFile(new Path("/foo")));
    assertTrue(container.exists());
}
Also used : Path(org.apache.hadoop.fs.Path) FileSystem(org.apache.hadoop.fs.FileSystem) FileNotFoundException(java.io.FileNotFoundException) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) Test(org.junit.Test)

Aggregations

CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)29 CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)13 CloudBlockBlob (com.microsoft.azure.storage.blob.CloudBlockBlob)9 CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)7 StorageException (com.microsoft.azure.storage.StorageException)7 URISyntaxException (java.net.URISyntaxException)6 Test (org.junit.Test)6 ListBlobItem (com.microsoft.azure.storage.blob.ListBlobItem)5 FileSystem (org.apache.hadoop.fs.FileSystem)5 Path (org.apache.hadoop.fs.Path)5 InvalidKeyException (java.security.InvalidKeyException)4 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 URI (java.net.URI)3 WebApp (com.microsoft.azure.management.appservice.WebApp)2 BlobContainerPermissions (com.microsoft.azure.storage.blob.BlobContainerPermissions)2 CloudBlob (com.microsoft.azure.storage.blob.CloudBlob)2 ArrayList (java.util.ArrayList)2 RepositoryException (org.elasticsearch.repositories.RepositoryException)2