Quentin.C
Quentin.C

Reputation: 13

How to write/modify/add some text in a file stored in azure file storage?

My goal is to modify a .txt file in azure file storage using WindowsAzure.Storage API. I would like to know if there is any method to add some text in the file.

Is it easier to use the System.IO API?

I've already tried the cloudFileStream.Write() but it didn't work.

Thank you

Upvotes: 1

Views: 3333

Answers (2)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30025

If you want to add some text(append to the existing data) to the file on azure file storage, there is no direct method. You need to download it and then upload with the text you want to append.

            string accountName = "xxx";
            string key = "xxx";
            var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, key), true);
            var share = storageAccount.CreateCloudFileClient().GetShareReference("testfolder");
            CloudFile file1 = share.GetRootDirectoryReference().GetFileReference("a.txt");

            //if you want to append some text from local file
            var stream1 = File.OpenRead("your file path in local, like d:\hello.txt");
            string from_local_file = (new StreamReader(stream1)).ReadToEnd();

            //if you just want to add some text from string, directly use the string
            //string from_local_file ="the text I want to append to azure file";


            //download the content of the azure file
            string from_azure_file = file1.DownloadText();

            //this does the trick like appending text to azure file, not overwrite
            file1.UploadText(from_azure_file + from_local_file);

If you want to directly upload text to file stored on azure file storage, you should use one of the following methods: UploadText() / UploadFromFile() / UploadFromStream(). Note that this will overwrite the existing data in the azure file.

If you want to update the context of azure file, you can use WriteRange() method. But it has some limitations, if you're interesting about it, I can provide you some code.

Upvotes: 3

betelgeuce
betelgeuce

Reputation: 837

The sample on https://github.com/Azure/azure-storage-net/blob/master/Test/WindowsRuntime/File/FileStreamTests.cs shows you how to do this.

 public async Task FileOpenWriteTestAsync()
        {
            byte[] buffer = GetRandomBuffer(2 * 1024);
            CloudFileShare share = GetRandomShareReference();
            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (CloudFileStream fileStream = await file.OpenWriteAsync(2048))
                {
                    Stream fileStreamForWrite = fileStream;
                    await fileStreamForWrite.WriteAsync(buffer, 0, 2048);
                    await fileStreamForWrite.FlushAsync();

                    byte[] testBuffer = new byte[2048];
                    MemoryStream dstStream = new MemoryStream(testBuffer);
                    await file.DownloadRangeToStreamAsync(dstStream, null, null);

                    MemoryStream memStream = new MemoryStream(buffer);
                    TestHelper.AssertStreamsAreEqual(memStream, dstStream);
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }

Upvotes: 2

Related Questions