Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
738 views
in Technique[技术] by (71.8m points)

c# - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...