根据标题,我有一个程序,我试图使用PutBlock方法添加到现有的BlobkBlob:
private static void UploadNewText(string text) { string fileName = "test4.txt"; string containerString = "mycontainer"; CloudStorageAccount storage = CloudStorageAccount.Parse(connection); CloudBlobClient client = storage.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference(containerString); CloudBlockBlob blob = container.GetBlockBlobReference(fileName); using (MemoryStream stream = new MemoryStream()) using (StreamWriter sw = new StreamWriter(stream)) { sw.Write(text); sw.Flush(); stream.Position = 0; string blockId = Convert.ToBase64String( ASCIIEncoding.ASCII.GetBytes("0000005")); Console.WriteLine(blockId); blob.PutBlock(blockId, stream, null); blob.PutBlockList(new string[] { blockId }); } }据我了解,提供BlockId增加(或至少不同),并且是一致的大小,这应该工作; 但是,当我第二次为同一个文件运行它时(无论我是否增加了Block ID),它只是用新文本覆盖现有文件。
我意识到还有其他选项可以附加到blob(例如AppendBlob),但我很好奇PutBlock,特别是可以做到这一点。 我正在努力做什么,如果是的话,我做错了什么?
As per the title, I have a program whereby I'm trying to add to an existing BlobkBlob using the PutBlock method:
private static void UploadNewText(string text) { string fileName = "test4.txt"; string containerString = "mycontainer"; CloudStorageAccount storage = CloudStorageAccount.Parse(connection); CloudBlobClient client = storage.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference(containerString); CloudBlockBlob blob = container.GetBlockBlobReference(fileName); using (MemoryStream stream = new MemoryStream()) using (StreamWriter sw = new StreamWriter(stream)) { sw.Write(text); sw.Flush(); stream.Position = 0; string blockId = Convert.ToBase64String( ASCIIEncoding.ASCII.GetBytes("0000005")); Console.WriteLine(blockId); blob.PutBlock(blockId, stream, null); blob.PutBlockList(new string[] { blockId }); } }As I understand it, providing the BlockId increases (or at least differs), and is a consistent size, this should work; however, when I run it a second time for the same file (regardless of whether or not I increase the Block ID) it just overwrites the existing file with the new text.
I realise there are other options for appending to a blob (such as AppendBlob), but I'm curious if PutBlock, specifically can do this. Is what I am trying to do possible and, if so, what am I doing wrong?
最满意答案
可以使用PutBlock附加到Azure中的现有BlockBlob
是的,它可以。 但是,为了做到这一点,你需要以一种不同的方式工作。
你需要做的是:
首先获取先前提交的阻止列表。 您要使用的方法是DownloadBlockList 。 上传新块。 记下它的块ID。 将此块ID附加到您在步骤1中下载的块ID列表。 使用此新列表调用put block list。Can PutBlock be used to append to an existing BlockBlob in Azure
Yes, it can. However in order to do that, you will need to work that in a little bit different way.
What you will need to do is:
First get the previously committed block list. The method you want to use is DownloadBlockList. Upload new block. Note down its block id. Append this block id to the list of block ids you downloaded in step #1. Call put block list with this new list.更多推荐
发布评论