These two posts looked promising:
How upload blob in Azure Blob Storage with specified ContentType with .NET v12 SDK?
Uploading blockblob and setting contenttype
But in both, they are using different libraries. (Why are there so many?) As mandated by powers out of my control, we are using BlobContainerClient. And within that class, we are using the UploadBlobAsync method.
public async Task<string> UploadAsync(string fileName, byte[] file, string containerName)
{
...
BlobContainerClient container = await createContainerIfNotExistsAsync(containerName);
using Stream stream = file.ToStream();
var result = await container.UploadBlobAsync(fileName, stream); // <-- does the upload.
...
}
By default, the type is set to application/octet-stream
. How can I override this?
Ok, figured it out. There are two ways you can upload a file to blob storage.
The first is how I was doing it, which was by bypassing the creation of the BlobClient
. See the OP for an example.
The second looks like this:
public async Task<string> UploadAsync(string fileName, byte[] file, string containerName)
{
...
BlobContainerClient container = await createContainerIfNotExistsAsync(containerName);
BlobClient blobClient = container.GetBlobClient(fileName);
using Stream stream = file.ToStream();
var result = await blobClient.UploadAsync(stream, new BlobHttpHeaders { ContentType = "text/plain" });
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With