使用 BlobContainerClient.UploadBlobAsync() 设置内容类型

use*_*704 4 c# azure

这两个帖子看起来很有希望:

如何使用 .NET v12 SDK 在 Azure Blob 存储中使用指定的 ContentType 上传 Blob?

上传 blockblob 并设置 contenttype

但在两者中,他们都使用不同的库。(为什么有这么多?)根据我无法控制的权力的要求,我们正在使用BlobContainerClient。在该类中,我们使用UploadBlobAsync方法。

public async Task<string> UploadAsync(string fileName, byte[] file, string containerName)
{
    ...
    BlobContainerClient container = await createContainerIfNotExistsAsync(containerName);
    using Stream stream = file.ToStream();
    var result = await container.UploadBlobAsync(fileName, stream);  // <-- does the upload.
    ...
}
Run Code Online (Sandbox Code Playgroud)

默认情况下,类型设置为application/octet-stream。我怎样才能覆盖这个?

use*_*704 9

好的,想通了。有两种方法可以将文件上传到 Blob 存储。

第一个是我是如何做的,这是通过绕过BlobClient. 有关示例,请参见 OP。

第二个看起来像这样:

public async Task<string> UploadAsync(string fileName, byte[] file, string containerName)
{
    ...
    BlobContainerClient container = await createContainerIfNotExistsAsync(containerName);
    BlobClient blobClient = container.GetBlobClient(fileName);
    using Stream stream = file.ToStream();
    var result = await blobClient.UploadAsync(stream, new BlobHttpHeaders { ContentType = "text/plain" });
    ...
}
Run Code Online (Sandbox Code Playgroud)