Append to CloudBlockBlob stream

霸气de小男生 提交于 2020-06-11 20:01:16

问题


We have a file system abstraction that allows us to easily switch between local and cloud (Azure) storage.

For reading and writing files we have the following members:

Stream OpenRead();
Stream OpenWrite();

Part of our application "bundles" documents into one file. For our local storage provider OpenWrite returns an appendable stream:

public Stream OpenWrite()
{
    return new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, BufferSize, useAsync: true);
}

For Azure blob storage we do the following:

public Stream OpenWrite()
{               
    return blob.OpenWrite();
}

Unfortunately this overrides the blob contents each time. Is it possible to return a writable stream that can be appended to?


回答1:


Based on the documentation for OpenWrite here http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.openwrite.aspx, The OpenWrite method will overwrite an existing blob unless explicitly prevented using the accessCondition parameter.

One thing you could do is read the blob data in a stream and return that stream to your calling application and let that application append data to that stream. For example, see the code below:

    static void BlobStreamTest()
    {
        storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
        CloudBlobContainer container = storageAccount.CreateCloudBlobClient().GetContainerReference("temp");
        container.CreateIfNotExists();
        CloudBlockBlob blob = container.GetBlockBlobReference("test.txt");
        blob.UploadFromStream(new MemoryStream());//Let's just create an empty blob for the sake of demonstration.
        for (int i = 0; i < 10; i++)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    blob.DownloadToStream(ms);//Read blob data in a stream.
                    byte[] dataToWrite = Encoding.UTF8.GetBytes("This is line # " + (i + 1) + "\r\n");
                    ms.Write(dataToWrite, 0, dataToWrite.Length);
                    ms.Position = 0;
                    blob.UploadFromStream(ms);
                }
            }
            catch (StorageException excep)
            {
                if (excep.RequestInformation.HttpStatusCode != 404)
                {
                    throw;
                }
            }
        }
    }



回答2:


There is now a CloudAppendBlob class that allows you to add content to an existing blob :

var account = CloudStorageAccount.Parse("storage account connectionstring");
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("container name");
var blob = container.GetAppendBlobReference("blob name");

In your case you want to append from a stream:

await blob.AppendFromStreamAsync(new MemoryStream());

But you can append from text, byte array, file. Check the documentation.



来源:https://stackoverflow.com/questions/19161869/append-to-cloudblockblob-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!