本文整理汇总了Java中com.microsoft.azure.storage.blob.CloudBlockBlob.commitBlockList方法的典型用法代码示例。如果您正苦于以下问题:Java CloudBlockBlob.commitBlockList方法的具体用法?Java CloudBlockBlob.commitBlockList怎么用?Java CloudBlockBlob.commitBlockList使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.microsoft.azure.storage.blob.CloudBlockBlob
的用法示例。
在下文中一共展示了CloudBlockBlob.commitBlockList方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadFileBlocksAsBlockBlob
import com.microsoft.azure.storage.blob.CloudBlockBlob; //导入方法依赖的package包/类
/**
* Creates and returns a temporary local file for use by the sample.
*
* @param blockBlob CloudBlockBlob object.
* @param filePath The path to the file to be uploaded.
*
* @throws Throwable
*/
private static void uploadFileBlocksAsBlockBlob(CloudBlockBlob blockBlob, String filePath) throws Throwable {
FileInputStream fileInputStream = null;
try {
// Open the file
fileInputStream = new FileInputStream(filePath);
// Split the file into 32K blocks (block size deliberately kept small for the demo) and upload all the blocks
int blockNum = 0;
String blockId = null;
String blockIdEncoded = null;
ArrayList<BlockEntry> blockList = new ArrayList<BlockEntry>();
while (fileInputStream.available() > (32 * 1024)) {
blockId = String.format("%05d", blockNum);
blockIdEncoded = Base64.getEncoder().encodeToString(blockId.getBytes());
blockBlob.uploadBlock(blockIdEncoded, fileInputStream, (32 * 1024));
blockList.add(new BlockEntry(blockIdEncoded));
blockNum++;
}
blockId = String.format("%05d", blockNum);
blockIdEncoded = Base64.getEncoder().encodeToString(blockId.getBytes());
blockBlob.uploadBlock(blockIdEncoded, fileInputStream, fileInputStream.available());
blockList.add(new BlockEntry(blockIdEncoded));
// Commit the blocks
blockBlob.commitBlockList(blockList);
}
catch (Throwable t) {
throw t;
}
finally {
// Close the file output stream writer
if (fileInputStream != null) {
fileInputStream.close();
}
}
}
示例2: testStoreBlobMd5
import com.microsoft.azure.storage.blob.CloudBlockBlob; //导入方法依赖的package包/类
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));
}
}
示例3: upload
import com.microsoft.azure.storage.blob.CloudBlockBlob; //导入方法依赖的package包/类
private void upload(EntryVideo video, String url)
throws Exception {
final byte[] buffer = new byte[BLOCK_SIZE_BYTES];
final File file = new File(App.STORAGE_DIRECTORY, video.filename);
if(!file.exists()) {
dao.markAsUploaded(video);
return;
}
CloudBlockBlob blob = new CloudBlockBlob(new URI(url));
ArrayList<BlockEntry> blocks = blob.exists()
? blob.downloadBlockList()
: new ArrayList<BlockEntry>();
BufferedInputStream input = new BufferedInputStream(
new FileInputStream(file));
try {
int id = 0;
if(blocks.size() > 0) {
id = decodeId(blocks.get(blocks.size() -1).getId()) + 1;
input.skip(BLOCK_SIZE_BYTES * id);
}
int read;
while((read = input.read(buffer, 0, BLOCK_SIZE_BYTES)) > 0) {
BlockEntry block = new BlockEntry(encodeId(id));
blocks.add(block);
blob.uploadBlock(block.getId(),
new ByteArrayInputStream(buffer, 0, read), read);
blob.commitBlockList(blocks);
id++;
video.percentUploaded = Math.min(100, (int)((((id + 1)
* BLOCK_SIZE_BYTES) / file.length()) * 100));
dao.update(video);
for(UploadProgressListener l : listeners)
l.progressChanged(video);
}
dao.markAsUploaded(video);
} finally {
input.close();
}
}