使用 BlobServiceClient 设置内容类型
Setting content type using BlobServiceClient
我正在使用 Azure.Storage.Blobs
library v12 将文件上传到我的 Azure Blob 存储容器。
它工作正常,但我还没有找到为文件设置 content-type
的方法。
我找到了一些文档,但智能感知没有给我任何设置选项 BlobHttpHeaders
。如何设置上传文件的内容类型?
这是我的代码
public class StorageService : IStorageService
{
private string containerName = "my-container";
private static string connectionString = "my-connection-string";
private readonly BlobServiceClient _client;
public StorageService()
{
_client = new BlobServiceClient(connectionString);
}
public async Task<string> UploadFile(string filePath, string fileName)
{
try
{
// Generate a unique name for the blob we're uploading
var blobName = Guid.NewGuid().ToString() + "__" + fileName;
var _blobContainerClient = _client.GetBlobContainerClient(containerName);
using (var fileStream = File.OpenRead(filePath))
await _blobContainerClient.UploadBlobAsync(blobName, fileStream).ConfigureAwait(false);
return blobName;
}
catch
{
throw new Exception();
}
}
}
尝试
try
{
// Generate a unique name for the blob we're uploading
var blobName = Guid.NewGuid().ToString() + "__" + fileName;
var _blobContainerClient = _client.GetBlobContainerClient(containerName);
BlobClient blob = _blobContainerClient.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;
default:
break;
}
await using (var fileStream = new MemoryStream(imageBytes))
{
var uploadedBlob = await blob.UploadAsync(fileStream, blobHttpHeader);
}
}
catch
{
throw new Exception();
}
请也检查一下:
check
我正在使用 Azure.Storage.Blobs
library v12 将文件上传到我的 Azure Blob 存储容器。
它工作正常,但我还没有找到为文件设置 content-type
的方法。
我找到了一些文档,但智能感知没有给我任何设置选项 BlobHttpHeaders
。如何设置上传文件的内容类型?
这是我的代码
public class StorageService : IStorageService
{
private string containerName = "my-container";
private static string connectionString = "my-connection-string";
private readonly BlobServiceClient _client;
public StorageService()
{
_client = new BlobServiceClient(connectionString);
}
public async Task<string> UploadFile(string filePath, string fileName)
{
try
{
// Generate a unique name for the blob we're uploading
var blobName = Guid.NewGuid().ToString() + "__" + fileName;
var _blobContainerClient = _client.GetBlobContainerClient(containerName);
using (var fileStream = File.OpenRead(filePath))
await _blobContainerClient.UploadBlobAsync(blobName, fileStream).ConfigureAwait(false);
return blobName;
}
catch
{
throw new Exception();
}
}
}
尝试
try
{
// Generate a unique name for the blob we're uploading
var blobName = Guid.NewGuid().ToString() + "__" + fileName;
var _blobContainerClient = _client.GetBlobContainerClient(containerName);
BlobClient blob = _blobContainerClient.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;
default:
break;
}
await using (var fileStream = new MemoryStream(imageBytes))
{
var uploadedBlob = await blob.UploadAsync(fileStream, blobHttpHeader);
}
}
catch
{
throw new Exception();
}
请也检查一下: check