Afshin
Afshin

Reputation: 149

How to upload a stream to Azure blob with a new name

I want to download an Azure blob into stream and then convert stream to image for resizing and then upload new image to Azure blob with a new name. now I can resize the image and upload the stream but I can't upload the stream with new name to Azure.
This is my code:

Stream stream = blockBlob.OpenRead();

Image newImage;          
Bitmap image = new Bitmap(stream);
newImage = new Bitmap(image, 20, 20);

var ms = new MemoryStream();
newImage.Save(ms, ImageFormat.Png);
ms.Position = 0;

blockBlob.UploadFromStream(ms);

Upvotes: 7

Views: 11836

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136369

This is actually pretty simple. What you could need to do is create an instance of CloudBlockBlob with new blob name and upload the content there. Assuming you wish to upload the new file in the same container, this is what you could do:

        Stream stream = blockBlob.OpenRead();

        Image newImage;
        Bitmap image = new Bitmap(stream);
        newImage = new Bitmap(image, 20, 20);

        var ms = new MemoryStream();
        newImage.Save(ms, ImageFormat.Png);
        ms.Position = 0;

        var newBlob = blockBlob.Container.GetBlockBlobReference("new-blob-name.png");
        newBlob.UploadFromStream(ms);

Upvotes: 7

Related Questions