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

The following examples show how to use com.microsoft.azure.storage.blob.CloudBlobContainer#create() . 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: CloudFileTests.java    From azure-storage-android with Apache License 2.0 7 votes vote down vote up
@Test
@Category(SlowTests.class)
public void testCopyBlockBlobSas() throws Exception {
    // Create source on server.
    final CloudBlobContainer container = BlobTestHelper.getRandomContainerReference();
    try {
        container.create();
        final CloudBlockBlob source = container.getBlockBlobReference("source");

        source.getMetadata().put("Test", "value");
        final String data = "String data";
        source.uploadText(data, Constants.UTF8_CHARSET, null, null, null);

        final CloudFile destination = doCloudBlobCopy(source, data.length());

        final String copyData = destination.downloadText(Constants.UTF8_CHARSET, null, null, null);
        assertEquals(data, copyData);
    }
    finally {
        container.deleteIfExists();
    }
}
 
Example 2
Source File: SecondaryTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryOn304() throws StorageException, IOException, URISyntaxException {
    OperationContext operationContext = new OperationContext();
    operationContext.getRetryingEventHandler().addListener(new StorageEvent<RetryingEvent>() {
        @Override
        public void eventOccurred(RetryingEvent eventArg) {
            fail("Request should not be retried.");
        }
    });

    CloudBlobContainer container = BlobTestHelper.getRandomContainerReference();
    try {
        container.create();
        CloudBlockBlob blockBlobRef = (CloudBlockBlob) BlobTestHelper.uploadNewBlob(container, BlobType.BLOCK_BLOB,
                "originalBlob", 1024, null);
        AccessCondition accessCondition = AccessCondition.generateIfNoneMatchCondition(blockBlobRef.getProperties().getEtag());
        blockBlobRef.download(new ByteArrayOutputStream(), accessCondition, null, operationContext);
        
        fail("Download should fail with a 304.");
    } catch (StorageException ex) {
        assertEquals("The condition specified using HTTP conditional header(s) is not met.", ex.getMessage());
    } finally {
        container.deleteIfExists();
    }
}
 
Example 3
Source File: CloudFileTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
@Category(SlowTests.class)
public void testCopyPageBlobSas() throws Exception {
    // Create source on server.
    final CloudBlobContainer container = BlobTestHelper.getRandomContainerReference();
    try {
        container.create();
        final CloudPageBlob source = container.getPageBlobReference("source");

        source.getMetadata().put("Test", "value");
        final int length = 512;
        final ByteArrayInputStream data = BlobTestHelper.getRandomDataStream(length);
        source.upload(data, length);

        final CloudFile destination = doCloudBlobCopy(source, length);

        final ByteArrayOutputStream copyData = new ByteArrayOutputStream();
        destination.download(copyData);
        BlobTestHelper.assertStreamsAreEqual(data, new ByteArrayInputStream(copyData.toByteArray()));
    }
    finally {
        container.deleteIfExists();
    }
}
 
Example 4
Source File: AzureBlobResource.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() throws Exception {
    StorageCredentials credentials = StorageCredentials.tryParseCredentials(System.getProperty("azurite.credentials"));
    URI uri = new URI(System.getProperty("azurite.blob.service.url") + "camel-test");
    CloudBlobContainer container = new CloudBlobContainer(uri, credentials);
    container.create();
}
 
Example 5
Source File: TestContainerChecks.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@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());

  // A list shouldn't create the container and will set file system store
  // 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());
}
 
Example 6
Source File: TestContainerChecks.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testContainerCreateAfterDoesNotExist() 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 and will set file system store
  // state to DoesNotExist
  try {
    assertNull(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();

  // Write should succeed
  assertTrue(fs.createNewFile(new Path("/foo")));
  assertTrue(container.exists());
}
 
Example 7
Source File: AzureBlobStorageTestAccount.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test account that goes against the storage emulator.
 * 
 * @return The test account, or null if the emulator isn't setup.
 */
public static AzureBlobStorageTestAccount createForEmulator()
    throws Exception {
  saveMetricsConfigFile();
  NativeAzureFileSystem fs = null;
  CloudBlobContainer container = null;
  Configuration conf = createTestConfiguration();
  if (!conf.getBoolean(USE_EMULATOR_PROPERTY_NAME, false)) {
    // Not configured to test against the storage emulator.
    System.out
      .println("Skipping emulator Azure test because configuration " +
          "doesn't indicate that it's running." +
          " Please see RunningLiveWasbTests.txt for guidance.");
    return null;
  }
  CloudStorageAccount account =
      CloudStorageAccount.getDevelopmentStorageAccount();
  fs = new NativeAzureFileSystem();
  String containerName = String.format("wasbtests-%s-%tQ",
      System.getProperty("user.name"), new Date());
  container = account.createCloudBlobClient().getContainerReference(
      containerName);
  container.create();

  // Set account URI and initialize Azure file system.
  URI accountUri = createAccountUri(DEFAULT_STORAGE_EMULATOR_ACCOUNT_NAME,
      containerName);
  fs.initialize(accountUri, conf);

  // Create test account initializing the appropriate member variables.
  //
  AzureBlobStorageTestAccount testAcct =
      new AzureBlobStorageTestAccount(fs, account, container);

  return testAcct;
}
 
Example 8
Source File: TestContainerChecks.java    From big-c with Apache License 2.0 5 votes vote down vote up
@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());

  // A list shouldn't create the container and will set file system store
  // 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());
}
 
Example 9
Source File: TestContainerChecks.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testContainerCreateAfterDoesNotExist() 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 and will set file system store
  // state to DoesNotExist
  try {
    assertNull(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();

  // Write should succeed
  assertTrue(fs.createNewFile(new Path("/foo")));
  assertTrue(container.exists());
}
 
Example 10
Source File: AzureBlobStorageTestAccount.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test account that goes against the storage emulator.
 * 
 * @return The test account, or null if the emulator isn't setup.
 */
public static AzureBlobStorageTestAccount createForEmulator()
    throws Exception {
  saveMetricsConfigFile();
  NativeAzureFileSystem fs = null;
  CloudBlobContainer container = null;
  Configuration conf = createTestConfiguration();
  if (!conf.getBoolean(USE_EMULATOR_PROPERTY_NAME, false)) {
    // Not configured to test against the storage emulator.
    System.out
      .println("Skipping emulator Azure test because configuration " +
          "doesn't indicate that it's running." +
          " Please see RunningLiveWasbTests.txt for guidance.");
    return null;
  }
  CloudStorageAccount account =
      CloudStorageAccount.getDevelopmentStorageAccount();
  fs = new NativeAzureFileSystem();
  String containerName = String.format("wasbtests-%s-%tQ",
      System.getProperty("user.name"), new Date());
  container = account.createCloudBlobClient().getContainerReference(
      containerName);
  container.create();

  // Set account URI and initialize Azure file system.
  URI accountUri = createAccountUri(DEFAULT_STORAGE_EMULATOR_ACCOUNT_NAME,
      containerName);
  fs.initialize(accountUri, conf);

  // Create test account initializing the appropriate member variables.
  //
  AzureBlobStorageTestAccount testAcct =
      new AzureBlobStorageTestAccount(fs, account, container);

  return testAcct;
}
 
Example 11
Source File: StorageServiceImpl.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
@NotNull
public static String createContainer(@NotNull final StorageInputs inputs) throws Exception {
    final CloudBlobClient blobClient = getCloudBlobClient(inputs);
    final CloudBlobContainer container = blobClient.getContainerReference(inputs.getContainerName());
    container.create();
    return inputs.getContainerName();
}
 
Example 12
Source File: AzureBlobStorageTestAccount.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public static AzureBlobStorageTestAccount createOutOfBandStore(
    int uploadBlockSize, int downloadBlockSize) throws Exception {

  saveMetricsConfigFile();

  CloudBlobContainer container = null;
  Configuration conf = createTestConfiguration();
  CloudStorageAccount account = createTestAccount(conf);
  if (null == account) {
    return null;
  }

  String containerName = String.format("wasbtests-%s-%tQ",
      System.getProperty("user.name"), new Date());

  // Create the container.
  container = account.createCloudBlobClient().getContainerReference(
      containerName);
  container.create();

  String accountName = conf.get(TEST_ACCOUNT_NAME_PROPERTY_NAME);

  // Ensure that custom throttling is disabled and tolerate concurrent
  // out-of-band appends.
  conf.setBoolean(KEY_DISABLE_THROTTLING, true);
  conf.setBoolean(KEY_READ_TOLERATE_CONCURRENT_APPEND, true);

  // Set account URI and initialize Azure file system.
  URI accountUri = createAccountUri(accountName, containerName);

  // Set up instrumentation.
  //
  AzureFileSystemMetricsSystem.fileSystemStarted();
  String sourceName = NativeAzureFileSystem.newMetricsSourceName();
  String sourceDesc = "Azure Storage Volume File System metrics";

  AzureFileSystemInstrumentation instrumentation = new AzureFileSystemInstrumentation(conf);

  AzureFileSystemMetricsSystem.registerSource(
      sourceName, sourceDesc, instrumentation);
  
  
  // Create a new AzureNativeFileSystemStore object.
  AzureNativeFileSystemStore testStorage = new AzureNativeFileSystemStore();

  // Initialize the store with the throttling feedback interfaces.
  testStorage.initialize(accountUri, conf, instrumentation);

  // Create test account initializing the appropriate member variables.
  //
  AzureBlobStorageTestAccount testAcct =
      new AzureBlobStorageTestAccount(testStorage, account, container);

  return testAcct;
}
 
Example 13
Source File: AzureBlobStorageTestAccount.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public static AzureBlobStorageTestAccount create(String containerNameSuffix,
    EnumSet<CreateOptions> createOptions, Configuration initialConfiguration)
    throws Exception {
  saveMetricsConfigFile();
  NativeAzureFileSystem fs = null;
  CloudBlobContainer container = null;
  Configuration conf = createTestConfiguration(initialConfiguration);
  configurePageBlobDir(conf);
  configureAtomicRenameDir(conf);
  CloudStorageAccount account = createTestAccount(conf);
  if (account == null) {
    return null;
  }
  fs = new NativeAzureFileSystem();
  String containerName = String.format("wasbtests-%s-%tQ%s",
      System.getProperty("user.name"), new Date(), containerNameSuffix);
  container = account.createCloudBlobClient().getContainerReference(
      containerName);
  if (createOptions.contains(CreateOptions.CreateContainer)) {
    container.create();
  }
  String accountName = conf.get(TEST_ACCOUNT_NAME_PROPERTY_NAME);
  if (createOptions.contains(CreateOptions.UseSas)) {
    String sas = generateSAS(container,
        createOptions.contains(CreateOptions.Readonly));
    if (!createOptions.contains(CreateOptions.CreateContainer)) {
      // The caller doesn't want the container to be pre-created,
      // so delete it now that we have generated the SAS.
      container.delete();
    }
    // Remove the account key from the configuration to make sure we don't
    // cheat and use that.
    conf.set(ACCOUNT_KEY_PROPERTY_NAME + accountName, "");
    // Set the SAS key.
    conf.set(SAS_PROPERTY_NAME + containerName + "." + accountName, sas);
  }

  // Check if throttling is turned on and set throttling parameters
  // appropriately.
  if (createOptions.contains(CreateOptions.useThrottling)) {
    conf.setBoolean(KEY_DISABLE_THROTTLING, false);
  } else {
    conf.setBoolean(KEY_DISABLE_THROTTLING, true);
  }

  // Set account URI and initialize Azure file system.
  URI accountUri = createAccountUri(accountName, containerName);
  fs.initialize(accountUri, conf);

  // Create test account initializing the appropriate member variables.
  //
  AzureBlobStorageTestAccount testAcct =
      new AzureBlobStorageTestAccount(fs, account, container);

  return testAcct;
}
 
Example 14
Source File: AzureBlobStorageTestAccount.java    From big-c with Apache License 2.0 4 votes vote down vote up
public static AzureBlobStorageTestAccount createOutOfBandStore(
    int uploadBlockSize, int downloadBlockSize) throws Exception {

  saveMetricsConfigFile();

  CloudBlobContainer container = null;
  Configuration conf = createTestConfiguration();
  CloudStorageAccount account = createTestAccount(conf);
  if (null == account) {
    return null;
  }

  String containerName = String.format("wasbtests-%s-%tQ",
      System.getProperty("user.name"), new Date());

  // Create the container.
  container = account.createCloudBlobClient().getContainerReference(
      containerName);
  container.create();

  String accountName = conf.get(TEST_ACCOUNT_NAME_PROPERTY_NAME);

  // Ensure that custom throttling is disabled and tolerate concurrent
  // out-of-band appends.
  conf.setBoolean(KEY_DISABLE_THROTTLING, true);
  conf.setBoolean(KEY_READ_TOLERATE_CONCURRENT_APPEND, true);

  // Set account URI and initialize Azure file system.
  URI accountUri = createAccountUri(accountName, containerName);

  // Set up instrumentation.
  //
  AzureFileSystemMetricsSystem.fileSystemStarted();
  String sourceName = NativeAzureFileSystem.newMetricsSourceName();
  String sourceDesc = "Azure Storage Volume File System metrics";

  AzureFileSystemInstrumentation instrumentation = new AzureFileSystemInstrumentation(conf);

  AzureFileSystemMetricsSystem.registerSource(
      sourceName, sourceDesc, instrumentation);
  
  
  // Create a new AzureNativeFileSystemStore object.
  AzureNativeFileSystemStore testStorage = new AzureNativeFileSystemStore();

  // Initialize the store with the throttling feedback interfaces.
  testStorage.initialize(accountUri, conf, instrumentation);

  // Create test account initializing the appropriate member variables.
  //
  AzureBlobStorageTestAccount testAcct =
      new AzureBlobStorageTestAccount(testStorage, account, container);

  return testAcct;
}
 
Example 15
Source File: AzureBlobStorageTestAccount.java    From big-c with Apache License 2.0 4 votes vote down vote up
public static AzureBlobStorageTestAccount create(String containerNameSuffix,
    EnumSet<CreateOptions> createOptions, Configuration initialConfiguration)
    throws Exception {
  saveMetricsConfigFile();
  NativeAzureFileSystem fs = null;
  CloudBlobContainer container = null;
  Configuration conf = createTestConfiguration(initialConfiguration);
  configurePageBlobDir(conf);
  configureAtomicRenameDir(conf);
  CloudStorageAccount account = createTestAccount(conf);
  if (account == null) {
    return null;
  }
  fs = new NativeAzureFileSystem();
  String containerName = String.format("wasbtests-%s-%tQ%s",
      System.getProperty("user.name"), new Date(), containerNameSuffix);
  container = account.createCloudBlobClient().getContainerReference(
      containerName);
  if (createOptions.contains(CreateOptions.CreateContainer)) {
    container.create();
  }
  String accountName = conf.get(TEST_ACCOUNT_NAME_PROPERTY_NAME);
  if (createOptions.contains(CreateOptions.UseSas)) {
    String sas = generateSAS(container,
        createOptions.contains(CreateOptions.Readonly));
    if (!createOptions.contains(CreateOptions.CreateContainer)) {
      // The caller doesn't want the container to be pre-created,
      // so delete it now that we have generated the SAS.
      container.delete();
    }
    // Remove the account key from the configuration to make sure we don't
    // cheat and use that.
    conf.set(ACCOUNT_KEY_PROPERTY_NAME + accountName, "");
    // Set the SAS key.
    conf.set(SAS_PROPERTY_NAME + containerName + "." + accountName, sas);
  }

  // Check if throttling is turned on and set throttling parameters
  // appropriately.
  if (createOptions.contains(CreateOptions.useThrottling)) {
    conf.setBoolean(KEY_DISABLE_THROTTLING, false);
  } else {
    conf.setBoolean(KEY_DISABLE_THROTTLING, true);
  }

  // Set account URI and initialize Azure file system.
  URI accountUri = createAccountUri(accountName, containerName);
  fs.initialize(accountUri, conf);

  // Create test account initializing the appropriate member variables.
  //
  AzureBlobStorageTestAccount testAcct =
      new AzureBlobStorageTestAccount(fs, account, container);

  return testAcct;
}