Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 845
Afshin Avatar asked Apr 12 '17 13:04

Afshin


Video Answer


1 Answers

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);
like image 70
Gaurav Mantri Avatar answered Oct 27 '22 06:10

Gaurav Mantri