Uploading blockblob and setting contenttype

后端 未结 6 1107
旧巷少年郎
旧巷少年郎 2020-12-15 16:20

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

This is how I\'m uploading things to storage:

// Store in storage
CloudStor         


        
相关标签:
6条回答
  • 2020-12-15 16:53

    Obviously best to set it on create like Gaurav Mantri's answer, if you are past that point and need to update the other answers here may mess you up.

    // GET blob
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); 
    
    // if you don't do this you'll wipe properties you didn't mean to
    await blockBlob.FetchAttributesAsync();
    
    // SET
    blockBlob.Properties.ContentType = mimetype;
    
    // SAVE
    await blockBlob.SetPropertiesAsync();
    
    0 讨论(0)
  • 2020-12-15 16:54

    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.

    0 讨论(0)
  • 2020-12-15 17:02

    Using the new SDK Azure.Storage.Blobs

    BlobHttpHeaders blobHttpHeaders = new BlobHttpHeaders();
    blobHttpHeaders.ContentType = "image/jpg";
    blobClient.SetHttpHeaders(blobHttpHeaders);
    
    0 讨论(0)
  • 2020-12-15 17:03

    Unfortunately, none of the answers provided here is currently working for the latest SDK (12.x.+)

    With the latest SDK, the content type should be set via BlobHttpHeaders.

    var _blobServiceClient = new BlobServiceClient("YOURCONNECTIONSTRING");
    
    var containerClient = _blobServiceClient.GetBlobContainerClient("YOURCONTAINERNAME");
    
    var blob = containerClient.GetBlobClient("YOURFILE.png");
    
    var blobHttpHeader = new BlobHttpHeaders();
    
    blobHttpHeader.ContentType = "image/png";
    
    var uploadedBlob = await blob.UploadAsync(YOURSTREAM, blobHttpHeader);
    
    0 讨论(0)
  • 2020-12-15 17:04

    with the new version of the Azure Blob SDK this is no longer working.

    this worked for me:

    CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(blobName);
    blockBlob.Properties.ContentType = contentType;                            
    await blockBlob.SetPropertiesAsync();
    
    0 讨论(0)
  • 2020-12-15 17:09

    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.

    0 讨论(0)
提交回复
热议问题