12

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

2 Answers 2

38

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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 Add the newlines to the string before Encoding.UTF8.GetBytes.
Thanks, @Gusman. I was able to do it with the help of Environment.NewLine before the one you said.
3
var blobServiceClient = new BlobServiceClient("connectionString") 
var containerClient = blobServiceClient.GetBlobContainerClient("container");
var blobClient = containerClient.GetBlobClient("blob");
await blobClient.UploadAsync(BinaryData.FromString("some string content"), overwrite: true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.