0

I'm using Azure.Storage.Blobs library v12 to upload files to my Azure Blob Storage container.

It's working fine but I haven't been able to figure out a way to set the content-type for the file.

I found some documentation but the intellisense is not giving me any options to set BlobHttpHeaders. How do I set the content type for the file I upload?

Here's my code

public class StorageService : IStorageService
{
    private string containerName = "my-container";
    private static string connectionString = "my-connection-string";
    private readonly BlobServiceClient _client;

    public StorageService()
    {
        _client = new BlobServiceClient(connectionString);
    }

    public async Task<string> UploadFile(string filePath, string fileName)
    {
        try
        {
            // Generate a unique name for the blob we're uploading
            var blobName = Guid.NewGuid().ToString() + "__" + fileName;

            var _blobContainerClient = _client.GetBlobContainerClient(containerName);

            using (var fileStream = File.OpenRead(filePath))
                await _blobContainerClient.UploadBlobAsync(blobName, fileStream).ConfigureAwait(false);

            return blobName;
        }
        catch
        {
            throw new Exception();
        }
    }
}
4
  • What you mean by content-type? Commented Mar 15, 2022 at 20:59
  • mime type e.g. image/jpeg, etc. Commented Mar 15, 2022 at 21:00
  • Add it to the name of the stream your saving. for example fileName.jpeg Commented Mar 15, 2022 at 21:05
  • It's there already but that's not doing the trick though. When I check the uploaded file on Azure Blob Storage, it's showing application/octet-stream as its content type even though it's a jpg file. Commented Mar 15, 2022 at 21:15

1 Answer 1

3

Try

try
{
// Generate a unique name for the blob we're uploading
var blobName = Guid.NewGuid().ToString() + "__" + fileName;
var _blobContainerClient = _client.GetBlobContainerClient(containerName);

BlobClient blob = _blobContainerClient.GetBlobClient(fileName);

var blobHttpHeader = new BlobHttpHeaders();
string extension = Path.GetExtension(blob.Uri.AbsoluteUri);
switch (extension.ToLower())
{
    case ".jpg":
    case ".jpeg":
        blobHttpHeader.ContentType = "image/jpeg";
        break;
    case ".png":
        blobHttpHeader.ContentType = "image/png";
        break;
    case ".gif":
        blobHttpHeader.ContentType = "image/gif";
        break;
    default:
        break;
}

await using (var fileStream = new MemoryStream(imageBytes))
{
    var uploadedBlob = await blob.UploadAsync(fileStream, blobHttpHeader);
}
}
catch
{
  throw new Exception();
}

please check this too : check

Sign up to request clarification or add additional context in comments.

4 Comments

I see that you've switched from BlobServiceClient to BlobClient. In all honesty, I don't know the difference between the two. As you can see in my code, I was instantiating BlobServiceClient in the constructor so that I can reuse it in all my methods e.g. UploadFile() and other methods. The BlobClient wants filename when you instantiate it so how would I reuse it if I instantiate it inside the UploadFile() method because filename will be different everytime I use the BlobClient?
Looking at the documentation here, it is not possible to set the content type when using UploadBlobAsync method in BlobContainerClient. You will need to use BlobClient.
Hey @GauravMantri. Thanks for chiming in. Does that mean I’m creating a new instance of BlobClient every time I upload a file?
That’s correct and it’s completely fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.