Java Code Examples for com.microsoft.azure.storage.blob.CloudBlockBlob#commitBlockList()

The following examples show how to use com.microsoft.azure.storage.blob.CloudBlockBlob#commitBlockList() . 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: TestBlobDataValidation.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
  assumeNotNull(testAccount);
  // Write a test file.
  String testFileKey = "testFile";
  Path testFilePath = new Path("/" + testFileKey);
  OutputStream outStream = testAccount.getFileSystem().create(testFilePath);
  outStream.write(new byte[] { 5, 15 });
  outStream.close();

  // Check that we stored/didn't store the MD5 field as configured.
  CloudBlockBlob blob = testAccount.getBlobReference(testFileKey);
  blob.downloadAttributes();
  String obtainedMd5 = blob.getProperties().getContentMD5();
  if (expectMd5Stored) {
    assertNotNull(obtainedMd5);
  } else {
    assertNull("Expected no MD5, found: " + obtainedMd5, obtainedMd5);
  }

  // Mess with the content so it doesn't match the MD5.
  String newBlockId = Base64.encode(new byte[] { 55, 44, 33, 22 });
  blob.uploadBlock(newBlockId,
      new ByteArrayInputStream(new byte[] { 6, 45 }), 2);
  blob.commitBlockList(Arrays.asList(new BlockEntry[] { new BlockEntry(
      newBlockId, BlockSearchMode.UNCOMMITTED) }));

  // Now read back the content. If we stored the MD5 for the blob content
  // we should get a data corruption error.
  InputStream inStream = testAccount.getFileSystem().open(testFilePath);
  try {
    byte[] inBuf = new byte[100];
    while (inStream.read(inBuf) > 0){
      //nothing;
    }
    inStream.close();
    if (expectMd5Stored) {
      fail("Should've thrown because of data corruption.");
    }
  } catch (IOException ex) {
    if (!expectMd5Stored) {
      throw ex;
    }
    StorageException cause = (StorageException)ex.getCause();
    assertNotNull(cause);
    assertTrue("Unexpected cause: " + cause,
        cause.getErrorCode().equals(StorageErrorCodeStrings.INVALID_MD5));
  }
}
 
Example 2
Source File: TestBlobDataValidation.java    From big-c with Apache License 2.0 4 votes vote down vote up
private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
  assumeNotNull(testAccount);
  // Write a test file.
  String testFileKey = "testFile";
  Path testFilePath = new Path("/" + testFileKey);
  OutputStream outStream = testAccount.getFileSystem().create(testFilePath);
  outStream.write(new byte[] { 5, 15 });
  outStream.close();

  // Check that we stored/didn't store the MD5 field as configured.
  CloudBlockBlob blob = testAccount.getBlobReference(testFileKey);
  blob.downloadAttributes();
  String obtainedMd5 = blob.getProperties().getContentMD5();
  if (expectMd5Stored) {
    assertNotNull(obtainedMd5);
  } else {
    assertNull("Expected no MD5, found: " + obtainedMd5, obtainedMd5);
  }

  // Mess with the content so it doesn't match the MD5.
  String newBlockId = Base64.encode(new byte[] { 55, 44, 33, 22 });
  blob.uploadBlock(newBlockId,
      new ByteArrayInputStream(new byte[] { 6, 45 }), 2);
  blob.commitBlockList(Arrays.asList(new BlockEntry[] { new BlockEntry(
      newBlockId, BlockSearchMode.UNCOMMITTED) }));

  // Now read back the content. If we stored the MD5 for the blob content
  // we should get a data corruption error.
  InputStream inStream = testAccount.getFileSystem().open(testFilePath);
  try {
    byte[] inBuf = new byte[100];
    while (inStream.read(inBuf) > 0){
      //nothing;
    }
    inStream.close();
    if (expectMd5Stored) {
      fail("Should've thrown because of data corruption.");
    }
  } catch (IOException ex) {
    if (!expectMd5Stored) {
      throw ex;
    }
    StorageException cause = (StorageException)ex.getCause();
    assertNotNull(cause);
    assertTrue("Unexpected cause: " + cause,
        cause.getErrorCode().equals(StorageErrorCodeStrings.INVALID_MD5));
  }
}