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

查看:78
本文介绍了可以使用PutBlock附加到Azure中的现有BlockBlob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照标题,我有一个程序试图通过PutBlock方法将其添加到现有的BlobkBlob中:

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 });
        }
    }

据我所知,只要BlockId增加(或至少有所不同),并且大小一致,这应该可行;但是,当我第二次为同一文件运行该文件时(无论是否增加Block ID),它只会用新文本覆盖现有文件.

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.

我意识到还有其他选项可以附加到Blob(例如AppendBlob),但是我很好奇PutBlock是否可以做到这一点.我试图做的事情是否可能?如果可以,我在做什么错了?

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

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.

您需要做的是:

  1. 首先获取先前提交的阻止列表.您要使用的方法是 DownloadBlockList .
  2. 上传新块.记下其块ID.
  3. 将此块ID附加到您在步骤#1中下载的块ID列表.
  4. 使用此新列表调用放置阻止列表.

这篇关于可以使用PutBlock附加到Azure中的现有BlockBlob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆