10

I have had a look at this following link to upload a string to azure blob. my task requirement does not allow me to store the string as a file.

Is there any way of writing file to Azure Blob Storage directly from C# application?

It is using CloudStorageAccount in WindowsAzure.Storage which is deprecated already as per this link

I am trying to use Azure.Storage.Blobs library. HOwever, there's no longer UploadString method as per this microsoft documentation

any advices? thanks

stuartd
  • 70,509
  • 14
  • 132
  • 163
jay
  • 1,055
  • 3
  • 15
  • 35

1 Answers1

32

You can upload a stream, so you can get the content of your string as a byte array, create a memory stream and upload it.

BlobContainerClient container = new BlobContainerClient(connectionString, "myContainer");
container.Create();

BlobClient blob = container.GetBlobClient("myString");

var myStr = "Hello!"
var content = Encoding.UTF8.GetBytes(myStr);
using(var ms = new MemoryStream(content))
    blob.Upload(ms);
Gusman
  • 14,905
  • 2
  • 34
  • 50
  • How to upload the same string separated by new lines (say I want to split the words into new lines) when I download and view the blob? – Deepak Dec 27 '21 at 09:45
  • @Deepak Add the newlines to the string before `Encoding.UTF8.GetBytes`. – Gusman Dec 27 '21 at 14:33
  • Thanks, @Gusman. I was able to do it with the help of `Environment.NewLine` before the one you said. – Deepak Dec 27 '21 at 17:11