【问题标题】:Cannot set the ContentType of a blob in Azure Data Lake Gen2 in csharp无法在 csharp 中设置 Azure Data Lake Gen2 中 blob 的 ContentType
【发布时间】:2020-04-10 08:38:47
【问题描述】:

我正在使用 Azure.Storage.Files.DataLake nuget 包在启用 Data Lake Gen2(包括层次结构)的 Azure 存储帐户上写入和附加文件。

但是,我需要设置我创建的新文件的内容类型,但我没有成功,尽管我认为我写的东西是正确的。这是我的代码:

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

上面代码执行后的结果是这样的(保持默认的content-type):

感谢您的任何见解

【问题讨论】:

    标签: azure azure-storage azure-data-lake


    【解决方案1】:

    我能够重现您所看到的行为。基本上问题在于fileClient.FlushAsync() 方法。在调用此方法之前,我检查了文件的内容类型,并且设置正确。但是执行此方法后,内容类型更改为application/octet-stream(这是默认值)。

    查看这个方法here的文档,你也可以在这个方法中设置headers。我尝试做同样的事情,内容类型更改为所需的类型。

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

    【讨论】:

    • 嘿高拉夫!又厉害了!确认是这种行为
    猜你喜欢
    • 2019-05-22
    • 2021-10-26
    • 1970-01-01
    • 2019-12-02
    • 2021-02-12
    • 2019-09-10
    • 2020-09-22
    • 2019-11-26
    • 2021-03-06
    相关资源
    最近更新 更多