Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading blockblob and setting contenttype

I'm using Microsoft.WindowsAzure.Storage.* library from C#.

This is how I'm uploading things to storage:

// Store in storage CloudStorageAccount storageAccount = CloudStorageAccount.Parse("...connection string..."); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("pictures");  // Create container if it doesnt exist container.CreateIfNotExists();  // Make available to everyone container.SetPermissions(new BlobContainerPermissions {     PublicAccess = BlobContainerPublicAccessType.Blob });  // Save image CloudBlockBlob blockBlob = container.GetBlockBlobReference("blah.jpg"); blockBlob.UploadFromByteArray(byteArrayThumbnail, 0, byteArrayThumbnail.Length); blockBlob.Properties.ContentType = "image/jpg";  // *** NOT WORKING *** 

All the things I upload to the storage are being saved with content type "application/octet-stream", even though I'm using the setter with value "image/jpg" (see the last line in my code).

So question #1: Why isn't working the ContentType setter?

And question #2: If I manually change the content type to "image/jpg", using Windows Azure management portal, and then copy the absolute URI of the file to the browser's address field, and press enter, the jpg file is downloaded instead of displayed. Isn't this mime type supposed to be displayed instead of downloaded? How do I change this?

like image 697
sports Avatar asked Jul 07 '14 23:07

sports


2 Answers

Actually you don't have to call SetProperties method. In order to set content type while uploading the blob, just set the ContentType property before calling the upload method. So your code should be:

// Save image CloudBlockBlob blockBlob = container.GetBlockBlobReference("blah.jpg"); blockBlob.Properties.ContentType = "image/jpg"; blockBlob.UploadFromByteArray(byteArrayThumbnail, 0, byteArrayThumbnail.Length); 

and that should do the trick.

like image 105
Gaurav Mantri Avatar answered Oct 04 '22 18:10

Gaurav Mantri


After you make any changes to Properties, you have to make a call to CloudBlockBlob.SetProperties() to actually save those changes.

Think of it as something similar to LINQ-to-Entities. You can make any changes you want to your local object, but until you call SaveChanges(), nothing is actually saved.

like image 35
Nathan A Avatar answered Oct 04 '22 19:10

Nathan A