从 Android 向 Azure Block Blob 添加一行文本
Append line of text to Azure Block Blob from Android
我想从 Android 设备向现有的 Azure 云块 blob 附加一行文本。
在 VB.Net 中,我将 AcquireLease、getBlockBlobReference、DownloadToFile 添加到本地文件系统、UploadToFile、ReleaseLease 行。简单又安全,就是有点啰嗦。
在Android中,看起来有点棘手。目前,我最好的解决方案是:
CloudBlockBlob blob1=container.getBlockBlobReference(chosenOne+".txt");
String proposedLeaseId1 = UUID.randomUUID().toString();
OperationContext operationContext1 = new OperationContext();
blob1.acquireLease(15, proposedLeaseId1, null /*access condition*/,null/* BlobRequestOptions */, operationContext1);
AccessCondition condition = new AccessCondition();
condition.setLeaseID(proposedLeaseId1);
BlobInputStream blobIn = blob1.openInputStream();
blob1.downloadAttributes();
long blobLengthToUse = blob1.getProperties().getLength();
byte[] result = new byte[(int) blobLengthToUse];
blob1.downloadToByteArray(result,0);
blobIn.close();
CloudBlockBlob blob1 = container.getBlockBlobReference(chosenOne+".txt");
String proposedLeaseId1 = UUID.randomUUID().toString();
OperationContext operationContext1 = new OperationContext();
blob1.acquireLease(15, proposedLeaseId1, null /*access condition*/,null/* BlobRequestOptions */, operationContext1);
AccessCondition condition = new AccessCondition();
condition.setLeaseID(proposedLeaseId1);
BlobInputStream blobIn = blob1.openInputStream();
blob1.downloadAttributes();
long blobLengthToUse = blob1.getProperties().getLength();
byte[] result = new byte[(int) blobLengthToUse];
blob1.downloadToByteArray(result,0);
blobIn.close();
blob1.deleteIfExists(DeleteSnapshotsOption.NONE,condition, null, operationContext1);
BlobOutputStream blobOut = blob1.openOutputStream();
//this is a byte by byte write ...
//which is fine ... but no use if you want to replace ...
/*int next = blobIn.read();
while (next != -1) {
blobOut.write(next);
next = blobIn.read();
}*/
blobOut.write(result);
String strTemp="This is just a test string";
blobOut.write(strTemp.getBytes());
blobOut.close();
除了啰嗦之外,我担心一旦删除 blob,租约就会消失,我可能会遇到完整性问题。对于使此代码更简单和更安全的任何帮助,我将不胜感激。我知道 Microsoft 计划在 2015 年第 3 季度推出附加 blob,但我现在想实施它。
您可以调用PutBlock上传追加内容(每个块的最大大小为4MB,所以如果需要请将追加内容拆分成块),然后通过传入之前提交的块在这个blob上调用PutBlockList加上和新附加的块。
我想从 Android 设备向现有的 Azure 云块 blob 附加一行文本。
在 VB.Net 中,我将 AcquireLease、getBlockBlobReference、DownloadToFile 添加到本地文件系统、UploadToFile、ReleaseLease 行。简单又安全,就是有点啰嗦。
在Android中,看起来有点棘手。目前,我最好的解决方案是:
CloudBlockBlob blob1=container.getBlockBlobReference(chosenOne+".txt");
String proposedLeaseId1 = UUID.randomUUID().toString();
OperationContext operationContext1 = new OperationContext();
blob1.acquireLease(15, proposedLeaseId1, null /*access condition*/,null/* BlobRequestOptions */, operationContext1);
AccessCondition condition = new AccessCondition();
condition.setLeaseID(proposedLeaseId1);
BlobInputStream blobIn = blob1.openInputStream();
blob1.downloadAttributes();
long blobLengthToUse = blob1.getProperties().getLength();
byte[] result = new byte[(int) blobLengthToUse];
blob1.downloadToByteArray(result,0);
blobIn.close();
CloudBlockBlob blob1 = container.getBlockBlobReference(chosenOne+".txt");
String proposedLeaseId1 = UUID.randomUUID().toString();
OperationContext operationContext1 = new OperationContext();
blob1.acquireLease(15, proposedLeaseId1, null /*access condition*/,null/* BlobRequestOptions */, operationContext1);
AccessCondition condition = new AccessCondition();
condition.setLeaseID(proposedLeaseId1);
BlobInputStream blobIn = blob1.openInputStream();
blob1.downloadAttributes();
long blobLengthToUse = blob1.getProperties().getLength();
byte[] result = new byte[(int) blobLengthToUse];
blob1.downloadToByteArray(result,0);
blobIn.close();
blob1.deleteIfExists(DeleteSnapshotsOption.NONE,condition, null, operationContext1);
BlobOutputStream blobOut = blob1.openOutputStream();
//this is a byte by byte write ...
//which is fine ... but no use if you want to replace ...
/*int next = blobIn.read();
while (next != -1) {
blobOut.write(next);
next = blobIn.read();
}*/
blobOut.write(result);
String strTemp="This is just a test string";
blobOut.write(strTemp.getBytes());
blobOut.close();
除了啰嗦之外,我担心一旦删除 blob,租约就会消失,我可能会遇到完整性问题。对于使此代码更简单和更安全的任何帮助,我将不胜感激。我知道 Microsoft 计划在 2015 年第 3 季度推出附加 blob,但我现在想实施它。
您可以调用PutBlock上传追加内容(每个块的最大大小为4MB,所以如果需要请将追加内容拆分成块),然后通过传入之前提交的块在这个blob上调用PutBlockList加上和新附加的块。