将文本行附加到Android的Azure Block Blob

时间:2015-06-26 22:32:42

标签: android azure-storage-blobs

我想在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,租约就会出现,我可能会遇到完整性问题。我将非常感谢帮助您使这些代码更简单,更安全。我知道微软计划在2015年第三季度推出附加blob,但我现在要实现这一点。

1 个答案:

答案 0 :(得分:1)

您可以调用PutBlock上传附加的内容(每个块的最大大小为4MB,因此请根据需要将附加的内容拆分为块),然后通过传入先前提交的块加上来调用此blob上的PutBlockList。新附加的块。