crystyxn
crystyxn

Reputation: 1621

Create text file in memory then upload it to Azure Blob Storage

Right now I am creating a Parquet file in memory with the help of a library and I am uploading it to Azure Blob storage. However, I want something simpler this time, namely just a text file to write DateTime.UtcNow to a text file in memory and upload it to Azure.

My code so far:

BlobServiceClient BlobServiceClient = new BlobServiceClient("my_code");
var containerClient = BlobServiceClient.GetBlobContainerClient("my_container");
var blobClient = containerClient.GetBlockBlobClient($"path/to/specific/directory/file.parquet");

using (var outStream = await blobClient.OpenWriteAsync(true).ConfigureAwait(false))
using (ChoParquetWriter parser = new ChoParquetWriter(outStream))
{
   parser.Write(data_list);
}

How do I do create the text file in memory without ChoParquetWriter which is part of the library I am using?

Update: found a solution

using (var outStream = await blobClient4.OpenWriteAsync(true).ConfigureAwait(false))
            { 
                byte[] bytes = null;
                using (var ms = new MemoryStream())
                {
                    TextWriter tw = new StreamWriter(ms);
                    tw.Write(RequestTime.ToString());
                    tw.Flush();
                    ms.Position = 0;
                    bytes = ms.ToArray();
                    ms.WriteTo(outStream);
                }
            }

is there a simpler way? if not this is okay

Upvotes: 2

Views: 5336

Answers (2)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30035

It's a little simpler if directly using UploadAsync or Upload method.

For example:

        //other code
        var blobClient4 = containerClient.GetBlockBlobClient("xxx");

        using (var ms = new MemoryStream())
        {
            StreamWriter writer = new StreamWriter(ms);
            writer.Write(RequestTime.ToString());
            writer.Flush();
            ms.Position = 0;
            await blobClient4.UploadAsync(ms);                
        }

Upvotes: 5

jjxtra
jjxtra

Reputation: 21180

I may be oversimplifying, but can't the text writer you create just write to outStream directly? Just make sure to wrap the text writer in a using statement as well so that it flushes properly...

Upvotes: 0

Related Questions