可以使用PutBlock附加到Azure中的现有BlockBlob(Can PutBlock be used to append to an existing BlockBlob in Azure)

编程入门 行业动态 更新时间:2024-10-26 10:39:39
可以使用PutBlock附加到Azure中的现有BlockBlob(Can PutBlock be used to append to an existing BlockBlob in Azure)

根据标题,我有一个程序,我试图使用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.

更多推荐

本文发布于:2023-07-18 19:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1164493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   PutBlock   Azure   existing   append

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!