无法在 csharp 的 Azure Data Lake Gen2 中设置 blob 的 ContentType
Cannot set the ContentType of a blob in Azure Data Lake Gen2 in csharp
我正在使用 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):
感谢您的任何见解
我能够重现您所看到的行为。基本上问题出在 fileClient.FlushAsync()
方法上。在调用这个方法之前,我检查了文件的内容类型并且设置正确。但是执行此方法后,内容类型更改为 application/octet-stream
(默认)。
查看此方法的文档here
,您也可以在此方法中设置headers。我尝试做同样的事情,内容类型更改为所需的类型。
var headers = new PathHttpHeaders()
{
ContentType = "text/plain"
};
await fileClient.FlushAsync(position: recordSize, httpHeaders: headers);
我正在使用 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):
感谢您的任何见解
我能够重现您所看到的行为。基本上问题出在 fileClient.FlushAsync()
方法上。在调用这个方法之前,我检查了文件的内容类型并且设置正确。但是执行此方法后,内容类型更改为 application/octet-stream
(默认)。
查看此方法的文档here
,您也可以在此方法中设置headers。我尝试做同样的事情,内容类型更改为所需的类型。
var headers = new PathHttpHeaders()
{
ContentType = "text/plain"
};
await fileClient.FlushAsync(position: recordSize, httpHeaders: headers);