Hello folks! on the previous versions of te Blob client SDK, it was easy to set the Content-Type of a blob using the blob reference.
Now I see that the new (v12) Azure.Storage.Blobs BlobClient can call GetPropertiesAsync() to get a BlobProperties object but I can't find how to set it.
So, how to properly set the file content type when uploading the blob?
I've found out await blob.SetHttpHeadersAsync(new BlobHttpHeaders { ContentType = GetContentType(fileType) }); but that requires another call to the the storage... Isn't there a better way for doing that while uploading the file?
Thanks!
//cc: @tg-msft
BlobClient.SetHttpHeadersAsync will let you set the Content-Type as you've noticed. You can also pass that BlobHttpHeaders type as the second parameter of BlobClient.UploadAsync to set the value when uploading.
Thanks for getting back so fast.
The problem is that when I use that overload that takes a BlobHttpHeaders, it doesn't allow me to set override: true and if I don't set that, I get the "blob already exist" response if I push a blob to override previous one.
Do we have an alternative way to set both headers and the override options?
Thanks!
Hummm...
await blob.UploadAsync(content, new BlobHttpHeaders{ ContentType = GetContentType(fileType)}, conditions: new BlobRequestConditions{ IfMatch = new ETag("*") });
Should that be the correct way?
Almost - if you want overwrite: true, that corresponds to conditions: null or just:
C#
await blob.UploadAsync(content, new BlobHttpHeaders { ContentType = GetContentType(fileType) });
Yeah, sorry, I forgot to mention that I've ended up with this:
blob = this._container.GetBlobClient(fileName);
await blob.UploadAsync(
content,
new BlobHttpHeaders
{
ContentType = GetContentType(fileType)
},
conditions: null);
sw.Stop();
Thanks folks! I'm closing the issue.
Thanks a lot @galvesribeiro for the useful info. Nevertheless, that solution is a little bit obscure for the general audience updating to V12. It would be great having an intuitive option like in previous versions. Not to mention that there is no explicit documentation about this in docs.microsoft....
The lack of documentation for this common use case is pretty poor.
Most helpful comment
The lack of documentation for this common use case is pretty poor.