0
votes

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

1 Answers

0
votes

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);
}