Malik Kashmiri
Malik Kashmiri

Reputation: 5851

upload azure blob using UploadFromStreamAsync Method

Problem

I am trying to generate Pdf and create MemoryStream object and trying to upload that stream to azure Blob Storage. I am already tried the below code so far but the blob is not uploaded to azure and also what is the name of that blob which I upload using stream method of azure sdk

Code

var memoryStream = new MemoryStream(byteArray, 0, byteArray.Length);
var cred = new StorageCredentials("foo", "key");
var account = new CloudStorageAccount(cred, true);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("container");
CloudBlockBlob sourceBlob = container.GetBlockBlobReference("foo/bar");
var attachment =  sourceBlob.UploadFromStreamAsync(memoryStream);

Upvotes: 2

Views: 4413

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17790

As @Kirk has said, use await sourceBlob.UploadFromStreamAsync(memoryStream); instead of var attachment = sourceBlob.UploadFromStreamAsync(memoryStream); Or your code will exit before upload is finished.

Note that your method should change to public async Task methodname(), you will see related tip shown by VS.

Some references for you

And see container.GetBlockBlobReference("blobname"); the string you use to get blob reference is the name of blob uploaded.

Upvotes: 3

Related Questions