1

I am new to Azure and I am moving my on-prem table to Azure blob as csv format. But in the Azure storage explorer, I am seeing the 'content type' column is showing as 'application/octet-stream' instead of 'csv'? . I am wondering why it is not showing as csv file ? .Can you explain, please ?

Regards

1 Answer 1

4

When you upload files in your Azure Storage account usually all files are uploaded with application/octet-stream as ContentType. If you just upload .csv files to Azure blob, it doesn't matter. You could change it to text/csv that you would like.

Modify ContentType:

private static CloudBlockBlob TrySetContentType(CloudBlockBlob blob, string contentType)
{
    if (blob.Properties.ContentType.ToLower() != contentType)
    {
        blob.Properties.ContentType = contentType;
        return blob;
    }

    return null;
}

For more details about ContentType property, see here.

In the latest (12.x+) New Azure SDK for .NET, setting content type needs to be done via BlobHttpHeaders object.

BlobClient blob = _container.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;
    case ".csv":
        blobHttpHeader.ContentType = "text/csv";
        break;
    default:
        break;
}

await using (var fileStream = new MemoryStream(fileBytes))
{
    var uploadedBlob = await blob.UploadAsync(fileStream, blobHttpHeader);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Much Pam. But where I should use or insert this code ?
Hi @GowriShankar, I edit my reply with the full code about modifying ContentType with C#. You could refer to this. If my reply is helpful, please accept it as answer, thank you.

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.