Sam Vanhoutte
Sam Vanhoutte

Reputation: 3451

Cannot set the ContentType of a blob in Azure Data Lake Gen2 in csharp

I'm using the Azure.Storage.Files.DataLake nuget package to write and append file on my Azure Storage account that is enabled for Data Lake Gen2 (including hierarchy).

However, I need to set the Content Type of a new file I create and I don't succeed in it, although I thought I had things written correctly. Here's my code:

public async Task<bool> WriteBytes(string fileName, byte[] recordContent, string contentType)
{
    var directory = await CreateDirectoryIfNotExists();
    var fileClient = await directory.CreateFileAsync(fileName, 
        new PathHttpHeaders 
        {
            ContentType = contentType
        })).Value;

    long recordSize = recordContent.Length;
    var recordStream = new MemoryStream(recordContent);

    await fileClient.AppendAsync(recordStream, offset: 0);
    await fileClient.FlushAsync(position: recordSize);
    return true;
}

The result after the execution of the above code looks like this (the default content-type is kept):

enter image description here

Thanks for any insights

Upvotes: 1

Views: 646

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136226

I am able to reproduce the behavior you're seeing. Basically the issue is with fileClient.FlushAsync() method. Before calling this method, I checked the content type of the file and it was set properly. However after execution of this method, the content type was changed to application/octet-stream (which is the default).

Looking at the documentation for this method here, you can also set the headers in this method. I tried doing the same and the content type changed to the desired one.

        var headers = new PathHttpHeaders()
        {
            ContentType = "text/plain"
        };
        await fileClient.FlushAsync(position: recordSize, httpHeaders: headers);

Upvotes: 1

Related Questions